## Spring Security Guides
這一節我們通過Spring官方給出的一個guides例子,來了解Spring Security是如何保護我們的應用的,之后會對進行一個解讀。
### 2.1 引入依賴
~~~
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
~~~
由于我們集成了springboot,所以不需要顯示的引入Spring Security文檔中描述core,config依賴,只需要引入spring-boot-starter-security即可。
### 2.2 創建一個不受安全限制的web應用
這是一個首頁,不受安全限制
`src/main/resources/templates/home.html`
~~~
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Spring Security Example</title>
</head>
<body>
<h1>Welcome!</h1>
<p>Click <a th:href="@{/hello}">here</a> to see a greeting.</p>
</body>
</html>
~~~
這個簡單的頁面上包含了一個鏈接,跳轉到”/hello”。對應如下的頁面
`src/main/resources/templates/hello.html`
~~~
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Hello World!</title>
</head>
<body>
<h1>Hello world!</h1>
</body>
</html>
~~~
接下來配置Spring MVC,使得我們能夠訪問到頁面。
~~~
@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/home").setViewName("home");
registry.addViewController("/").setViewName("home");
registry.addViewController("/hello").setViewName("hello");
registry.addViewController("/login").setViewName("login");
}
}
~~~
### 2.3 配置Spring Security
一個典型的安全配置如下所示:
~~~
@Configuration
@EnableWebSecurity <1>
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { <1>
@Override
protected void configure(HttpSecurity http) throws Exception {
http <2>
.authorizeRequests()
.antMatchers("/", "/home").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth <3>
.inMemoryAuthentication()
.withUser("admin").password("admin").roles("USER");
}
}
~~~
<1> @EnableWebSecurity注解使得SpringMVC集成了Spring Security的web安全支持。另外,WebSecurityConfig配置類同時集成了WebSecurityConfigurerAdapter,重寫了其中的特定方法,用于自定義Spring Security配置。整個Spring Security的工作量,其實都是集中在該配置類,不僅僅是這個guides,實際項目中也是如此。
<2> configure(HttpSecurity)定義了哪些URL路徑應該被攔截,如字面意思所描述:”/“, “/home”允許所有人訪問,”/login”作為登錄入口,也被允許訪問,而剩下的”/hello”則需要登陸后才可以訪問。
<3> configureGlobal(AuthenticationManagerBuilder)在內存中配置一個用戶,admin/admin分別是用戶名和密碼,這個用戶擁有USER角色。
我們目前還沒有登錄頁面,下面創建登錄頁面:
~~~
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Spring Security Example </title>
</head>
<body>
<div th:if="${param.error}">
Invalid username and password.
</div>
<div th:if="${param.logout}">
You have been logged out.
</div>
<form th:action="@{/login}" method="post">
<div><label> User Name : <input type="text" name="username"/> </label></div>
<div><label> Password: <input type="password" name="password"/> </label></div>
<div><input type="submit" value="Sign In"/></div>
</form>
</body>
</html>
~~~
這個Thymeleaf模板提供了一個用于提交用戶名和密碼的表單,其中name=”username”,name=”password”是默認的表單值,并發送到“/ login”。 在默認配置中,Spring Security提供了一個攔截該請求并驗證用戶的過濾器。 如果驗證失敗,該頁面將重定向到“/ login?error”,并顯示相應的錯誤消息。 當用戶選擇注銷,請求會被發送到“/ login?logout”。
最后,我們為hello.html添加一些內容,用于展示用戶信息。
~~~
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Hello World!</title>
</head>
<body>
<h1 th:inline="text">Hello [[${#httpServletRequest.remoteUser}]]!</h1>
<form th:action="@{/logout}" method="post">
<input type="submit" value="Sign Out"/>
</form>
</body>
</html>
~~~
我們使用Spring Security之后,HttpServletRequest#getRemoteUser()可以用來獲取用戶名。 登出請求將被發送到“/ logout”。 成功注銷后,會將用戶重定向到“/ login?logout”。
### 2.4 添加啟動類
~~~
@SpringBootApplication
public class Application {
public static void main(String[] args) throws Throwable {
SpringApplication.run(Application.class, args);
}
}
~~~
### 2.5 測試
略。
### 2.6 總結
本篇文章沒有什么干貨,基本算是翻譯了Spring Security Guides的內容,稍微了解Spring Security的朋友都不會對這個翻譯感到陌生。考慮到受眾的問題,一個入門的例子是必須得有的,方便后續對Spring Security的自定義配置進行講解。下一節,以此guides為例,講解這些最簡化的配置背后,Spring Security都幫我們做了什么工作。
本節所有的代碼,可以直接在Spring的官方倉庫下載得到,git clone https://github.com/spring-guides/gs-securing-web.git 。不過,建議初學者根據文章先一步步配置,出了問題,再與demo進行對比。
- java
- 設計模式
- 設計模式總覽
- 設計原則
- 工廠方法模式
- 抽象工廠模式
- 單例模式
- 建造者模式
- 原型模式
- 適配器模式
- 裝飾者模式
- 代理模式
- 外觀模式
- 橋接模式
- 組合模式
- 享元模式
- 策略模式
- 模板方法模式
- 觀察者模式
- 迭代子模式
- 責任鏈模式
- 命令模式
- 備忘錄模式
- 狀態模式
- 訪問者模式
- 中介者模式
- 解釋器模式
- 附錄
- JVM相關
- JVM內存結構
- Java虛擬機的內存組成以及堆內存介紹
- Java堆和棧
- 附錄-數據結構的堆棧和內存分配的堆區棧區的區別
- Java內存之Java 堆
- Java內存之虛擬機和內存區域概述
- Java 內存之方法區和運行時常量池
- Java 內存之直接內存(堆外內存)
- JAVA內存模型
- Java內存模型介紹
- 內存模型如何解決緩存一致性問題
- 深入理解Java內存模型——基礎
- 深入理解Java內存模型——重排序
- 深入理解Java內存模型——順序一致性
- 深入理解Java內存模型——volatile
- 深入理解Java內存模型——鎖
- 深入理解Java內存模型——final
- 深入理解Java內存模型——總結
- 內存可見性
- JAVA對象模型
- JVM內存結構 VS Java內存模型 VS Java對象模型
- Java的對象模型
- Java的對象頭
- HotSpot虛擬機
- HotSpot虛擬機對象探秘
- 深入分析Java的編譯原理
- Java虛擬機的鎖優化技術
- 對象和數組并不是都在堆上分配內存的
- 垃圾回收
- JVM內存管理及垃圾回收
- JVM 垃圾回收器工作原理及使用實例介紹
- JVM內存回收理論與實現(對象存活的判定)
- JVM參數及調優
- CMS GC日志分析
- JVM實用參數(一)JVM類型以及編譯器模式
- JVM實用參數(二)參數分類和即時(JIT)編譯器診斷
- JVM實用參數(三)打印所有XX參數及值
- JVM實用參數(四)內存調優
- JVM實用參數(五)新生代垃圾回收
- JVM實用參數(六) 吞吐量收集器
- JVM實用參數(七)CMS收集器
- JVM實用參數(八)GC日志
- Java性能調優原則
- JVM 優化經驗總結
- 面試題整理
- 面試題1
- java日志規約
- Spring安全
- OAtuth2.0簡介
- Spring Session 簡介(一)
- Spring Session 簡介(二)
- Spring Session 簡介(三)
- Spring Security 簡介(一)
- Spring Security 簡介(二)
- Spring Security 簡介(三)
- Spring Security 簡介(四)
- Spring Security 簡介(五)
- Spring Security Oauth2 (一)
- Spring Security Oauth2 (二)
- Spring Security Oauth2 (三)
- SpringBoot
- Shiro
- Shiro和Spring Security對比
- Shiro簡介
- Session、Cookie和Cache
- Web Socket
- Spring WebFlux