shiro 登录不执行授权回调方法doGetAuthorizationInfo

转载自:http://blog.csdn.net/qq_31759675/article/details/72268141


登录页面跳转业务处理



SpringMVC控制器




Shiro配置文件

[html]  view plain  copy
  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.     xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"  
  3.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
  6.     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
  7.     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd  
  8.     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">  
  9.   
  10.     <!-- 1. 配置 SecurityManager! -->  
  11.     <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">  
  12.         <property name="cacheManager" ref="cacheManager" />  
  13.         <property name="authenticator" ref="authenticator" />  
  14.           
  15.         <property name="realms">  
  16.             <list>  
  17.                 <ref bean="jdbcRealm"/>  
  18.                 <!-- <ref bean="shiroRealm"/> -->  
  19.             </list>  
  20.         </property>  
  21.           
  22.         <!-- 设置登录时长 半天12个小时、-->  
  23.         <property name="rememberMeManager.cookie.maxAge" value="43200"></property>  
  24.     </bean>  
  25.   
  26.     <!-- 2. 配置 CacheManager. -->  
  27.     <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">  
  28.         <property name="cacheManagerConfigFile" value="classpath:shiro/ehcache.xml" />  
  29.     </bean>  
  30.       
  31.     <bean id="authenticator" class="org.apache.shiro.authc.pam.ModularRealmAuthenticator">  
  32.         <property name="authenticationStrategy">  
  33.             <bean class="org.apache.shiro.authc.pam.AtLeastOneSuccessfulStrategy"></bean>  
  34.         </property>  
  35.     </bean>  
  36.   
  37.     <!-- 3. 配置 多Realm验证:MD5 SHA1... 3.1 直接配置实现了 org.apache.shiro.realm.Realm 接口的 bean -->  
  38.     <bean id="jdbcRealm" class="com.vnasi.realms.ShiroRealm">  
  39.         <property name="credentialsMatcher">  
  40.             <bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">  
  41.                 <property name="hashAlgorithmName" value="MD5" />  
  42.                 <property name="hashIterations" value="1024" />  
  43.             </bean>  
  44.         </property>  
  45.     </bean>  
  46.     <!-- 暂时没用上 -->  
  47.     <!-- <bean id="shiroRealm" class="com.vnasi.realms.SecondRealm">  
  48.         <property name="credentialsMatcher">  
  49.             <bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">  
  50.                 <property name="hashAlgorithmName" value="SHA1" />  
  51.                 <property name="hashIterations" value="1024" />  
  52.             </bean>  
  53.         </property>  
  54.     </bean> -->  
  55.   
  56.     <!-- 4. 配置 LifecycleBeanPostProcessor. 可以自定的来调用配置在 Spring IOC 容器中 shiro bean 的生命周期方法. -->  
  57.     <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />  
  58.   
  59.     <!-- 5. 启用 IOC 容器中使用 shiro 的注解. 但必须在配置了 LifecycleBeanPostProcessor 之后才可以使用. -->  
  60.     <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor" />  
  61.     <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">  
  62.         <property name="securityManager" ref="securityManager" />  
  63.     </bean>  
  64.   
  65.     <!-- 6. 配置 ShiroFilter. -->  
  66.      <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">  
  67.         <property name="securityManager" ref="securityManager"/>  
  68.         <!-- <property name="loginUrl" value="/path/toLogin"/>  
  69.         <property name="successUrl" value="/foreground/index"/> -->  
  70.         <property name="unauthorizedUrl" value="/path/toUnAuthorized"/>  
  71.                 
  72.         <!-- 静态资源map -->  
  73.         <property name="filterChainDefinitionMap" ref="filterChainDefinitionMap" />  
  74.     </bean>  
  75.       
  76.     <!-- 采用从数据表中初始化静态资源map  配置一个 bean, 该 bean 实际上是一个 Map. 通过实例工厂方法的方式-->  
  77.     <bean id="filterChainDefinitionMap" factory-bean="filterChainDefinitionMapBuilder" factory-method="buildFilterChainDefinitionMap"></bean>  
  78.     <bean id="filterChainDefinitionMapBuilder" class="com.vnasi.factory.FilterChainDefinitionMapBuilder"/>  
  79.   
  80. </beans>  

静态资源map,就是shiro配置文件中的filterChainDefinitions内容动态模拟出来,实际查数据库



