SpringSecurity入門到實戰

前言:放假了一直在敲項目,之前敲的品優購項目,下載還沒有更新文章,其實已經做完幾個大模塊了,之所以遲遲沒有更新就是,敲着停不下來,因爲寫文章實在是太費時間了,就捨不得停下來,這段時間敲的太多了,還是更新一下,當做複習吧。這次講的是SpringSecurity安全框架,可能相對比shiro來說SpringSecurity會複雜的多,更多的公司會使用shiro,因爲shiro簡單易上手,基本已經滿足一般公司的安全登錄操作了。但是還是要學一下SpringSecurity的,畢竟有大廠在用,也是Spring家族中的東西。

一、spring security 簡介

spring security 的核心功能主要包括:

  • 認證 (你是誰)
  • 授權 (你能幹什麼)
  • 攻擊防護 (防止僞造身份)

其核心就是一組過濾器鏈,項目啓動後將會自動配置。最核心的就是 Basic Authentication Filter 用來認證用戶的身份,一個在spring security中一種過濾器處理一種認證方式。
在這裏插入圖片描述

比如,對於username password認證過濾器來說, 會檢查是否是一個登錄請求;是否包含username 和 password (也就是該過濾器需要的一些認證信息) ;如果不滿足則放行給下一個。

下一個按照自身職責判定是否是自身需要的信息,basic的特徵就是在請求頭中有 Authorization:Basic eHh4Onh4 的信息。中間可能還有更多的認證過濾器。最後一環是 FilterSecurityInterceptor,這裏會判定該請求是否能進行訪問rest服務,判斷的依據是 BrowserSecurityConfig中的配置,如果被拒絕了就會拋出不同的異常(根據具體的原因)。Exception Translation Filter 會捕獲拋出的錯誤,然後根據不同的認證方式進行信息的返回提示。

注意:綠色的過濾器可以配置是否生效,其他的都不能控制。

二、SpringSecurity入門實戰

  1. 創建一個普通的maven工程,打包方式爲war
  2. 在 src/main/resources中加入如下文件

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>
		
		<!-- 配置頁面的攔截規則  use-expressions="false"是否啓用SPEL表達式-->				
		<http use-expressions="false">
		<!-- 當前應乎必須屬於ROLE_USER這個角色,纔可以訪問根目錄以及所屬子目錄的資源 -->
			<intercept-url pattern="/**" access="ROLE_USER" />
			<!-- 開啓表單登錄的功能 -->
			<form-login login-page="/login.html" default-target-url="/index.html" authentication-failure-url="/login_error.html"/>
			<csrf disabled="true"/>
		</http>
		
		<!-- 認證管理器 -->
		<authentication-manager>
			<authentication-provider>
				<user-service>
				   <!--表示配置用戶屬於ROLE_USER並且配置用戶登陸的密碼和賬號-->
					<user name="admin" password="123456" authorities="ROLE_USER"/>
				</user-service>
			</authentication-provider>
		</authentication-manager>
		
</beans:beans>

這裏的很多東西都有註解了,說一下沒有註解的標籤,form-login標籤表示配置一個登陸的功能,login-page="/login.html"表示配置我們自己登陸的頁面路徑,若是沒有配置form-login這個標籤,SpringSecurity會自動幫我們生成一個登陸的頁面,但是都不會用SpringSecurity給我們生成的登錄頁面,authentication-failure-url標示密碼或者賬號錯誤跳轉的頁面,default-target-url表示密碼或者賬號正確登陸後跳轉的頁面,表示關閉csrf

  1. 配置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"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	version="2.5">	

  	 <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>
	
	 <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>
	
</web-app>

這個就是個Spring的配置文件而已,相信很多人都能看懂
4. 創建html頁面

index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>首頁</title>
</head>
<body>
<h1>歡迎進入進入神奇的Spring-Security世界</h1>
</body>
</html>

login_error.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>用戶名密碼錯誤</h1>
</body>
</html>

login.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登錄</title>
</head>
<body>

--歡迎登錄我的系統--
<form action="/login" method="post">
	用戶名:<input name="username"><br>
	密碼:<input name="password"><br>
	<button>登錄</button>
</form>

</body>
</html>

注意:特別說明一下login.html中東西,裏面的form表單的提交,其中name=“username"和name=“password”
是必須的,因爲SpringSecurity中默認就是以username和password來接受的,當然這個名字也可以改,但是一般沿用它的就行了,沒必要改,然後就是form表單的提交必須是method=“post”,提交的動作必須是action=”/login",一般不做修改。

項目的目錄結構爲

在這裏插入圖片描述

三、測試

啓動項目,因爲是war項目,所以要配置tomcat來啓動,啓動的命令直接就是tomcat7:run就行了,如圖表示啓動成功,然後直接粘貼這個url到瀏覽器進行訪問
在這裏插入圖片描述
登陸頁面如圖所示,我們直接輸入localhost:9090/index.html是不允許的,會被重定向到login.html頁面中,如
圖所示
在這裏插入圖片描述
然後輸入錯誤的密碼或者賬號,如圖所示,就會被重定向到錯誤的頁面,這個使我們自己配置的
在這裏插入圖片描述
最後輸入正確的密碼和賬號,如圖所示,就會直接t跳轉到index.html頁面中
在這裏插入圖片描述

更多的教程請關注:飛科班的科班

發佈了63 篇原創文章 · 獲贊 34 · 訪問量 4865
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章