zoukankan      html  css  js  c++  java
  • Shiro 自定义角色 认证

    转载,原博文的地址在:https://ailongni.iteye.com/blog/2086022

    由于Shiro filterChainDefinitions中 roles默认是and,
    /** = user,roles[system,general]
    比如:roles[system,general] ,表示同时需要“system”和“general” 2个角色才通过认证
    所以需要自定义 继承 AuthorizationFilter

    public class RolesAuthorizationFilter extends AuthorizationFilter{
    
        @Override
        protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue)
                throws Exception {
            Subject subject = getSubject(request, response); 
            String[] rolesArray = (String[]) mappedValue; 
    
            if (rolesArray == null || rolesArray.length == 0) { 
                //no roles specified, so nothing to check - allow access. 
                return true; 
            } 
    
            for(int i=0;i<rolesArray.length;i++){  
                if(subject.hasRole(rolesArray[i])){  
                    return true;  
                }  
            }  
            return false;  
        }
    
    }

    shiro过滤器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="/success" />
            <property name="filters">
                <map>
                    <entry key="anyRoles" value-ref="anyRoles"/>
                </map>
            </property>
            <property name="filterChainDefinitions">
                <value>
                    /login = authc
                    /login/logout = anon
                    / = anon
                    /XXX/** = user,anyRoles[system,general]
                                    /TTT = role[system]
                    /** = user
                </value>
            </property>
        </bean>
        
        <!--自定义的Roles Filter-->
        <bean id="anyRoles" class="com.jianfei.p.web.common.RolesAuthorizationFilter" />

    注意:/XXX/** = user,anyRoles[system,general], 注意红色的"anyRoles"一定要和 

      <entry key="anyRoles" value-ref="anyRoles"/> key一样就行,否则过滤器不起作用

  • 相关阅读:
    MySQL的事务和视图
    MySQL中的常用函数
    高级查询(一)
    街道管理系统
    深入.NET平台和C#编程的错题
    appium python下的API方法
    Charles 使用教程
    appium,iOS下,ios_predicate等定位方式
    Linux -常用命令
    python-列表以及相关函数认识
  • 原文地址:https://www.cnblogs.com/chenmz1995/p/10331035.html
Copyright © 2011-2022 走看看