[Shiro]權限控制

前言

Apache Shiro是Java的一個安全框架。目前,使用Apache Shiro的人越來越多,因爲它相當簡單,Shiro可以非常容易的開發出足夠好的應用,其不僅可以用在JavaSE環境,也可以用在JavaEE環境。Shiro可以幫助我們完成:認證、授權、加密、會話管理、與Web集成、緩存等。


準備工作

  1. log4j-1.2.16.jar
  2. shiro-all-1.3.2.jar
  3. slf4j-api-1.6.1.jar
  4. slf4j-log4j12-1.6.1.jar

下面我們來開始配置Shiro

1、在web.xml中配置filter

<filter>
    <filter-name>shiroFilter</filter-name>
    <filter-class>
        org.springframework.web.filter.DelegatingFilterProxy
    </filter-class>
    <async-supported>true</async-supported>
    <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>

2、配置securityManager

<bean id="securityManager" class="org.apache.shiro.
    web.mgt.DefaultWebSecurityManager">
    <!-- <property name="cacheManager" ref="cacheManager"/> -->
    <property name="realm" ref="jdbcRealm"/>
</bean>

3、配置緩存ehcache(可選)

<!-- 
    需要加入 ehcache-core-2.5.2.jar及ehcache.xml   
-->
<!-- 
<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
    <property name="cacheManagerConfigFile" value="classpath:ehcache.xml"/>
</bean> 
-->

4、配置Realm,同時配置加密器credentialsMatcher(加密器可選)

<!-- 
    自定義Realm,同時配置加密器credentialsMatcher
-->
<bean id="credentialsMatcher"
       class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
    <!-- 採用MD5加密 -->
    <property name="hashAlgorithmName" value="MD5" />
    <!-- 加密次數 -->
    <property name="hashIterations" value="2" />
</bean>
<bean id="jdbcRealm" class="test.jia.com.shiro.ShiroRealm">
    <property name="credentialsMatcher" ref="credentialsMatcher" />
</bean>

5、配置lifecycleBeanPostProcessor,可以自動的調用Spring IOC 容器中 Shiro Bean 的生命週期方法

<!--
    可以自動的調用Spring IOC 容器中 Shiro Bean 的生命週期方法
-->
<bean id="lifecycleBeanPostProcessor" 
    class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>

6、啓用IOC容器中 Shiro 的註解

<!--
    啓用IOC容器中 Shiro 的註解,但必須配置了lifecycleBeanPostProcessor之後纔可使用。
    注意:這裏需要修改spring-shiro.xml
    需要將
        <bean class="org.springframework.aop.framework.
                autoproxy.DefaultAdvisorAutoProxyCreator"
              depends-on="lifecycleBeanPostProcessor"/> 
        改爲
            <aop:config proxy-target-class="true"></aop:config>
    不然shiro的註解不起作用!
-->
<!-- 
    <bean class="org.springframework.aop.framework.
            autoproxy.DefaultAdvisorAutoProxyCreator"
        depends-on="lifecycleBeanPostProcessor"/> 
-->
<aop:config proxy-target-class="true"></aop:config>
<bean class="org.apache.shiro.spring.security.
        interceptor.AuthorizationAttributeSourceAdvisor">
    <property name="securityManager" ref="securityManager"/>
</bean>

7、配置ShiroFilter
注意: id必須和web.xml中的ShiroFilter的name一致

<!--
    配置ShiroFilter 
    注意: id必須和web.xml中的ShiroFilter的name一致
