王铁锤呀 2015-08-19 18:48 采纳率: 0%
浏览 37893

shiro 不执行授权方法 doGetAuthorizationInfo()

ShiroDbRealm.java 代码如下

public class ShiroDbRealm extends AuthorizingRealm {

    @Resource
    private UserService userService;

    /**
     * 认证回调函数,登录时调用.
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException {
        UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
        User currentUser = userService.findByUserName(token.getUsername());
        if (currentUser != null) {
            if (currentUser.getStatus()==User.STATUS_DISABLED) {
                throw new DisabledAccountException("用户已注销");
            }else if(currentUser.getStatus()==User.STATUS_NOT_ACTIVE){
                throw new DisabledAccountException("用户未激活");//这里需要编写一个用户未激活异常
            }
            return new SimpleAuthenticationInfo(currentUser.getUsername(),currentUser.getUserpwd(), "");
        }
        return null;
    }

    /**
     * 授权查询回调函数, 进行鉴权但缓存中无用户的授权信息时调用.
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(
            PrincipalCollection principals) {
        // Cache<Object, AuthenticationInfo> authenticationCache =
        // getAuthenticationCache();
        String primaryPrincipal = (String) principals.getPrimaryPrincipal();
        System.out.println("-----------*************************------------>"+ primaryPrincipal);
        List<String> roles = new ArrayList<String>();  
        List<String> permissions = new ArrayList<String>();

        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();

        User user = userService.findByUserName(primaryPrincipal);
        if(user != null){
            for (Role role : user.getRoles()) {
                roles.add(role.getName());
                for (Permission p : role.getPermissions()) {
                    permissions.add(p.getPrivilege());
                }
            }
        }else{
            throw new AuthorizationException();
        }
        //给当前用户设置角色
        info.addRoles(roles);
        //给当前用户设置权限
        info.addStringPermissions(permissions); 

        return info;
    }
}

applicationContext-shiro.xml配置

 <beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">

    <description>Shiro安全配置</description>

    <!-- Shiro's main business-tier object for web-enabled applications -->
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="realm" ref="shiroDbRealm"/>
        <property name="cacheManager" ref="shiroEhcacheManager"/>
    </bean>

    <!-- 項目自定义的Realm -->
    <bean id="shiroDbRealm" class="com.wsq.app.service.common.ShiroDbRealm">
        <!-- <property name="userService" ref="userService"/>  这里我在项目中只用了注解注入-->
    </bean>


    <!-- Shiro Filter -->
    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <!-- 这个属性是必须的 -->
        <property name="securityManager" ref="securityManager"/>
        <!-- 没有登录的用户请求需要登录的页面时自动跳转到登录页面,不是必须的属性,不输入地址的话会自动寻找项目web项目的根目录下的”/login.jsp”页面 -->
        <property name="loginUrl" value="/login"/>
        <!-- 登录成功默认跳转页面,不配置则跳转至”/”。如果登陆前点击的一个需要登录的页面,则在登录自动跳转到那个需要登录的页面。不跳转到此 -->
        <property name="successUrl" value="/"/>
        <!-- 没有权限默认跳转的页面 -->
        <property name="unauthorizedUrl" value=""/>
        <!-- 就是需要验证的地址的列表,常用的包含anon、authc、perms、roles、user、logout。 -->
        <property name="filterChainDefinitions">
            <value>
                /static/** = anon
                /login = anon
                /login/** = anon
                /logout = user
                /** = authc
            </value>
        </property>
    </bean>

    <!-- 用户授权信息Cache, 采用EhCache -->
    <bean id="shiroEhcacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
        <property name="cacheManagerConfigFile" value="classpath:resource/ehcache-shiro.xml"/>
    </bean>

    <!-- 保证实现了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>

</beans>

web.xml配置

  <!-- Shiro Security filter -->
    <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>
    </filter-mapping>

集成以后,项目可以正常启动,登陆时也可以正常调用登录验证,可就是在验证授权时,不掉用。求解答~也没分了,不好意思。

展开全部

  • 写回答

13条回答 默认 最新

  • 丁鹏程 2016-03-16 14:49
    关注

    在spring-mvc的配置文件中,添加

     <aop:config proxy-target-class="true"></aop:config>
        <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
            <property name="securityManager" ref="securityManager"/>
        </bean>
    

    启动shiro授权注解拦截方式,就可以了
    另外这个还需要aspectj的支持

     <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjweaver</artifactId>
                <version>${aspectj.version}</version>
            </dependency>
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjrt</artifactId>
                <version>${aspectj.version}</version>
            </dependency>
    
    评论
  • help12396 2018-03-29 18:07
    关注

    图片说明
    有没有加上aop切面支持,我也遇到过这个问题,看了好多帖子,发现最终问题是没有加aop,加了这个就可以授权了
    图片说明

    评论
  • JPF1024 2015-08-19 18:53
    关注

    参考一下:

     http://www.oschina.net/question/273295_105031
    
    评论
  • Mac_john 2016-01-17 19:39
    关注

    最近我也遇到这个问题,不知道楼主是否有解决,有的话还忘分享下,谢谢!

    评论
  • pthill 2016-03-23 00:18
    关注

    楼主的问题解决了吗?我也遇到了这个问题,登录后台都成功了,跳转进入时就又回到登录页面了。怎么回事呢???

    评论
  • wkewenku 2016-03-31 20:57
    关注

    /**
    * 授权查询回调函数, 进行鉴权但缓存中无用户的授权信息时调用.
    */
    清缓存

