Spring如何整合Shiro做权限控制模块
这篇文章主要介绍Spring如何整合Shiro做权限控制模块,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
1.引入Shiro的Maven依赖
<!-- Spring 整合Shiro需要的依赖 --> <<</<</<</</<<</<</<</</<<</<</<</</<<</<</<</</<!-- 除此之外还有一些东西也不可少spring, spring-mvc, ibatis等 spring.3.1.2 spring-mvc.3.1.2 ibatis.2.3.4 cglib.2.2 -->
2.web.xml中配置
<!-- 配置shiro的核心拦截器 --> <<</<</</<<</<</</3. 编写自己的UserRealm类继承自Realm,主要实现认证和授权的管理操作package com.jay.demo.shiro;import java.util.HashSet;import java.util.Iterator;import java.util.Set;import org.apache.shiro.authc.AuthenticationException;import org.apache.shiro.authc.AuthenticationInfo;import org.apache.shiro.authc.AuthenticationToken;import org.apache.shiro.authc.LockedAccountException;import org.apache.shiro.authc.SimpleAuthenticationInfo;import org.apache.shiro.authc.UnknownAccountException;import org.apache.shiro.authz.AuthorizationInfo;import org.apache.shiro.authz.SimpleAuthorizationInfo;import org.apache.shiro.realm.AuthorizingRealm;import org.apache.shiro.subject.PrincipalCollection;import org.springframework.beans.factory.annotation.Autowired;import com.jay.demo.bean.Permission;import com.jay.demo.bean.Role;import com.jay.demo.bean.User;import com.jay.demo.service.UserService;public class UserRealm extends AuthorizingRealm{@Autowiredprivate UserService userService; @Overrideprotected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {//String username = (String) getAvailablePrincipal(principals); String username = (String) principals.getPrimaryPrincipal(); Set<Role> roleSet = userService.findUserByUsername(username).getRoleSet(); //角色名的集合 Set<String> roles = new HashSet<String>(); //权限名的集合 Set<String> permissions = new HashSet<String>();Iterator<Role> 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;} @Overrideprotected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { String username = (String) token.getPrincipal();User user = userService.findUserByUsername(username); if(user==null){ //木有找到用户 throw new UnknownAccountException("没有找到该账号");} SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user.getUsername(), user.getPassword(),getName()); return info;}@Overridepublic String getName() { return getClass().getName();}}
4.在Spring的applicationContext.xml中进行Shiro的相关配置
添加shiroFilter定义
Xml代码
<!-- Shiro Filter -->
< bean id = "shiroFilter" class = "org.apache.shiro.spring.web.ShiroFilterFactoryBean" >
< property name = "securityManager" ref = "securityManager" />
< property name = "loginUrl" value = "/login" />
< property name = "successUrl" value = "/user/list" />
< property name = "unauthorizedUrl" value = "/login" />
< property name = "filterChainDefinitions" >
< value >
/ login = anon
/user/** = authc
/role/edit/* = perms[role:edit]
/role/ save = perms [role:edit]
/role/ list = perms [role:view]
/** = authc
</ value >
</ property >
</ bean >
添加securityManager定义
Xml代码
< bean id = "securityManager" class = "org.apache.shiro.web.mgt.DefaultWebSecurityManager" >
< property name = "realm" ref = "myRealm" />
</ bean >
添加realm定义
Xml代码
< bean id = " myRealm" class = "com.jay.demo.shiro.
UserRealm<span class="attribute-value" style="font-size: 250, 250, 250);">"<span style="color: black; font-size: 1em; font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; background-color: rgb(250, 250, 250);"> <span class="tag" style="font-size: 0, 102, 153); font-weight: bold; background-color: rgb(250, 250, 250);">/><span >
配置EhCache
< bean id = "cacheManager" class = "org.apache.shiro.cache.ehcache.EhCacheManager" />
保证实现了Shiro内部lifecycle函数的bean执行
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
特别注意:
如果使用Shiro相关的注解,需要在springmvc-servlet.xml中配置一下信息
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor"/><"org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"> <"securityManager" "securityManager"/></
以上是“Spring如何整合Shiro做权限控制模块”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注编程网行业资讯频道!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341