-->
<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" />
    <!--
        配置哪些頁面需要授權
        anon : 可以被匿名訪問
        authc: 必須被認證(即登陸後)纔可以被訪問
        logout:登出
        注 : 
            1、filterChainDefinitions沒有被覆蓋到的路徑可以直接訪問到
            2、路徑採用優先被匹配的原則
            例如:/login.jsp = anon
                        /** = authc
                        /list.jsp = anon

                        那麼list.jsp的權限應該爲authc,而不是anon,
                        因爲/**在/list.jsp的前面,/**優先被匹配
    -->
    <property name="filterChainDefinitions">
        <value>
            /login.jsp = anon
            /shiro/login = anon
            /shiro/logout = logout
            /LoginController/testRedis = anon
            /** = authc
            <!--
                /*.js=anon
                /test/*=anon
                /test/**=anon
            -->
        </value>
    </property>
</bean>

spring-shiro.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"

    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
</beans>

8、實現ShiroRealm

/**
 * 實現ShiroRealm
 */
public class ShiroRealm extends AuthorizingRealm { //AuthenticatingRealm{


    /**
     * 授權,需要繼承AuthorizingRealm
     * 
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection 
            principalCollection) {
        System.out.println("doGetAuthorizationInfo");
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        info.addRole("user");
        info.addStringPermission("user:delete");
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        return info;
    }



    /**
     * 登錄權限驗證  需要繼承AuthenticatingRealm
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(
            AuthenticationToken token) throws AuthenticationException {
        //把AuthenticationToken轉換爲UsernamePasswordToken
        UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) token;
        //從UsernamePasswordToken 獲取 username
        String username = usernamePasswordToken.getUsername();
        String password = new String(usernamePasswordToken.getPassword());
        //查詢數據庫
        //若用戶不存在,拋出異常UnknownAccountException異常

        /**
         * 根據情況,構建AuthenticationInfo對象,並返回
         * AuthenticationInfo是個接口,通常實現類使用的是SimpleAuthenticationInfo
         * 
         * principal:認證的實體信息,可以是username,也可以是數據庫該條記錄的對象
         * credentials:數據庫獲取的密碼
         * realmName:調用當前父類裏的 getName即可
         */
        Object principal = username;
        /**
         * 此時credentials不能直接引用明文的password了,
         * 因爲shiro.xml裏配置了加密規則,
         * 登陸時會將token裏的密碼加密的返回值與credentials匹配,
         * 如果能匹配上,說明密碼正確。
         * 
         * 注:在正式環境中不會這樣寫getMd5Password(password);
         *    只需要把數據庫取出來的結果賦值給credentials即可,
         */
        Object credentials = getMd5Password(password);
        //Object credentials = 數據庫取出來的密碼;
        String realmName = getName();
//      SimpleAuthenticationInfo info = 
//              new SimpleAuthenticationInfo(principal, credentials, realmName);

        /**
         * 與上面的SimpleAuthenticationInfo不同,多了一個credentialsSalt參數
         * 由於每個人的密碼MD5加密後有可能重複,系統存在安全隱患,
         * 所以配置了更加安全的SimpleAuthenticationInfo,
         * credentialsSalt參數的作用:將一個字符串與密碼一起加密,
         * 一般使用的字符串具有唯一性,例如:用戶名
         * 這樣就實現了所有人的密碼加密後不會存在重複值。
         */
        credentials = getMd5Password2(username,password);
        ByteSource credentialsSalt = ByteSource.Util.bytes(username);
        SimpleAuthenticationInfo info = 
                new SimpleAuthenticationInfo(principal, 
                        credentials, credentialsSalt, realmName);

        System.out.println(token.hashCode());
        return info;
    }

    /**
     * 獲取password Md5加密兩次的值
     * @param password
     * @return
     */
    public static String getMd5Password(String password){
        SimpleHash simpleHash = new SimpleHash("MD5", "123456", null, 2);
        String string = simpleHash.toString();
        return string;
    }

    /**
     * 獲取username與password Md5加密兩次的值
     * @param username
     * @param password
     * @return
     */
    public static String getMd5Password2(String username,String password){
        ByteSource bytes = ByteSource.Util.bytes(username);
        SimpleHash simpleHash = new SimpleHash("MD5", "123456", bytes, 2);
        String string = simpleHash.toString();
        return string;
    }


}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章