Files
shiro-freemaker-springboot-…/demo-shiro-web/test
ouburikou 91ab2ed237 commit
2018-02-08 16:45:14 +08:00

66 lines
2.9 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 权限认证为当前登录的Subject授予角色和权限
* @see本例中该方法的调用时机为需授权资源被访问时
* @see 并且每次访问需授权资源时都会执行该方法中的逻辑这表明本例中默认并未启用AuthorizationCache
* @see 如果连续访问同一个URL比如刷新该方法不会被重复调用Shiro有一个时间间隔也就是cache时间在ehcache-shiro.xml中配置超过这个时间间隔再刷新页面该方法会被执行
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
//获取当前登录输入的用户名,等价于(String) principalCollection.fromRealm(getName()).iterator().next();
String loginName = (String)super.getAvailablePrincipal(principalCollection);
//到数据库查是否有此对象
ShiroUser user= shiroUserService.findByUsername(loginName);
if(user!=null){
//权限信息对象info,用来存放查出的用户的所有的角色role及权限permission
SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();
//用户的角色集合
info.setRoles(user.getRolesName());
List<ShiroRole> roleList=user.getRoleList();
for (ShiroRole role : roleList) {
info.addStringPermissions(role.getPermissionsName());
}
return info;
}
// 返回null的话就会导致任何用户访问被拦截的请求时都会自动跳转到unauthorizedUrl指定的地址
return null;
}
/**
* 登录认证
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(
AuthenticationToken authenticationToken) throws AuthenticationException {
//UsernamePasswordToken对象用来存放提交的登录信息
UsernamePasswordToken token=(UsernamePasswordToken) authenticationToken;
//查出是否有此用户
ShiroUser user= shiroUserService.findByUsername(token.getUsername());
if(user==null){
throw new UnknownAccountException();//账户不存在
}
if(user!=null&&user.getStatus().equals(Constants.userStatus_2)){
throw new LockedAccountException(); //账户被锁
}
if(user!=null&&user.getStatus().equals(Constants.userStatus_0)){
throw new DisabledAccountException();//账户不存在
}
if(user!=null&&user.getStatus().equals(Constants.userStatus_1)){
// 若存在将此用户存放到登录认证info中无需自己做密码对比Shiro会为我们进行密码对比校验
return new SimpleAuthenticationInfo(user.getUsername(), user.getPassword(), ByteSource.Util.bytes("www"),getName());
}
return null;
}
}