Java tutorial
/******************************************************************************* * Copyright (c) 2005, 2014 springside.github.io * * Licensed under the Apache License, Version 2.0 (the "License"); *******************************************************************************/ package cn.dsgrp.field.stock.service.account; import java.math.BigInteger; import java.util.List; import org.apache.commons.lang3.StringUtils; import org.apache.shiro.SecurityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import cn.dsgrp.field.stock.entity.User; import cn.dsgrp.field.stock.repository.TaskDao; import cn.dsgrp.field.stock.repository.UserDao; import cn.dsgrp.field.stock.service.ServiceException; import cn.dsgrp.field.stock.service.account.ShiroDbRealm.ShiroUser; import org.springside.modules.security.utils.Digests; import org.springside.modules.utils.Clock; import org.springside.modules.utils.Encodes; /** * ?. * * @author calvin */ // Spring Service Bean. @Component @Transactional public class AccountService { public static final String HASH_ALGORITHM = "SHA-1"; public static final int HASH_INTERATIONS = 1024; private static final int SALT_SIZE = 8; private static Logger logger = LoggerFactory.getLogger(AccountService.class); private UserDao userDao; private TaskDao taskDao; private Clock clock = Clock.DEFAULT; public List<User> getAllUser() { return (List<User>) userDao.findAll(); } public User getUser(BigInteger id) { return userDao.findOne(id); } public User findUserByLoginName(String loginName) { return userDao.findByLoginName(loginName); } public void registerUser(User user) { entryptPassword(user); user.setRoles("user"); user.setRegisterDate(clock.getCurrentDate()); userDao.save(user); } public void updateUser(User user) { if (StringUtils.isNotBlank(user.getPlainPassword())) { entryptPassword(user); } userDao.save(user); } public void deleteUser(BigInteger id) { if (isSupervisor(id)) { logger.warn("?{}??", getCurrentUserName()); throw new ServiceException("??"); } userDao.delete(id); // taskDao.deleteByUserId(id); } /** * ??. */ private boolean isSupervisor(BigInteger id) { return id.longValue() == 1; } /** * ?Shiro?LoginName. */ private String getCurrentUserName() { ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal(); return user.loginName; } /** * ???salt?1024 sha-1 hash */ private void entryptPassword(User user) { byte[] salt = Digests.generateSalt(SALT_SIZE); user.setSalt(Encodes.encodeHex(salt)); byte[] hashPassword = Digests.sha1(user.getPlainPassword().getBytes(), salt, HASH_INTERATIONS); user.setPassword(Encodes.encodeHex(hashPassword)); } @Autowired public void setUserDao(UserDao userDao) { this.userDao = userDao; } @Autowired public void setTaskDao(TaskDao taskDao) { this.taskDao = taskDao; } public void setClock(Clock clock) { this.clock = clock; } }