千家信息网

Spring如何整合Shiro做权限控制模块

发表于:2025-02-23 作者:千家信息网编辑
千家信息网最后更新 2025年02月23日,这篇文章主要介绍Spring如何整合Shiro做权限控制模块,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!1.引入Shiro的Maven依赖 <<

2.web.xml中配置

 << roleSet =  userService.findUserByUsername(username).getRoleSet(); //角色名的集合 Set roles = new HashSet(); //权限名的集合 Set permissions = new HashSet();                            Iterator it = roleSet.iterator(); while(it.hasNext()){                        roles.add(it.next().getName()); for(Permission per:it.next().getPermissionSet()){                                permissions.add(per.getName());                        }                }                               SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();                                authorizationInfo.addRoles(roles);                authorizationInfo.addStringPermissions(permissions); return authorizationInfo;        } /**         * 身份验证操作         */ @Override        protected AuthenticationInfo doGetAuthenticationInfo(                 AuthenticationToken token) throws AuthenticationException { String username = (String) token.getPrincipal();               User user = userService.findUserByUsername(username); if(user==null){ //木有找到用户 throw new UnknownAccountException("没有找到该账号");                } /* if(Boolean.TRUE.equals(user.getLocked())) {                      throw new LockedAccountException(); //帐号锁定                  } */ /**                 * 交给AuthenticatingRealm使用CredentialsMatcher进行密码匹配,如果觉得人家的不好可以在此判断或自定义实现                   */ SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user.getUsername(), user.getPassword(),getName()); return info;        }               @Override        public String getName() { return getClass().getName();        }}

4.在Spring的applicationContext.xml中进行Shiro的相关配置

1、添加shiroFilter定义

Xml代码

  1. < bean id = "shiroFilter" class = "org.apache.shiro.spring.web.ShiroFilterFactoryBean" >

  2. < property name = "securityManager" ref = "securityManager" />

  3. < property name = "loginUrl" value = "/login" />

  4. < property name = "successUrl" value = "/user/list" />

  5. < property name = "unauthorizedUrl" value = "/login" />

  6. < property name = "filterChainDefinitions" >

  7. < value >

  8. / login = anon

  9. /user/** = authc

  10. /role/edit/* = perms[role:edit]

  11. /role/ save = perms [role:edit]

  12. /role/ list = perms [role:view]

  13. /** = authc

2、添加securityManager定义

Xml代码

  1. < bean id = "securityManager" class = "org.apache.shiro.web.mgt.DefaultWebSecurityManager" >

  2. < property name = "realm" ref = "myRealm" />

3、添加realm定义

Xml代码

  1. < bean id = " myRealm" class = "com.jay.demo.shiro.

    UserRealm" />

4、配置EhCache

< bean id = "cacheManager" class = "org.apache.shiro.cache.ehcache.EhCacheManager" />

5、 保证实现了Shiro内部lifecycle函数的bean执行

特别注意:

如果使用Shiro相关的注解,需要在springmvc-servlet.xml中配置一下信息

<"org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">     <"securityManager" "securityManager"/>

以上是"Spring如何整合Shiro做权限控制模块"这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!

配置 权限 代码 模块 控制 整合 内容 篇文章 不好 价值 信息 兴趣 函数 密码 小伙 小伙伴 更多 注解 用户 知识 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 服务器管理能力是什么 长沙智能锁软件开发岗位 七点数据库 奉贤区个性化网络技术专业服务 民政局数据库登记贫困困境儿童 数据库加固检查内容 r320服务器风扇拆卸 网络安全征文大学生 网络安全网贷发生后除置程序结构 国外学术数据库怎么搜期刊 数据库查询速度最慢的语句 现代网络安全研究的论文 学生数据库中学号属于什么型 静安区数字led大屏服务器 数据库连接池最大链接数 国际服如何调整服务器 计算机网络技术薪资怎样 计算机网络技术培洲 深圳虾皮公司做软件开发的怎么样 青少网络安全注册解释pdf 战地5怎么成为服务器管理员 数据库的数据模型是哪几个类型 杭州ios软件开发招聘 管家婆安装数据库打不开 vivo软件开发校招面试 怎么租服务器放网页 小学二年级网络安全班会记录 诸城市软件开发有未来吗 靠谱的软件开发应用 索尼服务器
0