1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
| import cn.com.hellowood.springsecurity.mapper.UserMapper; import cn.com.hellowood.springsecurity.model.UserModel; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.authentication.AccountExpiredException; import org.springframework.security.authentication.BadCredentialsException; import org.springframework.security.core.AuthenticationException; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.userdetails.User; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.stereotype.Service;
import javax.servlet.http.HttpSession; import java.util.ArrayList;
/** * The type Custom user details service. * * @author HelloWood */ @Service("userDetailsService") public class CustomUserDetailsService implements UserDetailsService {
private Logger logger = LoggerFactory.getLogger(getClass());
@Autowired private UserMapper userMapper;
@Autowired private HttpSession session;
/** * 通过用户名和密码加载用户信息并校验 * * @param username the username * @param password the password * @return the user model * @throws AuthenticationException the authentication exception */ public UserModel loadUserByUsernameAndPassword(String username, String password) throws AuthenticationException { logger.info("user {} is login by username and password", username); UserModel user = userMapper.getUserByUsernameAndPassword(username, password); validateUser(username, user); return user; }
/** * 通过用户名加载用户信息,重写该方法用于记住密码后通过 Cookie 登录 * * @param username * @param user */ @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { logger.info("user {} is login by remember me cookie", username); UserModel user = userMapper.getUserByUsername(username); validateUser(username, user); return new User(user.getUsername(), user.getPassword(), new ArrayList<GrantedAuthority>()); }
/** * 校验用户信息并将用户信息放在 Session 中 * * @param username * @param user */ private void validateUser(String username, UserModel user) { if (user == null) { logger.error("user {} login failed, username or password is wrong", username); throw new BadCredentialsException("Username or password is not correct"); } else if (!user.getEnabled()) { logger.error("user {} login failed, this account had expired", username); throw new AccountExpiredException("Account had expired"); } // TODO There should add more logic to determine locked, expired and others status
logger.info("user {} login success", username); // 当用户信息有效时放入 Session 中 session.setAttribute("user", user); } }
|