SpringSecurity安全框架的使用
框架簡介
Spring Security 是一個能夠為基于 Spring 的企業(yè)應(yīng)用系統(tǒng)提供聲明式的安全訪問控制解決方案的安全框架;它提供了一組可以在 Spring 應(yīng)用上下文中配置的 Bean,充分利用了 Spring IoC, DI 和 AOP 功能,為應(yīng)用系統(tǒng)提供聲明式的安全訪問控制功能,減少了為企業(yè)系統(tǒng)安全控制編寫大量重復(fù)代碼的工作。
簡單的說,就是我們可以控制用戶登陸的權(quán)限,通過配置讓不同權(quán)限的用戶登錄后所看到頁面不同,所能操作的內(nèi)容也不同,本篇博客介紹security簡單的配置和使用。
環(huán)境搭建
目錄結(jié)構(gòu):

1.POM文件
<?xml version="1.0" encoding="UTF-8"?>
<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>cn.itcast</groupId>
<artifactId>spring-security-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<spring.version>4.2.4.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>4.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>4.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- java 編譯插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<!-- 指定端口 -->
<port>9090</port>
<!-- 請求路徑 -->
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>2.測試用的首頁Index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>首頁</title>
</head>
<body>
<h3>Hello My Cookie !</h3>
</body>
</html>3. web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-security.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--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>
<!-- 指定默認(rèn)訪問的首頁 -->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>4.security配置文件spring-security.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.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.xsd">
<!-- 頁面攔截規(guī)則 -->
<!--
use-expressions 為是否使用使用 Spring 表達(dá)式語言( SpEL ) ,
默認(rèn)為 true ,如果開啟,則攔截的配置:
<intercept-url pattern="/**" access="hasRole('ROLE_USER')" />
-->
<http use-expressions="false">
<!-- intercept-url 表示攔截頁面 -->
<!--/* 表示的是該目錄下的資源,只包括本級目錄不包括下級目錄-->
<!--/** 表示的是該目錄以及該目錄下所有級別子目錄的資源-->
<intercept-url pattern="/**" access="ROLE_USER"/>
<!--form-login 為開啟表單登陸-->
<form-login/>
</http>
<!-- 認(rèn)證管理器 -->
<authentication-manager>
<authentication-provider>
<user-service>
<!--/** 測試登錄用的用戶名和密碼.ROLE_USER代表權(quán)限-->
<user name="admin" password="123456" authorities="ROLE_USER"/>
</user-service>
</authentication-provider>
</authentication-manager>
</beans:beans>測試環(huán)境
啟動服務(wù)器,訪問 localhost:9090 ,本來我們應(yīng)該是能夠直接進(jìn)入指定的 index.html 首頁,但是安全控制器攔截下了我們,并且跳轉(zhuǎn)到了系統(tǒng)自動生成的login頁面

我們在輸入了自定義的賬號和密碼后才能夠正常訪問頁面,這里Spring Security框架的作用就體現(xiàn)出來了, 他能夠幫助我們控制用戶的權(quán)限,讓不同權(quán)限的用戶訪問同一頁面的內(nèi)容不同,從而增加應(yīng)用的安全性。
實際的開發(fā)中,我們會使用自己定義的界面作為跳轉(zhuǎn)的界面,下面簡單介紹下如何配置:
自定義的登陸頁面:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登陸</title>
</head>
<body>
<form action='/login' method='POST'>
<table>
<tr>
<td>用戶名:</td>
<td><input type='text' name='username' value=''></td>
</tr>
<tr>
<td>密碼:</td>
<td><input type='password' name='password'/></td>
</tr>
<tr>
<td colspan='2'><input name="submit" type="submit"
value="登陸"/></td>
</tr>
</table>
</form>
</body>
</html>登陸失敗的頁面:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登陸失敗</title>
</head>
<body>
<h3 style="color : red;">驗證失敗 !</h3>
</body>
</html>修改spring-security.xml配置文件:
首先,要看配置我們自定義的頁面不被攔截:
<!-- 以下頁面不被攔截 security="none" 頁面不被攔截,即不登陸也能訪問 -->
<http pattern="/login.html" security="none"></http>
<http pattern="/login_error.html" security="none"></http>設(shè)置表單登陸:
<!--form-login 為開啟表單登陸-->
<!--
login-page:指定登錄頁面。
authentication-failure-url:指定了身份驗證失敗時跳轉(zhuǎn)到的頁面。
default-target-url:指定了成功進(jìn)行身份驗證和授權(quán)后默認(rèn)呈現(xiàn)給用戶的頁面。
-->
<form-login login-page="/login.html" authentication-failure-url="/login_error.html"
default-target-url="/index.html"/>關(guān)閉 csrf,如果不關(guān)登錄后不管成功或者失敗都會報錯誤:
<csrf disabled="true"/>
CSRF(Cross-site request forgery):跨站請求偽造,也被稱為“One Click Attack”或者 SessionRiding,通??s寫為 CSRF 或者 XSRF,是一種對網(wǎng)站的惡意利用。
當(dāng)使用html頁面時,不關(guān)閉就會報錯:
整體spring-security.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.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.xsd">
<!-- 以下頁面不被攔截 -->
<http pattern="/login.html" security="none"></http>
<http pattern="/login_error.html" security="none"></http>
<!-- 頁面攔截規(guī)則 -->
<!--
use-expressions 為是否使用使用 Spring 表達(dá)式語言( SpEL ) ,
默認(rèn)為 true ,如果開啟,則攔截的配置:
<intercept-url pattern="/**" access="hasRole('ROLE_USER')" />
-->
<http use-expressions="false">
<!-- intercept-url 表示攔截頁面 -->
<!--/* 表示的是該目錄下的資源,只包括本級目錄不包括下級目錄-->
<!--/** 表示的是該目錄以及該目錄下所有級別子目錄的資源-->
<intercept-url pattern="/**" access="ROLE_USER"/>
<!--form-login 為開啟表單登陸-->
<!--
login-page:指定登錄頁面。
authentication-failure-url:指定了身份驗證失敗時跳轉(zhuǎn)到的頁面。
default-target-url:指定了成功進(jìn)行身份驗證和授權(quán)后默認(rèn)呈現(xiàn)給用戶的頁面。
-->
<form-login login-page="/login.html" authentication-failure-url="/login_error.html"
default-target-url="/index.html"/>
<!--csrf disabled="true" 關(guān)閉 csrf ,如果不加會出現(xiàn)錯誤-->
<csrf disabled="true"/>
</http>
<!-- 認(rèn)證管理器 -->
<authentication-manager>
<authentication-provider>
<user-service>
<user name="admin" password="123456" authorities="ROLE_USER"/>
</user-service>
</authentication-provider>
</authentication-manager>
</beans:beans>總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java中使用StackWalker和Stream API進(jìn)行堆棧遍歷
StackWalking API是添加到Java中最酷的(并且對大多數(shù)開發(fā)人員來說完全不切實際,一般不會用,除非深層跟蹤調(diào)優(yōu))的功能之一。在這篇簡短的文章中,我們將看到它是什么以及使用它有多么容易,很快的認(rèn)識它2018-09-09
詳解Spring中Spel表達(dá)式和el表達(dá)式的區(qū)別
在?Java?開發(fā)中,表達(dá)式語言是一種強(qiáng)大的工具,而SpEL?表達(dá)式與EL?表達(dá)式是我們常常遇到兩種表達(dá)式語言,下面我們就來看看它們的具體使用與區(qū)別吧2023-07-07
SpringBoot + Mybatis增刪改查實戰(zhàn)記錄
這篇文章主要給大家介紹了關(guān)于SpringBoot + Mybatis增刪改查的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
spring boot實現(xiàn)過濾器和攔截器demo
本篇文章主要介紹了spring boot實現(xiàn)過濾器和攔截器demo ,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02

