这篇文章主要介绍春天如何整合Shiro做权限控制模块,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
1.引入Shiro的Maven依赖
!春天整合Shiro需要的依赖- ////////////////!-除此之外还有一些东西也不可少spring,spring-mvc,ibatis等spring.3.1.2spring-mvc.3.1.2
伊巴蒂斯。2 .3 .4油嘴滑舌。2.2-
2.web.xml中配置
!-配置shiro的核心拦截器- //////
3.编写自己的用户领域类继承自领域,主要实现认证和授权的管理操作打包。杰。演示。Shiro导入Java。乌提尔。hashset导入Java。乌提尔。迭代器;导入Java。乌提尔。设置;导入组织。阿帕奇。Shiro。authc。authenticationexception导入组织。阿帕奇。Shiro。authc。authenticationinfo导入组织。阿帕奇。Shiro。authc。authenticationtoken导入组织。阿帕奇。Shiro。authc。LockeDaccountException导入组织。阿帕奇。Shiro。authc。simpleauthenticationinfo导入组织。阿帕奇。Shiro。authc。未知帐户异常;导入组织。阿帕奇。Shiro。奥兹。authorizationinfo导入组织。阿帕奇。Shiro。奥兹。simpleauthorizationinfo导入组织。阿帕奇。Shiro。领域。authorizinggrealm导入组织。阿帕奇。Shiro。主题。本金收款;导入组织。弹簧框架。豆子。工厂。注释。自动连线;导入com。杰。演示。比恩。许可;导入com。杰。演示。比恩。角色;导入com。杰。演示。比恩。用户;导入com。杰。演示。服务。用户服务;
公共类user ealme extendsauthorizngrealm {
@自动连线
privateserviceuserservice/* *
*授权操作
*/@覆盖
protectedAuthorizationInfodoGetAuthorizationInfo(principalcollectionprinciples){//Stringusername=(String)get available principles(principles);Stringusername=(字符串)原则。getprimaryprincipal();setRoleSet=userServiCe。FindUserByusername(用户名)。getRoleSet();//角色名的集合SetStringroles=new hashsetstring();//权限名的集合
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;
} /**
* 身份验证操作
*/ @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代码
-
<!-- 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 >
2、添加securityManager定义
Xml代码
-
< bean id = "securityManager" class = "org.apache.shiro.web.mgt.DefaultWebSecurityManager" >
-
< property name = "realm" ref = "myRealm" />
-
</ bean >
3、添加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 >
4、配置EhCache
< bean id = "cacheManager" class = "org.apache.shiro.cache.ehcache.EhCacheManager" />
5、 保证实现了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做权限控制模块”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!
内容来源网络,如有侵权,联系删除,本文地址:https://www.230890.com/zhan/157058.html