Apache Shiro 是 Java 的一个安全(权限)框架。它能够很是容易的开发出足够安全的应用,其不只能够用在 JavaSE 环境,也能够用在 JavaEE 环境 。前端
Shiro 能够完成:认证、受权、加密、会话管理、与Web 集成、缓存 等。下载:http://shiro.apache.org/ 或 https://github.com/apache/shirojava
Shiro目标:Shiro开发团队所称的“应用程序安全”的四个基石——身份验证、受权、会话管理和密码git
还有额外的功能来支持和加强:github
从外部来看Shiro ,即从应用程序角度的来观察如何使用 Shiro 完成工做:web
从Shiro内部来看:spring
咱们先导入Shiro的所需jar,咱们先经过构建一个JavaSE应用来把Shiro运行起来 ,能够参照Shiro源码包下 (shiro-shiro-root-1.3.2\samples\quickstart)的快速开始数据库
<dependencies> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-all</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.6.4</version> </dependency> </dependencies>
Shiro须要slf4j来作日志,因此slf4j不能缺乏apache
这些配置文件直接放在Src下,若是是Maven项目就放在resources浏览器
咱们直接使用下载的Demo中quickstart下面的Quickstart.java类便可,它有一个main方法,咱们直接运行看到没有异常并打印出日志信息就是运行成功了缓存
咱们通常状况下都是使用Spring来整合Shiro,在Web环境下对URL进行验证等工做。其经过一个 ShiroFilter 入口来拦截须要安全控制的URL,而后进行相应的控制
ShiroFilter 相似于如 Strut2/SpringMVC 这种 web 框架的前端控制器,是安全控制的入口点,其负责读取配置(如ini 配置文件或Spring整合以后的filterChainDefinitions属性),而后判断URL是否须要登陆/权限等工做。
上面两步就不细说了。下面开始配置文件(能够参照:shiro-root-1.3.2-sourcerelease\shiro-root-1.3.2\samples\spring 配置 web.xml 文件和 Spring 的配置文件)
web.xml:主要配置关于Shiro的Filter
<!-- Filters 配置Shiro过滤器--> <filter> <filter-name>shiroFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> <init-param> <param-name>targetFilterLifecycle</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>shiroFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
注意:这个filter-name的值,咱们须要它与Spring整合文件的 org.apache.shiro.spring.web.ShiroFilterFactoryBean 这个bean的id值一致
Spring整合文件,主要的配置有
<!-- 1. 配置 SecurityManager安全管理器 --> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="cacheManager" ref="cacheManager"/> <property name="sessionMode" value="native"/> <property name="realm" ref="jdbcRealm"/> </bean> <!-- 2. 配置 CacheManager. (用户受权信息Cache) 2.1 须要加入 ehcache 的 jar 包及配置文件. --> <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager"> <property name="cacheManagerConfigFile" value="classpath:ehcache.xml"/> </bean> <!-- 3. 配置 Realm 3.1 直接配置实现了 org.apache.shiro.realm.Realm 接口的 bean --> <bean id="jdbcRealm" class="cn.lynu.realms.ShiroRealm"></bean> <!-- 4. 配置 LifecycleBeanPostProcessor. 能够自定的来调用配置在 Spring IOC 容器中 shiro bean 的生命周期方法. --> <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/> <!-- 5. 启用 IOC 容器中使用 shiro 的注解. 但必须在配置了 LifecycleBeanPostProcessor 以后才可使用. --> <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor"/> <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"> <property name="securityManager" ref="securityManager"/> </bean> <!-- 6. 配置 ShiroFilter. 6.1 id 必须和 web.xml 文件中配置的 DelegatingFilterProxy 的 <filter-name> 一致. 若不一致, 则会抛出: NoSuchBeanDefinitionException. 由于 Shiro 会来 IOC 容器中查找和 <filter-name> 名字对应的 filter bean. 能够设置targetBeanName指定Shiro的filter名 --> <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <property name="securityManager" ref="securityManager"/> <property name="loginUrl" value="/login.jsp"/> <property name="successUrl" value="/index.jsp"/> <property name="unauthorizedUrl" value="/unauthorized.jsp"/> <!-- 配置哪些页面须要受保护. 以及访问这些页面须要的权限. 1). anon 能够被匿名访问 2). authc 必须认证(即登陆)后才可能访问的页面. 3). logout 登出. 4). roles 角色过滤器 --> <property name="filterChainDefinitions"> <value> /login.jsp = anon # everything else requires authentication: /** = authc </value> </property> </bean>
我这里只配了一个login.jsp能够在未登陆状况下访问,访问其余的页面都会由于未受权而重定向到login.jsp
其格式是:“url=过滤器[参数]”。若是当前请求的 url 匹配 [urls] 部分的某个 url 模式,将会执行其配置的过滤器。部分Shiro内置的过滤器:
过滤器 | 过滤器类 | 描述 | 例子 |
anon | org,apache.shiro.web.filter.authc.AnonymousFilter | 能够直接访问 | /admin=anon |
authc | org,apache.shiro.web.filter.authc.FormAuthenticationFilter | 必须认证(登陆)以后才能访问 | /admin=authc |
logout | org,apache.shiro.web.filter.authc.logoutFilter | 注销登陆:全部的session都会失效,全部身份都会失去关联,remember Me Cookie也会删除 | /logout=logout |
user | org,apache.shiro.web.filter.authc.UserFilter | 表示只要有用户存在,不管是认证后或rememberMe均可以访问 | /admin=user |
roles | org,apache.shiro.web.filter.authc.RolesAuthorizcationFilter | 角色过滤器,判断当前用户是否为该角色。参数能够有多个,多个参数必须加上引号,多个参数之间用逗号分隔 | admin/**=roles["admin,guest"] |
url 模式使用 Ant 风格模式 ,Ant 路径通配符支持 ?、 * 、 **,注意通配符匹配不包括目录分隔符“/”:
其实咱们在Spring整合Shiro的配置文件的URL过滤器能够经过编码的方式配置,这样咱们能够将URL和过滤器配置到数据表中,经过编码查询数据库的方式得到对应的关系,添加进一个 LinkedHashMap。咱们就建一个名为 FilterChainDefinitionMapBuilder 的类,在这个类中写一个 buildFilterChainDefinitionMap方法,咱们在这个方法中能够查询数据库获得URL和过滤器的对应关系,这里就用模拟数据:
public class FilterChainDefinitionMapBuilder { public LinkedHashMap<String, String> buildFilterChainDefinitionMap(){ LinkedHashMap<String, String> map = new LinkedHashMap<>(); map.put("/login.jsp", "anon"); map.put("/shiro/login", "anon"); map.put("/shiro/logout", "logout"); map.put("/user.jsp", "authc,roles[user]"); map.put("/admin.jsp", "authc,roles[admin]"); map.put("/list.jsp", "user"); map.put("/**", "authc"); return map; }
而后修改Spring整合Shiro配置文件的 filterChainDefinitions :
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <property name="securityManager" ref="securityManager"/> <property name="loginUrl" value="/login.jsp"/> <property name="successUrl" value="/list.jsp"/> <property name="unauthorizedUrl" value="/unauthorized.jsp"/> <property name="filterChainDefinitionMap" ref="filterChainDefinitionMap"></property> </bean> <!-- 配置一个 bean, 该 bean 其实是一个 Map. 经过实例工厂方法的方式 --> <bean id="filterChainDefinitionMap" factory-bean="filterChainDefinitionMapBuilder" factory-method="buildFilterChainDefinitionMap"></bean> <bean id="filterChainDefinitionMapBuilder" class="cn.lynu.factory.FilterChainDefinitionMapBuilder"></bean>
这个 cn.lynu.factory.FilterChainDefinitionMapBuilder 就是咱们写的名为 FilterChainDefinitionMapBuilder 的类,buildFilterChainDefinitionMap就是这个类的中写的方法
使用Shiro能够对密码进行加密操做,常见的MD5,SHA1都是支持的。咱们在前端得到密码以后,Shiro会根据咱们的配置进行对应的加密,并于数据库中的加密后字符串进行比较,比较成功以后就会放行进入受保护的页面,比对失败则会重定向到登陆页。
咱们先写个Controller,若是访问该请求,handler类比Quickstart.java类访问咱们自定义的realm:
package cn.lynu.controller; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.AuthenticationException; import org.apache.shiro.authc.UsernamePasswordToken; import org.apache.shiro.subject.Subject; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller @RequestMapping("/shiro") public class ShiroController { @RequestMapping("/login") public String login(@RequestParam("userName") String userName, @RequestParam("password") String password) { Subject currentUser = SecurityUtils.getSubject(); if (!currentUser.isAuthenticated()) { //把用户名和密码封装为UsernamePasswordToken UsernamePasswordToken token = new UsernamePasswordToken(userName, password); //Remember Me 操做 token.setRememberMe(true); try { System.out.println("token:"+token.hashCode()); //执行登陆 currentUser.login(token); } //AuthenticationException是全部认证失败的父类 catch (AuthenticationException ae) { System.err.println("登陆失败:"+ae); } } return "redirect:/index.jsp"; } }
Subject的login方法就能够访问咱们配置的realm,咱们还须要在Spring的Shiro整合文件中配置这个URL为anon(未登陆也可访问)
再来看看咱们配置的realm,咱们先使用明文进行测试,这里的自定义realm继承于AuthenticatingRealm,AuthenticatingRealm实现了realm接口:
public class ShiroRealm extends AuthenticatingRealm{ @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { System.out.println("doGetAuthenticationInfo1:"+token.hashCode()); //1.将AuthenticationToken强转为UsernamePasswordToken UsernamePasswordToken uspaToken=(UsernamePasswordToken)token; //2.得到用户名 String userName=uspaToken.getUsername(); //3.查询数据库得到真实的用户名或密码(这里模拟) //3.1若用户不存在,抛出UnknownAccountException if("unknown".equals(userName)) { throw new UnknownAccountException("用户不存在"); } //3.2根据用户信息抛出其余信息(这里使用被锁定,抛出LockedAccountException) if("lock".equals(userName)){ throw new LockedAccountException("用户被锁定"); } //4.根据用户信息构建AuthenticationInfo,咱们经常使用其子类: //1).principal 用户实体信息 能够是userName,也能够是数据表对应的实体类信息 Object principal=userName; //2).credentials 密码 Object credentials="123"; //3).realmName 使用当前realName便可 String realmName=this.getName(); SimpleAuthenticationInfo info=new SimpleAuthenticationInfo(principal, credentials, realmName); return info; }
这里用户名和密码的在真实环境中应该从数据库中查到,为了不麻烦这里只要用户名不为unknown或lock,密码为123就能够登陆成功了。
若是用户名为unknown就抛一个 UnknownAccountException (不存在帐户的异常);若是用户名为lock就抛一个LockedAccountException(帐户锁定异常),这俩个异常类都是AuthenticationException的子类,因此能够抛出,AuthenticationException还有其余的子类,咱们能够根据用户信息来抛异常的方式终止登陆。
登陆成功以后咱们就会根据在Controller中配置的重定向到index.jsp,在以前未登陆的状况下,咱们是不能访问这个页面的
可是这里有个问题:咱们知道了Shiro内部使用缓存(ehCache)来保存验证,认证信息之类的,因此就出现了登陆成功以后再次回到登陆页,再次登陆不管输入什么用户名和密码都会登陆成功,由于登陆成功的信息已经被缓存,这的时候就须要咱们进行登出操做:
<a href="shiro/logout">登出</a>
这里简单的使用一个超连接访问URL->shiro/logout,咱们只须要在Spring的Shiro整合文件中配置这个URL为logout便可
只须要在这里配置一下,咱们点击登出超连接以后就会重定向到login.jsp要求再次登陆
下面来到重头戏:加密。这里以MD5加密为例,首先咱们须要在Spring的Shiro整合文件中配置自定义realm指定所需加密方式和加密次数:
加密次数与复杂度成正比,这样配置以后前端传过来的密码就会被Shiro以MD5进行1024次加密
由于存在于加密后的字符串进行比较,因此咱们必须先知道加密后的结果,真实的使用固然是从数据库中查出来,咱们这里简单点,使用Shiro提供的SimpleHash类来得到加密后的字符串。
在加密以前,咱们再来复习密码学中的加盐概念:相同的密码通过加盐加密以后就会获得不一样的加密字符串,这样咱们将不一样的加密字符串保存在数据库中,也不会看得出这是相同密码加密以后的结果,提升安全性。对于盐值的要求:惟一不重复,对于每一个用户使用其惟一的属性,例如用户名之类的做为盐值,这样不一样用户即便密码相同,加盐加密以后也会获得不一样的加密字符串。
public static void main(String[] args) { //加密方式 String algorithmName="MD5"; //密码 Object credentials="123"; //盐值(通常将一个惟一值做为盐值) Object salt=ByteSource.Util.bytes("admin"); //加密次数 int hashIterations=1024; SimpleHash simpleHash = new SimpleHash(algorithmName, credentials, salt, hashIterations); System.out.println(simpleHash); }
simpleHash就能够获得加密以后的字符串,这里盐值使用了Shiro提供的 ByteSource.Util.bytes 方法进行得到盐值,这里使用admin来得到盐值,由于一会咱们直接使用用户名为admin(将至关于在使用用户名这个惟一值来生成盐值),密码为123进行登陆
修改咱们自定义Realm(Shirorealm)的doGetAuthenticationInfo方法:
@Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { System.out.println("doGetAuthenticationInfo1:"+token.hashCode()); //1.将AuthenticationToken强转为UsernamePasswordToken UsernamePasswordToken uspaToken=(UsernamePasswordToken)token; //2.得到用户名 String userName=uspaToken.getUsername(); //3.查询数据库得到真实的用户名或密码(这里模拟) //3.1若用户不存在,抛出UnknownAccountException if("unknown".equals(userName)) { throw new UnknownAccountException("用户不存在"); } //3.2根据用户信息抛出其余信息(这里使用被锁定,抛出LockedAccountException) if("lock".equals(userName)){ throw new LockedAccountException("用户被锁定"); } //4.根据用户信息构建AuthenticationInfo,咱们经常使用其子类: //1).principal 用户实体信息 能够是userName,也能够是数据表对应的实体类信息 Object principal=userName; //2).credentials 密码 Object credentials=null; //这里使用加盐密码 if("admin".equals(userName)) { credentials="c41d7c66e1b8404545aa3a0ece2006ac"; } //3).realmName 使用当前realName便可 String realmName=this.getName(); //4).盐值(原始密码一致,可是经过加盐加密以后的字符串会不同,提升安全性) ByteSource credentialsSalt=ByteSource.Util.bytes(userName); SimpleAuthenticationInfo info=new SimpleAuthenticationInfo(principal, credentials, credentialsSalt, realmName); return info; }
Ok,如今只有用户名为admin,密码为123的才能够登陆成功
若是存在多个Realm,咱们应该如何配置呢?
1.这个时候Spring的Shiro整合文件就须要先配置多个Realm,这里咱们配两个:
<bean id="jdbcRealm" class="cn.lynu.realms.ShiroRealm"> <!-- 设置加密方式和加密次数 --> <property name="credentialsMatcher"> <bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher"> <property name="hashAlgorithmName" value="MD5"></property> <property name="hashIterations" value="1024"></property> </bean> </property> </bean> <!--第二个realm --> <bean id="secondRealm" class="cn.lynu.realms.ShiroRealm2"> <!-- 设置加密方式和加密次数 --> <property name="credentialsMatcher"> <bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher"> <property name="hashAlgorithmName" value="SHA1"></property> <property name="hashIterations" value="1024"></property> </bean> </property> </bean>
一个id为jdbcRealm,另外一个id为secondRealm
2.修改SecurityManager,以前使用一个realm,因此就直接将realm配置在SecurityManager里面了,这里咱们使用realms属性替代:
<!-- 1. 配置 SecurityManager安全管理器 --> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="cacheManager" ref="cacheManager"/> <property name="sessionMode" value="native"/> <!-- 配置单个realm <property name="realm" ref="jdbcRealm"/> --> <!--配置多个realm --> <property name="authenticator" ref="authenticator"></property> <property name="realms"> <list> <ref bean="jdbcRealm"/> <ref bean="secondRealm"/> </list> </property> </property> </bean>
注意看这个 realms 值使用的是list,因此到验证的时候顺序就根据在这里配置的realm前后顺序进行验证
配置后以后,咱们再来看看这个ShiroRealm2如何写的(其实就是根据ShiroRealm改过来的,改成使用SHA1加密方式)
package cn.lynu.realms; 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.authc.UsernamePasswordToken; import org.apache.shiro.crypto.hash.SimpleHash; import org.apache.shiro.realm.AuthenticatingRealm; import org.apache.shiro.util.ByteSource; public class ShiroRealm2 extends AuthenticatingRealm{ @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { System.out.println("doGetAuthenticationInfo2:"+token.hashCode()); //1.将AuthenticationToken强转为UsernamePasswordToken UsernamePasswordToken uspaToken=(UsernamePasswordToken)token; //2.得到用户名 String userName=uspaToken.getUsername(); //3.查询数据库得到真实的用户名或密码(这里模拟) //3.1若用户不存在,抛出UnknownAccountException if("unknown".equals(userName)) { throw new UnknownAccountException("用户不存在"); } //3.2根据用户信息抛出其余信息(这里使用被锁定,抛出LockedAccountException) if("lock".equals(userName)){ throw new LockedAccountException("用户被锁定"); } //4.根据用户信息构建AuthenticationInfo,咱们经常使用其子类: //1).principal 用户实体信息 能够是userName,也能够是数据表对应的实体类信息 Object principal=userName; //2).credentials 密码 Object credentials=null; //这里使用加盐密码 if("admin".equals(userName)) { credentials="49d9fbf007fd95343492e607aa34455eeb062b26"; } //3).realmName 使用当前realName便可 String realmName=this.getName(); //4).盐值(原始密码一致,可是经过加盐加密以后的字符串会不同,提升安全性) ByteSource credentialsSalt=ByteSource.Util.bytes(userName); SimpleAuthenticationInfo info=new SimpleAuthenticationInfo(principal, credentials, credentialsSalt, realmName); return info; } public static void main(String[] args) { //加密方式 String algorithmName="SHA1"; //密码 Object credentials="123"; //盐值(通常将一个惟一值做为盐值) Object salt=ByteSource.Util.bytes("admin"); //加密次数 int hashIterations=1024; SimpleHash simpleHash = new SimpleHash(algorithmName, credentials, salt, hashIterations); System.out.println(simpleHash); } }
为了区分,在这两个Realm的开始都打印一句: System.out.println("doGetAuthenticationInfo1:"+token.hashCode()); System.out.println("doGetAuthenticationInfo2:"+token.hashCode()); 用于区分不一样的realm
运行以后会发现先打印的是doGetAuthenticationInfo1 后打印doGetAuthenticationInfo2,这与咱们在realms属性中配置的顺序有关
多个Realm运行的前后顺序咱们已经知道了与realms属性有关,可是多个Realm用于验证,以哪一个为主呢?
<!--配置多realm运行策略 --> <bean id="authenticator" class="org.apache.shiro.authc.pam.ModularRealmAuthenticator"> <property name="authenticationStrategy"> <bean class="org.apache.shiro.authc.pam.AtLeastOneSuccessfulStrategy"></bean> </property> </bean>
并加入进 securityManager 中管理
<property name="authenticator" ref="authenticator"></property>
咱们其实在ModularRealmAuthenticator中配置了authenticationStrategy(验证策略)为AtLeastOneSuccessfulStrategy(只要有一个Realm验证成功便可。这其实也是默认的验证策略)。验证策略还有:
这些验证策略都是AuthenticationStrategy 接口的实现。咱们能够修改验证策略来知足咱们的需求
有的时候咱们须要给用户分配角色,用于区分不一样角色的用户均可以作什么操做。这个时候使用的仍是自定义的Realm,以前咱们在验证的时候是将自定义的realm继承AuthenticatingRealm类(实现了realm接口),而咱们受权是将自定义的realm继承AuthorizingRealm,AuthorizingRealm类是AuthenticatingRealm类的子类。验证使用的是重写AuthenticatingRealm的doGetAuthenticationInfo方法,而AuthorizingRealm类由于是子类也有这个方法,咱们也可使用这个方法完成验证,而受权的方法是doGetAuthorizationInfo方法,咱们须要重写它。这两个类和方法都比较接近,注意区分。
public class ShiroRealm extends AuthorizingRealm{ //用于验证的方法 @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { System.out.println("doGetAuthenticationInfo1:"+token.hashCode()); //1.将AuthenticationToken强转为UsernamePasswordToken UsernamePasswordToken uspaToken=(UsernamePasswordToken)token; //2.得到用户名 String userName=uspaToken.getUsername(); //3.查询数据库得到真实的用户名或密码(这里模拟) //3.1若用户不存在,抛出UnknownAccountException if("unknown".equals(userName)) { throw new UnknownAccountException("用户不存在"); } //3.2根据用户信息抛出其余信息(这里使用被锁定,抛出LockedAccountException) if("lock".equals(userName)){ throw new LockedAccountException("用户被锁定"); } //4.根据用户信息构建AuthenticationInfo,咱们经常使用其子类: //1).principal 用户实体信息 能够是userName,也能够是数据表对应的实体类信息 Object principal=userName; //2).credentials 密码 Object credentials=null; //这里使用加盐密码 if("admin".equals(userName)) { credentials="c41d7c66e1b8404545aa3a0ece2006ac"; } if("user".equals(userName)) { credentials="2bbffae8c52dd2532dfe629cecfb2c85"; } //3).realmName 使用当前realName便可 String realmName=this.getName(); //4).盐值(原始密码一致,可是经过加盐加密以后的字符串会不同,提升安全性) ByteSource credentialsSalt=ByteSource.Util.bytes(userName); SimpleAuthenticationInfo info=new SimpleAuthenticationInfo(principal, credentials, credentialsSalt, realmName); return info; } //用于受权的方法 @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { //1. 从 PrincipalCollection 中来获取登陆用户的信息 Object principal = principals.getPrimaryPrincipal(); //2. 利用登陆的用户的信息来用户当前用户的角色或权限(可能须要查询数据库) Set<String> roles = new HashSet<>(); roles.add("user"); if("admin".equals(principal)){ roles.add("admin"); } //3. 建立 SimpleAuthorizationInfo, 并设置其 roles 属性. SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(roles); //4. 返回 SimpleAuthorizationInfo 对象. return info; } }
doGetAuthorizationInfo方法中默认给全部用户赋予的是user的角色,而若是用户名为admin的用户还会额外赋予admin角色。别忘了在Spring的Shiro的配置文件中配置这个自定义的realm
user和admin角色均可以作什么呢?
咱们在Spring与Shiro的整合配置文件中
/user.jsp=authc,roles[user]
/admin.jsp=authc,roles[admin]
user.jsp只有在认证和拥有user角色的状况下才能够访问,而admin.jsp在认证和拥有admin角色的状况下才能够访问
如今的user.jsp和admin.jsp只有在登陆以后并拥有对应权限的用户在能够访问。
Shiro提供了一套标签供咱们在JSP页面进行权限控制,首先在页面导入Shiro标签库
<%-- 使用Shiro标签 --%> <%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
guest标签:用户没有身份验证时显示对应信息,及游客能够看到的信息
<shiro:guest> 欢迎游客访问,<a href="login.jsp">登陆</a> </shiro:guest>
user标签:用户已经认证/记住我登陆以后显示的信息
<shiro:user> 欢迎[<shiro:prinicpal/>]登陆,<a href="logout">退出</a> </shiro:user>
authenticated标签:用户已经身份验证经过,即经过Subject.logon登陆成功,而不是经过记住我登陆,显示的信息
<shiro:authenticated> 用户[<shiro:principal/>]已经身份验证经过 </shiro:authenticated>
notAuthenticated标签:用户未进行身份认证,即没经过Subject.login进行登陆,记住我自动登陆也属于为未通过身份验证。
<shiro:notAuthenticated> 未身份验证(包括记住我) <shiro:notAuthenticated>
principal标签:显示用户身份信息.即调用了Subject.getPrincipal()得到PrimaryPrincipal。
<shiro:principaol/>
hasRole标签:若是当前Subject有角色将显示内容
<shiro:hasRole name="admin"> 用户[<shiro:principal/>]拥有admin角色<be> </shiro:hasRole>
hasAnyRoles标签:若是当前Subject有任意一个角色(或的关系)将显示内容
<shiro:hasAnyRoles name="admin,user"> 用户[<shiro:principal/>]拥有角色admin或user<br> </shiro:hasAnyRoles>
lacksRole标签:若是当前subject没有对应角色将显示的内容
<shiro:lacksRole name="admin"> 用户[<shiro:principal/>]没有角黑色admin<br> </shiro:lacksRole>
hasPermission标签: 若是当前Subject有权限则显示内容
<shiro:hasPermission name="user:create"> 用户[<shiro:principal/>]拥有权限user:create<br> </shiro:hasPermission>
lacksPermission标签:若是当前Subject没有权限将显示内容
<shiro:lacksPermission name="user:create"> 用户[<shiro:principal/>]没有权限user:create </shiro:lacksPermission>
@RequiresAuthentication:表示Subject已经经过login进行了身份认证;即Subject.isAuthenticated()返回true
@RequiresUser:表示当前Subject已经经过身份验证或记住我登陆
@RequiresGuest:表示是当前Subject没有身份认证或记住我登陆过,便是游客身份
@RequiresRoles(values={"admin","user"},logical=Logical.OR):表示当前Subject须要角色admin或user
我在Service层配置一个方法只有拥有admin在可运行:
@RequiresRoles(value={"admin"}) public void testMethod(){ System.out.println("Now Date:"+new Date()); }
这样只有拥有admin角色的用户调用这个方法才会打印日期,其余角色的用户调用这个方法不会成功,会抛一个异常:
咱们能够根据这个异常作一个异常处理:只要出现这个异常就转到一个页面,提示一些东西
可是我在测试的时候遇到一些问题:若是使用注解标注Service那么Shiro就失效了,只有用XML中声明式的配置Service才能够?
Shiro提供了完整的会话管理,它不依赖于底层容器(如web容器tomcat),无论JavaSE仍是JavaEE均可以使用,提供了会话管理,会话监听,失效/过时支持等等
会话相关的API
这里咱们完成一个操做:在Controller层往Session中放入一个名为userName的lz值,并在Service层取出这个userName值,不要将HttpSession传入给Service层避免代码侵入,使用Shiro的Session得到这个值
Controller:
@RequestMapping("/testShiroAnnotation") public String testShiroAnnotation(HttpSession session){ session.setAttribute("userName", "lz"); shiroService.testMethod(); return "redirect:/list.jsp"; }
Service:
public void testMethod(){ Session session = SecurityUtils.getSubject().getSession(); Object val = session.getAttribute("userName"); System.out.println("Service SessionVal: " + val); }
Shiro 提供了记住我(RememberMe)的功能,好比访问如淘宝 等一些网站时,关闭了浏览器,下次再打开时仍是能记住你是谁, 下次访问时无需再登陆便可访问,基本流程以下:
咱们以前其实一直使用了rememberMe功能:
//把用户名和密码封装为UsernamePasswordToken UsernamePasswordToken token = new UsernamePasswordToken(userName, password); //Remember Me 操做 token.setRememberMe(true);
咱们能够在前端加一个checkbox用于判断是否须要rememberMe,若是勾选,这个设置setRememberMe 为true
由于rememberMe底层使用的是Cookies,咱们能够在Spring整合Shiro的配置文件的securityManager配置rememberMeManager.cookie.maxAge
<!--设置remember Me 的Cookie时间 单位秒 --> <property name="rememberMeManager.cookie.maxAge" value="1800"></property>