Shiro使用之自定义realm的编写
来源:互联网 发布:下拉框淘宝店铺搜索 编辑:程序博客网 时间:2024/05/21 10:45
//MyRealm继承与Shiro自带的AuthorizingRealm 类public class MyRealm extends AuthorizingRealm { @Autowired private UserService userService; private RoleService roleService; /** 验证当前登录用户 * **/ @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { //获取token的身份信息(如登录名) String userName = (String)token.getPrincipal(); //通过token的身份信息获取实体user User user = userService.getUserByName(userName); //为什么不是通过id查询?因为token令牌的getPrincipal()获得是身份信息,就好像是登录时输入的username,没有谁会输入id一样。 //若果是自己进行登录的验证,把获取到user的id信息作为token的Principal,作为后续验证的凭据,思路应该可以,但是没试过。 if(user!=null){ //创建 (简单的验证信息) 包含(身份信息)、(凭证)、(realm域名)三个参数 //return authcInfo 由父类进行验证 AuthenticationInfo authcInfo = new SimpleAuthenticationInfo(user.getUserName(),user.getUserPassword(),getName()); return authcInfo; }else{ return null; } } /** 为当前登录的用户授予角色和权限 * **/ @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { //获取 主身份信息(如账户) String userName = (String)principals.getPrimaryPrincipal(); //创建简单的 授权信息 SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo(); //根据用户名查询当前所拥有的角色 Set<Role> roleSet = userService.getRole(userName); Set<String> roleString = new HashSet<String>(); Set<String> permissions = new HashSet<String>(); for(Role role : roleSet){ roleString.add(role.getRoleName()); permissions.add(roleService.getPermission(role.getRoleName()).getPermissionName()); } //为什么只传rolename的Set集合,为不直接传Set<Role>? //因为Shiro没有setRole<Role>的设置,只有set<String>,所以只能将角色实例变成角色名传给info authorizationInfo.setRoles(roleString); //shiro倒是有传Set<Permission>的设置,但是这个Permission的类型是Shiro框架自带的类,不是你自己创建的Permission类,两者不一定兼容 //所以还是将Set<String>的permission传给info authorizationInfo.setStringPermissions(permissions); return authorizationInfo; }}
0 0
- Shiro使用之自定义realm的编写
- shiro使用总结-自定义Realm
- 【Shiro】Apache Shiro架构之自定义realm
- 【Shiro】Apache Shiro架构之自定义realm
- 【Shiro】Apache Shiro架构之自定义realm
- 使用shiro进行登录校验;自定义realm的实现
- Apache shiro 笔记整理之自定义Realm
- Shiro-学习总结-认证之自定义realm
- shiro学习之路(3)----自定义Realm
- shiro自定义realm无法使用注解
- shiro自定义Realm
- Shiro入门-自定义realm
- Shiro自定义Realm
- shiro自定义Realm实现
- Shiro 自定义realm认证
- 【shiro】--- 自定义realm
- Shiro(四) 自定义Realm
- 我的shiro之旅-realm
- 虚拟机安装Oracle Linux 6.6,启动进不了登录界面
- Kotlin语法(十九)-内联函数(Inline Functions)
- Android运行环境Dalvik模式和ART模式的区别
- hibernate注解的有关问题
- MD5加密 加密功能更好的一段代码
- Shiro使用之自定义realm的编写
- 查询某个表的字段名,字段类型,长度及字段说明,查询主键信息,查询自增列信息的存储过程
- CSS中em单位和px单位的区别
- E:无法修正错误,因为您要求某些软件包保持现状,就是它们破坏了软件包间的依赖关系
- 应用禁止屏幕旋转
- Fragment的生命周期
- Referrer Policy 介绍
- 关于某些网站的图片盗链功能,相关http 请求的解决方法
- 1052 Tian Ji -- The Horse Racing 田忌赛马 贪心算法