1. 程式人生 > >springmvc+shiro認證框架配置

springmvc+shiro認證框架配置

1,在web.xml中配置fiter,如下所示

    <!-- shiro的filter -->
    <!-- shiro過慮器,DelegatingFilterProxy通過代理模式將spring容器中的bean和filter關聯起來 -->
    <filter>
        <filter-name>shiroFilter</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
        <!-- 設定true由servlet容器控制filter的生命週期 -->
        <init-param>
            <param-name>targetFilterLifecycle</param-name>
            <param-value>true</param-value>
        </init-param>
        <!-- 設定spring容器filter的bean id,如果不設定則找與filter-name一致的bean-->
        <init-param>
            <param-name>targetBeanName</param-name>
            <param-value>shiroFilter</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>shiroFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

2,上面配置了shiroFilter,那麼在spring-shiro.xml中要配置shirofilter bean

    <!-- web.xml中shiro的filter對應的bean -->
    <!-- Shiro 的Web過濾器 -->
    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <property name="securityManager" ref="securityManager" />
     <!-- loginUrl認證提交地址,如果沒有認證將會請求此地址進行認證,請求此地址將由formAuthenticationFilter進行表單認證 -->
        <property name="loginUrl" value="/login" />
        <!-- 認證成功統一跳轉到first.action,建議不配置,shiro認證成功自動到上一個請求路徑 -->
        <!--<property name="successUrl" value="/index.jsp"/>-->
        <!-- 通過unauthorizedUrl指定沒有許可權操作時跳轉頁面-->
        <!--<property name="unauthorizedUrl" value="/sysPer/noperm" />-->
        <!--自定義filter配置 -->
        <!--<property name="filters">-->
        <!--<map>-->
        <!-- 將自定義的FormAuthenticationFilter注入shiroFilter中-->
        <!--<entry key="authc" value-ref="formAuthenticationFilter" />-->
        <!--</map>-->
        <!--</property>-->

        <!-- 過慮器鏈定義,從上向下順序執行,一般將/** 放在最下邊 -->
        <property name="filterChainDefinitions">
            <value>
                <!-- 對靜態資源設定匿名訪問 -->
                /styles/easyui/** = anon
                /scripts/easyui/** = anon
                /styles/** = anon
                /images/** = anon
                <!-- 請求 logout地址,shiro去清除session-->
                /logout = logout
                <!--商品查詢需要商品查詢許可權,取消url攔截配置,使用註解授權方式 -->
                <!-- /items/queryItems.action = perms[item:query] -->
                <!--/sysuser/deleteUser = perms[user:delete]-->
                <!-- 配置記住我或認證通過可以訪問的地址 -->
                <!--/index.jsp  = user-->              
                /login = anon
                <!-- /** = authc 所有url都必須認證通過才可以訪問-->
                /** = authc
            </value>
        </property>
    </bean>

3,在spring-shiro.xml中新增securityManager自定義realm,ecacheManager的相關配置

    <!-- securityManager安全管理器 -->
    <!-- realm -->
    <bean id="customRealm" class="com.unisits.zngkpt.framework.privilegeframe.CustomeRealm">
        <!-- 將憑證匹配器設定到realm中,realm按照憑證匹配器的要求進行雜湊 -->
        <property name="credentialsMatcher" ref="credentialsMatcher"/>
    </bean>
    <!-- 憑證匹配器 -->
    <bean id="credentialsMatcher"
          class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
        <property name="hashAlgorithmName" value="md5" />
        <property name="hashIterations" value="1" />
    </bean>

    <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"></bean>

    <!-- 配額securityManager -->
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="realm" ref="customRealm" />
        <!-- 注入快取管理器 -->
        <property name="cacheManager" ref="cacheManager"/>
    </bean>
    <!-- 快取管理器 -->
    <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
        <property name="cacheManagerConfigFile" value="classpath:shiro-ehcache.xml"/>
    </bean>

3,在spring-shiro.xml中,開啟shiro的註解支援

<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>

4,在spring-shiro.xml中配置shiro認證異常的頁面

    <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        <property name="exceptionMappings">
            <props>
                <prop key="org.apache.shiro.authz.UnauthorizedException">/errorpage/refuse</prop>
            </props>
        </property>
    </bean>