自定义ShiroRealm 继承AuthorizingRealm 实现授权和信息认证

[html]  view plain  copy
  1. <span style="font-size:18px;">public class ShiroRealm extends AuthorizingRealm {  
  2.   
  3.     @Autowired  
  4.     private UserService service;  
  5.   
  6.     // 认证回调函数  
  7.         @Override  
  8.         protected AuthenticationInfo doGetAuthenticationInfo(  
  9.                 AuthenticationToken token) throws AuthenticationException {  
  10.             //1.将AuthenticationToken强制转换为UsernamePasswordToken从而得到userName  
  11.             UsernamePasswordToken upToken = (UsernamePasswordToken)token;  
  12.               
  13.             //2.根据转换过来的UsernamePasswordToken对象得到userName  
  14.             String username = upToken.getUsername();  
  15.               
  16.             //3.根据userName从数据库查出来的对应的记录  
  17.             VnasiUser user = service.findUserByUserName(username);  
  18.             Object principal = user.getUserId();              
  19.             Object credentials = user.getPassword();  
  20.               
  21.             //4.当用户不存在时  
  22.             if(!principal.equals(username)){  
  23.                 throw new UnknownAccountException("该用户不存在!");  
  24.             }  
  25.             //5.当用户被锁定时  
  26.             if(!principal.equals(username)){  
  27.                 throw new LockedAccountException("该用户已被锁定!");  
  28.             }  
  29.               
  30.             //MD5盐值加密后的密码  
  31.             String hashAlgorithmName = "MD5";  
  32.             Object salt = ByteSource.Util.bytes(principal);  
  33.             int hashIterations = 1024;  
  34.               
  35.             Object result = new SimpleHash(hashAlgorithmName,credentials,salt,hashIterations);  
  36.               
  37.             //credentials密码是通过用户名和密码进行盐值加密得出的  
  38.             if(principal.equals(username)){  
  39.                 credentials = result;  
  40.             }  
  41.             //6.3 盐值加密  
  42.             ByteSource credentialsSalt = ByteSource.Util.bytes(username);  
  43.               
  44.             //6.4当前 realm 对象的 name. 调用父类的 getName()   
  45.             String realm = getName();  
  46.               
  47.             //7.通过这个简单验证对象得到验证信息  
  48.             SimpleAuthenticationInfo info = null; //new SimpleAuthenticationInfo(principal, credentials, realmName);  
  49.             info = new SimpleAuthenticationInfo(principal, credentials,credentialsSalt,realm);  
  50.             return info;  
  51.         }  
  52.       
  53.     // 授权回调函数  
  54.     @Override  
  55.     protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {  
  56.         // 1. 从 PrincipalCollection 中来获取登录用户的信息  
  57.         String userName = (String) principals.getPrimaryPrincipal();  
  58.   
  59.         // 2. 利用登录的用户的信息来获取当前用户的角色或权限  
  60.         VnasiUser user = service.findUserByUserName(userName);  
  61.         // 2.1  
  62.         // 无论是普通user角色还是amdin系统角色都给上一个user(1)用户角色,要是admin就给普通用户再加上一个admin(2)角色  
  63.         Set<String> roles = new HashSet<String>();  
  64.         roles.add("user");  
  65.         if (user.getUserId().equals(userName)) {  
  66.             roles.add("admin");  
  67.         }  
  68.   
  69.         // 3. 创建 SimpleAuthorizationInfo, 并设置其 reles 属性.  
  70.         SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(roles);  
  71.   
  72.         // 4. 返回 SimpleAuthorizationInfo 对象 .  
  73.         return info;  
  74.     }</span>  


以上都是大部分完整代码,跑起来也没有任何问题,shiro相关配置也是从Copy上一个项目的,上一个项目是可以顺利运行的。搞了半天测试直接访问首页的时候就是愣是直接进去了,后台代码找了又找都没找出名堂,网上也找了很多,说是在SpringMVC配置文件加上

<aop:config proxy-target-class="true" />

发现也没有什么卵用。后台实在找不出又跑去登录成功之后跳转的前台页面找找,果然,发现没有加上Shiro的标签库和权限标签

<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>

<shiro:hasRole name="user"></shiro:hasRole> 

加上就行了,记录自己的小问题,希望可以帮到一些需要的人


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值