zoukankan      html  css  js  c++  java
  • shiro 定义realm

    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接口获取角色及权限信息。

  • 相关阅读:
    Winform学习笔记
    ASP.NET后台注册javascript脚本方法
    使用MultipleActiveResultSets复用Sql Server 2008数据库连接
    angular 2 新建组件命令
    一个关于日志操作方法
    vs2017 打开包管理器 (程序包管理控制台)
    Asp.Net Core Identity 怎么获取当前登录的用户信息?
    abp 实现所有审计的接口
    IIS8.5 布署 Asp.Net Core 老是报500的错误怎么办?
    .NET Core 1.1布署后出现“HTTP Error 502.5 Process Failure”的解决办法
  • 原文地址:https://www.cnblogs.com/rxl007/p/5812610.html
Copyright © 2011-2022 走看看