Spring Security教程(一):初識Spring Security

1、概要

Spring Security,這是一種基於 Spring AOP Servlet 過濾器安全框架它提供全面的安全性解決方案,同時在 Web 請求級方法調用級處理身份確認和受權。如下是百科裏對於Spring Security的介紹。java

Spring Security是一個可以爲基於Spring的企業應用系統提供聲明式的安全訪問控制解決方案的安全框架。它提供了一組能夠在Spring應用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反轉Inversion of Control ,DI:Dependency Injection 依賴注入)和AOP(面向切面編程)功能,爲應用系統提供聲明式的安全訪問控制功能,減小了爲企業系統安全控制編寫大量重複代碼的工做。web

經過介紹也能夠看出,spring security並不像apache shiro那樣支持javase和ejb環境。spring

目前最新版本的Spring Security爲4.2.2,可是我這裏用了穩定版本3.1.3。下面例子爲一個簡單的Spring Security配置應用。apache

2、新建一個web maven項目

新建好項目以後在webapp下添加了兩個jsp文件,adminPage.jsp和index.jsp。其中adminPage.jsp只有那些擁有ROLE_ADMIN,ROLE_USER其中一種權限的用戶才能訪問,而index.jsp只容許那些擁有ROLE_USER權限的用戶才能訪問。編程

3、配置過濾器

爲了在項目中 使用Spring Security控制權限,首先要在web.xml中配置過濾器,這樣咱們就能夠控制對這個項目的每一個請求了。
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">
    <display-name>SpringSecurity</display-name>
    
    <!-- 加載配置文件 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/config/applicationContext*.xml</param-value>
    </context-param>
    <!-- spring security 的過濾器配置 -->
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
 
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
</web-app>

4、Spring Security的配置

在WEB-INF/config/下新建applicationContext.xml,配置以下瀏覽器

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
                        http://www.springframework.org/schema/security
                        http://www.springframework.org/schema/security/spring-security.xsd">
    <http auto-config='true'>
        <intercept-url pattern="/adminPage.jsp" access="ROLE_ADMIN" />
        <intercept-url pattern="/**" access="ROLE_USER" />
    </http>
    <authentication-manager>
        <authentication-provider>
            <user-service>
                <user name="admin" password="123" authorities="ROLE_USER, ROLE_ADMIN" />
                <user name="user" password="123" authorities="ROLE_USER" />
            </user-service>
        </authentication-provider>
    </authentication-manager>
</beans:beans>

說明:tomcat

  1. 文件的頭部爲,聲明在xml中使用Spring Security提供的命名空間。
  2. http部分配置如何攔截用戶請求。auto-config='true'將自動配置幾種經常使用的權限控制機制,包括form, anonymous, rememberMe
  3. 利用intercept-url來判斷用戶須要具備何種權限才能訪問對應的url資源,能夠在pattern中指定一個特定的url資源,也可使用通配符指定一組相似的url資源。例子中定義的兩個intercepter-url,第一個用來控制對/admin.jsp的訪問,第二個使用了通配符/**,說明它將控制對系統中全部url資源的訪問。
  4. 其實在實際使用中,Spring Security採用的是一種就近原則,就是說當用戶訪問的url資源知足多個intercepter-url時,系統將使用第一個符合條件的intercept-url進行權限控制。在咱們這個例子中就是,當用戶訪問/admin.jsp時,雖然兩個intercept-url都知足要求,但由於第一個intercept-url排在上面,因此Spring Security會使用第一個intercept-url中的配置處理對/adminPage.jsp的請求,也就是說,只有那些擁有了ROLE_ADMIN權限的用戶才能訪問/adminPage.jsp。
  5. access指定的權限都是以ROLE_開頭的,實際上這與Spring Security中的Voter機制有着千絲萬縷的聯繫,只有包含了特定前綴的字符串纔會被Spring Security處理。目前來講咱們只須要記住這一點就能夠了。
  6. user-service中定義了兩個用戶,admin和user。爲了簡便起見,咱們使用明文定義了兩個用戶對應的密碼,這只是爲了當前演示的方便,以後的例子中咱們會使用Spring Security提供的加密方式,避免用戶密碼被他人竊取
  7. 最重要的部分是authorities,這裏定義了這個用戶登錄以後將會擁有的權限,它與上面intercept-url中定義的權限內容一一對應。每一個用戶能夠同時擁有多個權限,例子中的admin用戶就擁有ROLE_ADMIN和ROLE_USER兩種權限,這使得admin用戶在登錄以後能夠訪問ROLE_ADMIN和ROLE_USER容許訪問的全部資源。與之對應的是,user用戶就只擁有ROLE_USER權限,因此他只能訪問ROLE_USER容許訪問的資源,而不能訪問ROLE_ADMIN容許訪問的資源。

5、pom.xml文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
 
    <groupId>com.zmc</groupId>
    <artifactId>SpringSecurityDemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
 
    <name>SpringSecurityDemo</name>
    <url>http://maven.apache.org</url>
 
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java-version>1.7</java-version>
        <org.springframework-version>3.2.2.RELEASE</org.springframework-version>
        <org.aspectj-version>1.6.10</org.aspectj-version>
        <org.slf4j-version>1.6.1</org.slf4j-version>
    </properties>
 
    <dependencies>
        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.3</version>
        </dependency>
        <!-- Spring security -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
            <version>3.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>3.1.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>3.1.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-crypto</artifactId>
            <version>3.1.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-taglibs</artifactId>
            <version>3.1.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

這樣一個項目就構建完成了,部署到tomcat進行測試。安全

6、結果

在瀏覽器上輸入:http://localhost:8888/SpringSecurityDemo/,由於沒有登錄,因此沒法訪問index.jsp頁面,這個時候spring security就起做用了,對資源進行攔截,由於沒有符合權限的用戶登錄,因此就跳轉到登錄頁面,其中這個登錄頁面是Spring Security自動生成的,這也是auto-config=」true」起的做用之一mvc

而後輸入用戶名和密碼,成功跳轉到index.jsp頁面。app

這裏由於admin用戶有ROLE_ADMIN和ROLE_USER權限,而index.jsp頁面ROLE_USER權限便可訪問,因此admin用戶能夠成功訪問index.jsp和adminPage.jsp頁面。
下面再來測試用戶user,注意已經登錄了的話,應該重啓瀏覽器,要否則會一直記住用戶,沒法作測試。

從上圖中能夠看到,登錄用戶user,能夠訪問index.jsp頁面可是沒法訪問adminPage.jsp。這是由於user用戶只有ROLE_USER權限,而adminPage.jsp頁面須要ROLE_USER權限,因此就拒絕訪問。以上就是一個簡單的spring security配置應用。

相關文章
相關標籤/搜索