Shiro學習之路 Hello Shiro(一)

    因爲最近本身的項目須要加入權限的功能因此使用Shiro整合Spring mvc來實現。java

Shiro簡介

Shiro是一個擁有許多綜合性的程序安全框架。有很是方便的身份認證、受權、企業會話管理和加密。web

Shiro能夠爲咱們作什麼:算法

    驗證用戶來覈實他們的身份。spring

    對用戶執行訪問控制,如:apache

        判斷用戶是否被分配一個肯定的安全角色。瀏覽器

        判斷用戶是否被容許作某事。緩存

    在任何環境下使用Session API,即便沒有Web或EJB容器。安全

    在身份驗證、訪問控制期間、會話的生命週期,對事件做出反應。session

    啓用單點登陸功能。。。。多線程

Shiro 把 Shiro 開發團隊稱爲「應用程序的四大基石」——身份驗證,受權,會話管理和加密做爲其目標。

    Authentication:有時也簡稱爲「登陸」,這是一個證實用戶是他們所說的他們是誰的行爲。

    Authorization:訪問控制的過程,也就是決定「誰」去訪問「什麼」。 

    Session Management:管理用戶特定的會話,即便在非 Web 或 EJB 應用程序。

    Cryptography:經過使用加密算法保持數據安全同時易於使用。

也提供了額外的功能來支持和增強在不一樣環境下所關注的方面,尤爲是如下這些:

    Web Support:Shiro 的 web 支持的 API 可以輕鬆地幫助保護 Web 應用程序。

    Caching:緩存是 Apache Shiro 中的第一層公民,來確保安全操做快速而又高效。

    Concurrency:Apache Shiro 利用它的併發特性來支持多線程應用程序。

    Testing:測試支持的存在來幫助你編寫單元測試和集成測試,並確保你的可以如預期的同樣安全。

    "Run As":一個容許用戶假設爲另外一個用戶身份(若是容許)的功能,有時候在管理腳本頗有用。

    "Remember Me":在會話中記住用戶的身份,因此他們只須要在強制時候登陸。

shiro配置

Spring Mvc 整合 Shiro所需jar

pom.xml

<!-- https://mvnrepository.com/artifact/org.apache.shiro/shiro-core -->
<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-core</artifactId>
    <version>1.2.3</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.shiro/shiro-web -->
<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-web</artifactId>
    <version>1.2.3</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.shiro/shiro-spring -->
<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-spring</artifactId>
    <version>1.2.3</version>
</dependency>

web.xml配置文件

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:config/shiro.xml</param-value>
</context-param>
<!-- 配置Shiro過濾器,先讓Shiro過濾系統接收到的請求 -->
<!-- 這裏filter-name必須對應applicationContext.xml中定義的<bean id="shiroFilter"/> -->
<!-- 使用[/*]匹配全部請求,保證全部的可控請求都通過Shiro的過濾 -->
<!-- 一般會將此filter-mapping放置到最前面(即其餘filter-mapping前面),以保證它是過濾器鏈中第一個起做用的 -->
<filter>
    <filter-name>shiroFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    <!--<init-param>
        &lt;!&ndash; 該值缺省爲false,表示生命週期由SpringApplicationContext管理,設置爲true則表示由ServletContainer管理 &ndash;&gt;
        <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>

shiro.xml配置文件(此配置必須在web.xml配置以前引用)

<!-- 繼承自AuthorizingRealm的自定義Realm,即指定Shiro驗證用戶登陸的類爲自定義的ShiroDbRealm.java -->
<bean id="myRealm" class="com.zi.realm.MyRealm"/>

<!-- Shiro默認會使用Servlet容器的Session,可經過sessionMode屬性來指定使用Shiro原生Session -->
<!-- 即<property name="sessionMode" value="native"/>,詳細說明見官方文檔 -->
<!-- 這裏主要是設置自定義的單Realm應用,如有多個Realm,可以使用'realms'屬性代替 -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
    <property name="realm" ref="myRealm"/>
</bean>

<!-- Shiro主過濾器自己功能十分強大,其強大之處就在於它支持任何基於URL路徑表達式的、自定義的過濾器的執行 -->
<!-- Web應用中,Shiro可控制的Web請求必須通過Shiro主過濾器的攔截,Shiro對基於Spring的Web應用提供了完美的支持 -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
    <!-- Shiro的核心安全接口,這個屬性是必須的 -->
    <property name="securityManager" ref="securityManager"/>
    <!-- 要求登陸時的連接(可根據項目的URL進行替換),非必須的屬性,默認會自動尋找Web工程根目錄下的"/login.jsp"頁面 -->
    <property name="loginUrl" value="/"/>
    <!-- 登陸成功後要跳轉的鏈接(本例中此屬性用不到,由於登陸成功後的處理邏輯在LoginController裏硬編碼爲main.jsp了) -->
    <!-- <property name="successUrl" value="/system/main"/> -->
    <!-- 用戶訪問未對其受權的資源時,所顯示的鏈接 -->
    <!-- 若想更明顯的測試此屬性能夠修改它的值,如unauthor.jsp,而後用[玄玉]登陸後訪問/admin/listUser.jsp就看見瀏覽器會顯示unauthor.jsp -->
    <property name="unauthorizedUrl" value="/WEB-INF/index.jsp"/>
    <!-- Shiro鏈接約束配置,即過濾鏈的定義 -->
    <!-- 此處可配合個人這篇文章來理解各個過濾連的做用http://blog.csdn.net/jadyer/article/details/12172839 -->
    <!-- 下面value值的第一個'/'表明的路徑是相對於HttpServletRequest.getContextPath()的值來的 -->
    <!-- anon:它對應的過濾器裏面是空的,什麼都沒作,這裏.do和.jsp後面的*表示參數,比方說login.jsp?main這種 -->
    <!-- authc:該過濾器下的頁面必須驗證後才能訪問,它是Shiro內置的一個攔截器org.apache.shiro.web.filter.authc.FormAuthenticationFilter -->
    <property name="filterChainDefinitions">
        <value>
            /jsp/login=anon
            /jsp/index=authc
            <!--/jsp/index=authc,perms[admin:manage]-->
        </value>
    </property>
</bean>

下章講解在使用過程當中遇到的問題和使用過程。

相關文章
相關標籤/搜索