参考:
http://kdboy.iteye.com/blog/1103794
http://blog.csdn.net/hxpjava1/article/details/7035724
http://blog.itpub.net/23071790/viewspace-709367/
http://www.infoq.com/cn/articles/apache-shiro
org.apache.shiro.spring.web.ShiroFilterFactoryBean DOC
在web.xml中配置Shiro的过滤器
<filter> <filter-name>shiroFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>shiroFilter</filter-name> <url-pattern>/*</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> </filter-mapping>
可以在<filter></filter>中加入targetFilterLifecycle
<init-param> <param-name>targetFilterLifecycle</param-name> <param-value>true</param-value> </init-param>
如果设置"targetFilterLifecycle"为true,则spring来管理Filter.init()和Filter.destroy();若为false,则这两个方法失效。
在Spring的配置文件中添加Shiro的过滤器
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <property name="securityManager" ref="securityManager" /> <property name="loginUrl" value="/login" /> <property name="successUrl" value="/wellcome" /> <property name="unauthorizedUrl" value="/forbid" /> <property name="filterChainDefinitions"> <value> /login = authc /logout = logout /account/** = user /admin/** = authcBasic </value> </property> </bean>
这个时候就遇到一个问题,Shiro过滤器都是干什么用的?
anon | org.apache.shiro.web.filter.authc.AnonymousFilter |
authc | org.apache.shiro.web.filter.authc.FormAuthenticationFilter |
authcBasic |
org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter
|
perms |
org.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter
|
port |
org.apache.shiro.web.filter.authz.PortFilter
|
rest |
org.apache.shiro.web.filter.authz.HttpMethodPermissionFilter
|
roles |
org.apache.shiro.web.filter.authz.RolesAuthorizationFilter
|
ssl | org.apache.shiro.web.filter.authz.SslFilter |
user | org.apache.shiro.web.filter.authc.UserFilter |
写道
rest:例子/admins/user/**=rest[user],根据请求的方法,相当于/admins/user/**=perms[user:method] ,其中method为post,get,delete等。
port:例子/admins/user/**=port[8081],当请求的url的端口不是8081是跳转到schemal://serverName:8081?queryString,其中schmal是协议http或https等,serverName是你访问的host,8081是url配置里port的端口,queryString
是你访问的url里的?后面的参数。
perms:例子/admins/user/**=perms[user:add:*],perms参数可以写多个,多个时必须加上引号,并且参数之间用逗号分割,例如/admins/user/**=perms["user:add:*,user:modify:*"],当有多个参数时必须每个参数都通过才通过,想当于
isPermitedAll()方法。
roles:例子/admins/user/**=roles[admin],参数可以写多个,多个时必须加上引号,并且参数之间用逗号分割,当有多个参数时,例如/admins/user/**=roles["admin,guest"],每个参数通过才算通过,相当于hasAllRoles()方法。
anon:例子/admins/**=anon 没有参数,表示可以匿名使用。
authc:例如/admins/user/**=authc表示需要认证才能使用,没有参数
authcBasic:例如/admins/user/**=authcBasic没有参数表示httpBasic认证
ssl:例子/admins/user/**=ssl没有参数,表示安全的url请求,协议为https
user:例如/admins/user/**=user没有参数表示必须存在用户,当登入操作时不做检查
这些过滤器分为两组,一组是认证过滤器,一组是授权过滤器。其中anon,authcBasic,auchc,user是第一组,
perms,roles,ssl,rest,port是第二组
port:例子/admins/user/**=port[8081],当请求的url的端口不是8081是跳转到schemal://serverName:8081?queryString,其中schmal是协议http或https等,serverName是你访问的host,8081是url配置里port的端口,queryString
是你访问的url里的?后面的参数。
perms:例子/admins/user/**=perms[user:add:*],perms参数可以写多个,多个时必须加上引号,并且参数之间用逗号分割,例如/admins/user/**=perms["user:add:*,user:modify:*"],当有多个参数时必须每个参数都通过才通过,想当于
isPermitedAll()方法。
roles:例子/admins/user/**=roles[admin],参数可以写多个,多个时必须加上引号,并且参数之间用逗号分割,当有多个参数时,例如/admins/user/**=roles["admin,guest"],每个参数通过才算通过,相当于hasAllRoles()方法。
anon:例子/admins/**=anon 没有参数,表示可以匿名使用。
authc:例如/admins/user/**=authc表示需要认证才能使用,没有参数
authcBasic:例如/admins/user/**=authcBasic没有参数表示httpBasic认证
ssl:例子/admins/user/**=ssl没有参数,表示安全的url请求,协议为https
user:例如/admins/user/**=user没有参数表示必须存在用户,当登入操作时不做检查
这些过滤器分为两组,一组是认证过滤器,一组是授权过滤器。其中anon,authcBasic,auchc,user是第一组,
perms,roles,ssl,rest,port是第二组
authcBasic就是弹出一个登录窗口,要求用户输入用户名和密码。适合于对安全性要求不高的系统或设备中,如大家所用路由器的配置页面的认证
还有就是自定义过滤器了,举个官方的例子
<bean id="myCustomFilter" class="com.class.that.implements.javax.servlet.Filter"/> <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <property name="filterChainDefinitions"> <value> /some/path/** = authc, myCustomFilter </value> </property> </bean>
在Spring的配置文件中添加securityManager和realm,加了个ehcache
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="realm" ref="shiroDbRealm" /> <property name="cacheManager" ref="shiroEhcacheManager" /> </bean> <bean id="shiroDbRealm" class="com.**.ShiroDbRealm"> <property name="accountService" ref="accountService"/> </bean> <bean id="shiroEhcacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager"> <property name="cacheManagerConfigFile" value="classpath:security/ehcache-shiro.xml"/> </bean>
ehcache的配置
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE xml> <ehcache updateCheck="false" name="shiroCache"> <!-- http://ehcache.org/ehcache.xml --> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="false" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" /> </ehcache>
又加上了AOP的权限控制和生命周期的配置
<!-- 保证实现了Shiro内部lifecycle函数的bean执行 --> <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/> <!-- AOP式方法级权限检查 --> <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor"> <property name="proxyTargetClass" value="true" /> </bean> <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"> <property name="securityManager" ref="securityManager"/> </bean>
以上已经将配置的东西都弄完了,好像很简单的样子
配置完成就是Java的一些实现了,先说Realm
public class ShiroDbRealm extends AuthorizingRealm { protected AccountService accountService; /** * 认证回调函数,登录时调用. */ @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException { UsernamePasswordToken token = (UsernamePasswordToken) authcToken; User user = accountService.findUserByLoginId(token.getUsername()); if (user != null) { if ("disabled".equals(user.getStatus())) { throw new DisabledAccountException(); } byte[] salt = Encodes.decodeHex(user.getSalt()); return new SimpleAuthenticationInfo(new ShiroUser(user.getLoginId(), user.getName()), user.getPassword(), ByteSource.Util.bytes(salt), getName()); } else { return null; } } /** * 授权查询回调函数, 进行鉴权但缓存中无用户的授权信息时调用. */ @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { ShiroUser shiroUser = (ShiroUser) principals.getPrimaryPrincipal(); User user = accountService.findUserByLoginId(shiroUser.loginName); if (user != null) { Hibernates.initLazyProperty(user.getRoles()); } SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(); for (Role role : user.getRoles()) { // 基于Role的权限信息 info.addRole(role.getName()); // 基于Permission的权限信息 info.addStringPermissions(role.getPermissionList()); } return info; } /** * 设定Password校验的Hash算法与迭代次数. */ @PostConstruct public void initCredentialsMatcher() { HashedCredentialsMatcher matcher = new HashedCredentialsMatcher(AccountService.HASH_ALGORITHM); matcher.setHashIterations(AccountService.HASH_INTERATIONS); setCredentialsMatcher(matcher); } public void setAccountService(AccountService accountService) { this.accountService = accountService; } }
自定义了一个ShiroUser,目的是为了保存更多的用户信息
/** * 自定义Authentication对象,使得Subject除了携带用户的登录名外还可以携带更多信息. */ public static class ShiroUser implements Serializable { private static final long serialVersionUID = -1373760761780840081L; public String loginName; public String name; public ShiroUser(String loginName, String name) { this.loginName = loginName; this.name = name; } public String getName() { return name; } /** * 本函数输出将作为默认的<shiro:principal/>输出. */ @Override public String toString() { return loginName; } /** * 重载hashCode,只计算loginName; */ @Override public int hashCode() { return Objects.hashCode(loginName); } /** * 重载equals,只计算loginName; */ @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } ShiroUser other = (ShiroUser) obj; if (loginName == null) { if (other.loginName != null) { return false; } } else if (!loginName.equals(other.loginName)) { return false; } return true; } }
建了两个PO,user,role
public class User implements Serializable { private Long id; private String loginId; private String password; private String salt; private String name; private String email; private Integer status; private List<Role> roles = Lists.newArrayList(); ... get/set... }
public class Role implements Serializable { private Long id; private String name; private String permissions; ...get/set... public List<String> getPermissionList() { return ImmutableList.copyOf(StringUtils.split(permissions, ",")); } }
OK。其他的就是登录页面和Controller了,这个就不说了。
相关推荐
手把手教你集成spring cloud + shiro微服务...用最少的工作量,改造基于shiro安全框架的微服务项目,实现spring cloud + shiro 框架集成。博客地址:https://blog.csdn.net/weixin_42686388/article/details/103084289
但是该套实现,代码质量非常差(10几个java文件,都快吓晕),并且redis的连接没有基于spring,而是在java代码中硬编码,这几乎无法容忍。 为此,本人基于spring连接redis,吸取了他们文章的思想,重新写了一套解决...
通用后台系统,基于spring mvc,mybatis,Shiro,dwz 实现的
基于Maven+SSM整合shiro+Redis实现后台管理项目基于Maven+SSM整合shiro+Redis实现后台管理项目
使用SpringBoot与shiro实现基于数据库的细粒度动态权限管理系统实例。
Java语言的框架整合及权限的控制(粗粒度、细粒度) SSM(spring+springmvc+mybatis)的整合 SSM+shiro基于资源的权限控制整合demo 配置文件原理的注释 shiro的认证、授权、及从数据库mysql获取的认证信息方法都有...
本项目是基于Spring、Spring MVC、MiniJdbc和Shiro开发的权限管理系统LarvaFrame设计源码,包含436个文件,其中包括145个Java源文件,92个JavaScript文件,30个PNG图片,30个CSS样式文件,27个GIF图片,24个XML文件...
本资源提供了一套基于Spring4和Struts2的简单Shiro项目设计源码。该项目包含了11个XML配置文件、9个Java源文件、4个JSP文件、1个LICENSE文件、1个Markdown文件、1个META-INF文件、1个SQL文件、1个XSD文件,共29个...
本文小编将基于 SpringBoot 集成 Shiro 实现动态uri权限,由前端vue在页面配置uri,Java后端动态刷新权
在线考试系统源码+项目说明(在线测试+辅助课程教学和学生学习,基于Spring Boot、Shiro、MyBatis、Redis、thymeleaf等框架).zip 在线考试系统源码+项目说明(在线测试+辅助课程教学和学生学习,基于Spring Boot、...
本系统采用企业级开发标准,使用SpringBoot架构,数据访问层采用Spring Data Jpa,业务控制层采用SpringMvc,安全框架采用Shiro,实现了完整权限系统,Controller方法采用shiro注解,来实现有效的权限控制;...
权限管理系统 shiro + ssm实现,实现菜单,有学习的可以下载哦!项目基于jdk1.8整合了springboot+mvc+mybatis(通用mapper)+druid+jsp+bootstrap等技术,springboot+Listener(监听器),Filter(过滤器),Interceptor(拦截器)...
项目基于jdk1.8整合了springboot+mvc+mybatis(通用mapper)+druid+jsp+bootstrap等技术,springboot+Listener(监听器),Filter(过滤器),Interceptor(拦截器),Servlet,springmvc静态资源,文件上传下载,多数据源切换,缓存...
0、重点!重点!...1、本教程适用所有开发人员简单易懂,结合文章教程与demo示例。...5、基于RBAC五张表:用户表 tb_user、角色表tb_role、权限表tb_permission、用户角、表tb_user_role、角色权限tb_role_permissio
本项目基于Spring,整合Apache Shiro框架,实现用户管理和权限控制,主要内容如下: 1.登录(带验证码),包括“记住我”的功能; 2.加密,存储的密码不采用明文,初始密码123; 3.session管理:使用shiro默认的...
基于Spring Boot+Vue+Shiro实现的前后端分离的代码生成器项目源码,可根据数据库字段动态生成各种类型代码 该项目可根据数据库字段动态生成 controller、mapper、service、html、jsp、vue、php、.py ... 等各种类型...
权限管理系统 shiro + ssm实现,实现菜单,有学习的可以下载哦!项目基于jdk1.8整合了springboot+mvc+mybatis(通用mapper)+druid+jsp+bootstrap等技术,springboot+Listener(监听器),Filter(过滤器),Interceptor(拦截器)...
1、资源内容:毕业设计&课设&实战演练--基于Spring Boot + MyBatis-Plus + Shiro + MySQL 基于Java的学生考勤系统 2、适用人群:计算机,电子信息工程、数学等专业的学习者,作为python参考资料学习借鉴使用。 3、本...
基于SpringBoot+shiro实现数据库的细粒度动态权限管理系统 项目经过严格测试,确保可以运行!源码无需做任何更改! 基于SpringBoot+shiro实现数据库的细粒度动态权限管理系统 项目经过严格测试,确保可以运行!...
SSM+Shiro+Druid实现的企业资产后台管理系统代码分享 项目描述 随着企业的发展,很多中小企业的规模越来越大,需要管理资产也越来越多,比如...spring+spring mvc+mybatis-plus+shiro+quartz+bootstrap+jquery+ajax