1. //自定义realm
    2. public class CustomerRealm extends AuthorizingRealm {
    3. @Override
    4. protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    5. return null;
    6. }
    7. @Override
    8. protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
    9. //根据身份信息//从传过来的token获取到的用户名
    10. String principal = (String) token.getPrincipal();
    11. //在工厂中获取service对象
    12. UserService userService = (UserService) ApplicationContextUtils.getBean("userService");
    13. //根据身份信息查询
    14. User user = userService.findByUserName(principal);
    15. //用户不为空
    16. if(!ObjectUtils.isEmpty(user)){
    17. //返回数据库信息
    18. return new SimpleAuthenticationInfo(user.getUsername(), user.getPassword(),
    19. ByteSource.Util.bytes(user.getSalt()), this.getName());;
    20. }
    21. return null;
    22. }