    评论
  • qq_18192973 2016-05-20 00:50
    关注

    楼主的问题解决了吗?我也遇到了这个问题,登录后台都成功了,跳转进入时就又回到登录页面了。怎么回事呢???

    评论
  • 每天进步一点点丶 2017-05-07 08:02
    关注

    我靠,没看到一个有用的回答,真心酸

    评论
  • superwc3000 2017-11-06 17:54
    关注

    这个问题我也碰到了,现在已经解决,出现这个问题的原因是 你的地址栏的地址没有改变。倒是filter不会调用。
    上代码:
    @RequestMapping(value = "doLogin", method = RequestMethod.POST)
    //@ResponseBody
    public String login(String userName,String password, HttpServletRequest request,Model model) {
    Subject subject = SecurityUtils.getSubject();
    UsernamePasswordToken token = new UsernamePasswordToken(userName,password);
    token.setRememberMe(false);
    String msg="";
    try{
    if (!subject.isAuthenticated()) {
    subject.login(token);

    }else{
    String username= (String)subject.getPrincipal();

    if(!username.equalsIgnoreCase(userName)){
    subject.logout();

    subject.login(token);

    }

    }
    return "redirect:/webpage/moduleMaintain"; --- 主要是这里要修改成这样, 不要直接返回 moduleMaintain 那个页面。 那样的话处理是forword的形式地址栏不会改变。前提是你之前的其他配置都正确。

        }catch (IncorrectCredentialsException e) {  
            msg = "Password for account " + token.getPrincipal() + " was incorrect.";  
            model.addAttribute("message", msg);  
            System.out.println(msg);  
        } catch (ExcessiveAttemptsException e) {  
            msg = "Login fault to much times";  
            model.addAttribute("message", msg);  
            System.out.println(msg);  
        } catch (LockedAccountException e) {  
            msg = "The account for username " + token.getPrincipal() + " was locked.";  
            model.addAttribute("message", msg);  
            System.out.println(msg);  
        } catch (DisabledAccountException e) {  
            msg = "The account for username " + token.getPrincipal() + " was disabled.";  
            model.addAttribute("message", msg);  
            System.out.println(msg);  
        } catch (ExpiredCredentialsException e) {  
            msg = " the account for username " + token.getPrincipal() + "  was expired.";  
            model.addAttribute("message", msg);  
            System.out.println(msg);  
        } catch (UnknownAccountException e) {  
            msg = "There is no user with username of " + token.getPrincipal();
            model.addAttribute("message", msg);  
            System.out.println(msg); 
        }
        return "login";
    }
    

    展开全部

    评论
  • tahe4033 2017-12-25 03:56
    关注
    评论
  • qq_43685931 2021-05-10 18:51
    关注

    难受一个一个都没用,试过3种授权办法,xml配置文件增加生命周期、自动创建代理,能查到的试了一遍

     

    授权方法里写了一些纯文字语句,看控制台都没有任何反应

    评论
  • rianbowJson 2016-02-29 06:47
    关注

    我也是这个问题,授权的方法不调用,之前都要调用的,不知道现在怎么不调用了,也没有异常抛出来,不知道你们解决没有

    评论
  • zihan11201120 2016-06-06 00:25
    关注

    这个是我的解决方案,真实有用,可以看考下http://www.javaweb1024.com/java/JavaWebzhongji/2016/06/06/966.html,如果有问题,可以私聊我.这是我的网站

    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部