Shiro-初體驗

0) 前言

Shiro: 一個易上手,設計靈活的權限框架, 經過簡單的配置就能實現系統的安全管理.git

從系統的安全角度考慮, 你的系統至少須要實現以下功能:github

  • 未登陸的用戶訪問頁面A時跳轉至登陸頁面
  • 用戶登陸成功後會自動跳轉至頁面A或指定頁面
  • 已經登陸的用戶能夠直接訪問頁面B
  • 普通會員不能夠訪問頁面C
  • 超級會員能夠訪問頁面D

上述功能總結起來爲兩點:web

  • 所訪問頁面是否須要登陸
  • 是否有權限訪問頁面(權限是須要登陸以後根據登陸用戶獲得的)

Shiro給登陸和權限定義了兩個專有名詞, 分別是認證(authc)和受權(authz).spring

A,B,C三個頁面須要登陸用戶訪問, 其中: C頁面須要有權限的用戶才能訪問. 在Shiro中能夠說成只有認證用戶才能訪問A,B,C頁面. 只有經過受權的用戶才能訪問頁面Capache

系統的安全需求已經清晰了, 下面咱們來配置Shiro, 本例中代碼只進行基礎配置, 深刻內容後續講到.安全

因爲Spring已經一統天下, 本例中的Shiro所有基於Spring進行配置.bash

1) 引入Shiro相關依賴

Spring下的Shiro須要三個類庫:session

  • shiro-web.jar: 提供filter, session相關的類
  • shiro-core.jar: 核心包
  • shiro-spring.jar: spring整合必備
"org.apache.shiro:shiro-core:1.3.2",
"org.apache.shiro:shiro-web:1.3.2",
"org.apache.shiro:shiro-spring:1.3.2"
複製代碼

2)  Shiro配置文件

咱們進行以下配置: 除/page/n外, 全部請求都須要認證(登陸)才能夠訪問.app

beans {

    // Shiro核心配置
    shiroFilter(ShiroFilterFactoryBean) {
        securityManager = ref("securityManager")
        // 配置URL規則
        // 有請求訪問時Shiro會根據此規則找到對應的過濾器處理
        filterChainDefinitionMap = [
            "/page/n" : "anon", // /page/n不須要登陸便可訪問
            "/**": "authc" // 其他全部頁面須要認證(authc爲認證過濾器)
        ]
    }

    // 安全管理器
    securityManager(DefaultWebSecurityManager)

}
複製代碼

shiroFilter: 定義及配置shiro核心過濾器並交由Spring管理, 須要被Shiro管理的URL在訪問時都通過該過濾器處理(下面會在web.xml進行配置), Shiro是基於過濾器實現的安全框架,  原理是URL與過濾器對應, 當訪問URL時找到對應的過濾器, 在過濾器中處理認證和受權. Shiro內置了不少的過濾器, 下面介紹幾個經常使用的過濾器:框架

  • anon: 匿名過濾器, 不進行任何認證和受權的處理, 全部不須要認證和受權(全部人均可以訪問的頁面)配置該過濾器, 例如: CSS, JS, 圖片, 網站首頁等. 對應類爲org.apache.shiro.web.filter.authc.AnonymousFilter

  • authc: 表單認證過濾器, Shiro認證最核心的過濾器, 處理認證, 登陸等操做, 全部須要認證才能訪問的URL都需配置該過濾器, 後續篇幅會根據源碼講解該過濾器的實現原理及機制. 對應類爲org.apache.shiro.web.filter.authc.FormAuthenticationFilter

  • logout: 登出過濾器, 通常登出的URL須要配置爲logout, 當用戶點擊登出連接時進入該過濾器. 系統的登出操做交由Shiro處理. 對應類爲org.apache.shiro.web.filter.authc.LogoutFilter

爲方便記憶與配置,上述的過濾器都是簡寫, 在Shiro中有一個簡寫和實際過濾器類的對應關係, 具體其餘過濾器的簡寫能夠在代碼中進行查看. 在org.apache.shiro.web.filter.mgt.DefaultFilter

anon(AnonymousFilter.class),
  authc(FormAuthenticationFilter.class),
  authcBasic(BasicHttpAuthenticationFilter.class),
  logout(LogoutFilter.class),
  noSessionCreation(NoSessionCreationFilter.class),
  perms(PermissionsAuthorizationFilter.class),
  port(PortFilter.class),
  rest(HttpMethodPermissionFilter.class),
  roles(RolesAuthorizationFilter.class),
  ssl(SslFilter.class),
  user(UserFilter.class);
複製代碼

shiroFilter還能夠配置:

  • loginUrl: 登陸頁面請求地址, 默認爲/login.jsp, 當訪問請求的用戶未登陸時Shiro會重定向到該地址讓用戶進行登陸.
  • successUrl: 用戶直接訪問登陸頁, 登陸成功後跳轉至此地址. 用戶訪問頁面A時跳轉至登陸, 登陸成功後會重定向至頁面A.
  • unauthorizedUrl: 未受權頁面地址, 未受權的用戶頁面時, Shiro會重定向到該頁面提示用戶無訪問權限.
  • securityManager: Shiro安全管理器, 提供Shiro核心的安全管理邏輯, 後續篇幅細講. 此處默認聲明一下便可.

3)  Web.xml添加Shiro過濾器

上面提到了全部的URL都交由Shiro的過濾器進行處理, 所以須要在web.xml中添加shiro的過濾器

<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>
複製代碼

<filter-class>是Spring提供的代理類, 後續會講到, <filter-name>必定要和Shiro配置文件中的名稱相同才能找到對應的過濾器

4) Shiro配置文件

<!-- Spring Context Listener -->
<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
	<!-- groovy DSL -->
	<param-name>contextClass</param-name>
	<param-value>
		org.springframework.web.context.support.GroovyWebApplicationContext
	</param-value>
</context-param>
<context-param>
	<param-name>contextConfigLocation</param-name>
	<!-- Shiro configuration -->
	<param-value>classpath*:/spring-shiro.groovy</param-value>
</context-param>
複製代碼

5) 示例代碼

本例代碼使用了Spring4新增的Groovy DSL語法. 此處只是增長了Shiro配置文件. 無其餘特殊配置

至此, 一個基於Shiro控制項目認證的示例配置完成, Shiro所有采用配置. 運行項目, 訪問/page/n, 能夠正常訪問. 訪問其餘頁面會跳轉至登陸.

相關文章
相關標籤/搜索