shiro学习笔记-UserRealm-BeanUtils-SimpleAuthenticationInfo

UserRealm

public class UserRealm extends AuthorizingRealm {
    
    
    private UserService userService = new UserServiceImpl();
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    
    
        String username = (String)principals.getPrimaryPrincipal();
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        authorizationInfo.setRoles(userService.findRoles(username));
        authorizationInfo.setStringPermissions(userService.findPermissions(username));
        return authorizationInfo;
    }
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
    
    
        String username = (String)token.getPrincipal();
        User user = userService.findByUsername(username);
        if(user == null) {
    
    
            throw new UnknownAccountException();//没找到帐号
        }
        if(Boolean.TRUE.equals(user.getLocked())) {
    
    
            throw new LockedAccountException(); //帐号锁定
        }
        //交给AuthenticatingRealm使用CredentialsMatcher进行密码匹配,如果觉得人家的不好可以在此判断或自定义实现
        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
                user.getUsername(), //用户名
                user.getPassword(), //密码
                ByteSource.Util.bytes(user.getCredentialsSalt()),//salt=username+salt
                getName()  //realm name
        );
        return authenticationInfo;
    }
}

1、UserRealm 父类 AuthorizingRealm 将获取 Subject 相关信息分成两步:获取身份验证信息(doGetAuthenticationInfo)及授权信息(doGetAuthorizationInfo);

2、doGetAuthenticationInfo 获取身份验证相关信息:首先根据传入的用户名获取 User 信息;然后如果 user 为空,那么抛出没找到帐号异常 UnknownAccountException;如果 user 找到但锁定了抛出锁定异常 LockedAccountException;最后生成 AuthenticationInfo 信息,交给间接父类 AuthenticatingRealm 使用 CredentialsMatcher 进行判断密码是否匹配,如果不匹配将抛出密码错误异常 IncorrectCredentialsException;另外如果密码重试此处太多将抛出超出重试次数异常 ExcessiveAttemptsException;在组装 SimpleAuthenticationInfo 信息时,需要传入:身份信息(用户名)、凭据(密文密码)、盐(username+salt),CredentialsMatcher 使用盐加密传入的明文密码和此处的密文密码进行匹配。

3、doGetAuthorizationInfo 获取授权信息:PrincipalCollection 是一个身份集合,因为我们现在就一个 Realm,所以直接调用 getPrimaryPrincipal 得到之前传入的用户名即可;然后根据用户名调用 UserService 接口获取角色及权限信息。

一、简介:

BeanUtils提供对Java反射和自省API的包装。其主要目的是利用反射机制对JavaBean的属性进行处理。我们知道,一个JavaBean通常包含了大量的属性,很多情况下,对JavaBean的处理导致大量get/set代码堆积,增加了代码长度和阅读代码的难度。

二、用法:

如果你有两个具有很多相同属性的JavaBean,一个很常见的情况就是PO对象(持久对象)和Dto对象(传输对象)。传统的方式是使用类似下面的语句对属性逐个赋值:

// 逐一赋值  
userDto.setUsername(user.getUsername);  
  
userDto.setPassword(user.getPassword);  
  
userDto.setAge(user.getAge);  
.........
.........

使用 BeanUtils.copyProperties() 方法以后,代码量大大的减少,而且整体程序看着也简洁明朗,代码如下:

BeanUtils.copyProperties(userDto, user);  //第一个参数是转换后的类,第二个参数是待转换的类

如果User和UserDto间存在名称不相同的属性,则BeanUtils不对这些属性进行处理,需要手动处理。

SimpleAuthenticationInfo中可以传三个参数也可以传四个参数。

第一个参数:传入的都是com.java.entity包下的User类的user对象。

注意:此参数可以通过subject.getPrincipal()方法获取—获取当前记录的用户,从这个用户对象进而再获取一系列的所需要的属性。

Subject subject = SecurityUtils.getSubject();
User user = (User) subject.getPrincipal();

第二个参数: 传入的是从数据库中获取到的password,然后再与token中的password进行对比,匹配上了就通过,匹配不上就报异常。

第三个参数,盐–用于加密密码对比。 若不需要,则可以设置为空 “ ”

第四个参数:当前realm的名字。

猜你喜欢

转载自blog.csdn.net/qq_46199553/article/details/120611386
今日推荐