1. 程式人生 > >Spring Security 使web應用更加安全

Spring Security 使web應用更加安全

一句話介紹

Spring Security 是基於Spring AOP和Servlet過濾器的安全框架,安全主要包括兩個操作“認證”與“驗證”(有時候也會叫做許可權控制)

開始使用

引入類庫,例子中使用到的security版本是3.1

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <org.springframework-version>4.1.4.RELEASE</org.springframework-version
>
<org.slf4j-version>1.7.8</org.slf4j-version> </properties> <dependencies> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version
>
${org.springframework-version}</version> <exclusions> <!-- Exclude Commons Logging in favor of SLF4j --> <exclusion> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId
>
</exclusion> </exclusions> </dependency> <!-- Logging --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${org.slf4j-version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>${org.slf4j-version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${org.slf4j-version}</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> </dependencies>

配置web.xml

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

    <!-- 載入security配置檔案 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/security.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

在WEB-INF下建立spring資料夾並新建一個security的配置檔案

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:security="http://www.springframework.org/schema/security"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    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 ">

    <!-- Spring Security 3.1 基於Spring AOP和Servlet過濾器的安全框架 -->
    <!-- pattern需要控制的url  access 許可權級別 這裡指定的許可權級別是管理員許可權 -->
    <security:http auto-config="true">
        <security:intercept-url pattern="/**" access="ROLE_ADMIN" />
    </security:http>
    <!-- AuthenticationManager用於配置認證身份  -->
    <security:authentication-manager>
        <security:authentication-provider>
            <!-- 這裡配置兩個不同許可權賬戶測試 -->
            <security:user-service>
                <security:user name="user" password="user" authorities="ROLE_USER" />
                <security:user name="admin" password="admin" authorities="ROLE_USER, ROLE_ADMIN" />
            </security:user-service>
        </security:authentication-provider>
    </security:authentication-manager>
</beans>

啟動專案

這裡寫圖片描述

可以看到的是由於我們並沒有登入(Session中沒有登入資訊),Spring 會給我們一個登入頁面,即我們的安全框架生效了。使用user/user 登入會跳403(沒有訪問許可權) ;admin/admin 會跳 index 首頁。

那麼

我們也沒有寫這個登入頁面,它從哪裡來呢?
這時我們需要看security的配置檔案

 <security:http auto-config="true">

這個配置項即告訴spring,我們使用預設的配置項,框架中的
org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter 的 generateLoginPageHtml() 方法會生成這個登入頁面,大家可以去查閱一下這部分的原始碼

還有一個問題就是這個action是怎麼被處理的?
同樣的,預設配置中框架中的
org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter 的 attemptAuthentication() 會去處理這個請求。