1. 今日內容
文章目錄
2. spring-security概述
1. 爲什麼學習spring-security?
* spring-security框架是一套專門針對於權限處理的一套解決方案,比如登入操作,不同的用戶有不同的權限。當然這個登入操作我們使用攔截器或
者過濾器也可以實現,並在這樣更加的輕量級。但是,在實際的開發過程中,針對於權限的操作不止限於登入操作,還有一系列其他的操作,比
如記住密碼,session管理等等。而這些,spring-security都已經幫我們生成了相應的工具,我們進行配置和調用就可以使用。
2. spring-security介紹
* Spring Security 爲基於J2EE企業應用軟件提供了全面安全服務。特別 是使用領先的J2EE解決方案-Spring框架開發的企業軟件項目。人們使用
Spring Security有很多種原因,不過通常吸 引他們的是在J2EE Servlet規範或EJB規範中找不到典型企業應用場景的解決方案。 特別要指出的是
他們不能再 WAR 或 EAR 級別進行移植。這樣,如果你更換服務器環境,就要,在新的目標環境進行大量的工作,對你的應用 系統進行重新配
置安全。使用Spring Security 解決了這些問題,也爲你提供很多有用的,完全可以指定的其他安 全特性。 安全包括兩個主要操作。
1. “認證”,是爲用戶建立一個他所聲明的主體。主題一般式指用戶,設備或可以在你係統中執行動作的其他系統。
2. “授權”指的是一個用戶能否在你的應用中執行某個操作,在到達授權判斷之前,身份的主題已經由 身份驗證 過程建立了。
3. Spring Security 目前支持認證一體化如下認證技術
1. HTTP BASIC authentication headers (一個基於IEFT RFC 的 標準)
2. HTTP Digest authentication headers (一個基於IEFT RFC 的標準)
3. HTTP X.509 client certificate exchange (一個基於IEFT RFC 的標準)
4. LDAP (一個非常常見的跨平臺認證需要做法,特別是在大環境)
5. Form-based authentication (提供簡單用戶接口的需求)
6. OpenID authentication Computer Associates Siteminder JA-SIG Central Authentication Service (CAS,這是一個流行的開源單點登錄系統)
7. Transparent authentication context propagation for Remote Method Invocation and HttpInvoker (一個Spring遠程調用協議)
2. spring-security入門案例
2.1 在pom.xml文件中導入依賴
<spring.security.version>5.0.1.RELEASE</spring.security.version>
<!-- spring-security的jar包 -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>${spring.security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>${spring.security.version}</version>
</dependency>
2.2 創建 spring-security.xml配置文件並配置
在resources目錄
下建立spring-security.xml
,並配置:
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.xsd">
<!--
1. auto-config="true" 是自動配置,能夠啓動幫我們生成一個登入驗證的頁面
2. use-expressions="false" 不使用 hasRole('ROLE_USER') 表達,
如果不寫,默認爲true,則需要寫<security:intercept-url pattern="/**" access="hasRole('ROLE_USER')"/>
否則會報錯
3. intercept-url定義一個過濾規則 pattern表示對哪些url進行權限控制。/** 表示對所有url
4. ccess屬性表示在請求對應 的URL時需要什麼權限,裏面必須以ROLE_開頭。即ROLE_XXX
如果有多個權限用逗號隔開 "ROLE_USER,ROLE_ADMIN"
-->
<security:http auto-config="true" use-expressions="false">
<security:intercept-url pattern="/**" access="ROLE_USER"/>
</security:http>
<!-- 構造用戶。實際開發過程中不配置一下代碼,是從數據庫中讀取用戶,但是此代碼對整合測試階段非常有用 -->
<security:authentication-manager>
<security:authentication-provider>
<security:user-service>
<!-- 密碼前面需要帶{noop},不然不能識別密碼 -->
<security:user name="zhangsan" password="{noop}123" authorities="ROLE_USER"></security:user>
</security:user-service>
</security:authentication-provider>
</security:authentication-manager>
</beans>
注意:
- 設置 use-expressions=“false”
- ccess屬性表示在請求對應 的URL時需要什麼權限,裏面必須以ROLE_開頭。即ROLE_XXX
- 密碼前面需要帶{noop},不然不能識別密碼
- 實際開發過程中,不會構造用戶,用戶是從數據庫中讀取的。
- 當指定http元素的auto-config=”true”時,就相當於如下內容的簡寫:
<security:http> <security:form-login/> <security:http-basic/> <security:logout/> </security:http>
這些元素負責建立表單登錄、基本的認證和登出處理。它們都可以通過指定對應的屬性來改變它們的行爲。
2.3 在 web.xml文件中加載spring-security.xml文件
<!-- 配置加載類路徑的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml,classpath:spring-security.xml</param-value>
</context-param>
<!-- 配置Spring的監聽器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
注意:classpath:applicationContext是spring的配置文件,如果你不是spring項目,可以只寫
<param-value>classpath:spring-security.xml</param-value>
<!-- 配置哪些請求交給springSecurity處理,是第一個filter,在springmvc的filter之前 -->
<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>
注意:上面的springSecurity的過濾器可以自動生成:
2.4 檢測spring-security是否配置成功
啓動項目,訪問項目http://localhost/springSecurity/
:
輸入一個錯誤的用戶:
輸入正確的用戶後,就允許的跳到指定的index.jsp頁面了, 因爲http://localhost/springSecurity/
訪問的是index.jsp頁面:
如果能夠出現上面三種情況,則入門成功!!!