我們在編寫Web應用時,經常需要對頁面做一些安全控制,比如:對於沒有訪問權限的用戶需要轉到登錄表單頁面。要實現訪問控制的方法多種多樣,可以通過Aop、攔截器實現,也可以通過框架實現(如:Apache Shiro、Spring Security)。
本文將具體介紹在Spring Boot中如何使用Spring Security進行安全控制。
準備工作
Web層實現請求映射
@Controller
public class HelloController {
@RequestMapping("/")
public String index() {
return "index";
}
@RequestMapping("/hello")
public String hello() {
return "hello";
}
}
- / :映射到index.html
- /hello :映射到hello.html
實現映射的頁面
- src/main/resources/templates/index.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入門</title>
</head>
<body>
<h1>歡迎使用Spring Security!</h1>
<p>點擊 <a th:href="@{/hello}">這裏</a> 打個招呼吧</p>
</body>
</html>
- 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>
可以看到在index.html中提供到 /hello 的鏈接,顯然在這裏沒有任何安全控制,所以點擊鏈接後就可以直接跳轉到hello.html頁面。(不讓其他人隨便訪問hello.html)
整合Spring Security
在這一節,我們將對 /hello 頁面進行權限控制,必須是授權用戶才能訪問。當沒有權限的用戶訪問後,跳轉到登錄頁面。
添加依賴
在pom.xml中添加如下配置,引入對Spring Security的依賴。
<dependencies>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
...
</dependencies>
Spring Security配置
創建Spring Security的配置類 WebSecurityConfig ,具體如下:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/home").permitAll() // / 和 /home 不需要任何認證就可以訪問
.anyRequest().authenticated()
.and()
.formLogin() //通過 formLogin() 定義當需要用戶登錄時候,轉到的登錄頁面。
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("user").password("password").roles("USER");
}
}
-
通過 @EnableWebMvcSecurity 註解開啓Spring Security的功能(小疑問:@EnableWebMvcSecurity和 @EnableWebSecurity似乎一樣啊)
-
繼承 WebSecurityConfigurerAdapter ,並重寫它的方法來設置一些web安全的細節
-
configure(HttpSecurity http) 方法
通過 authorizeRequests() 定義哪些URL需要被保護、哪些不需要被保護。例如以上代碼指定了 / 和 /home 不需要任何認證就可以訪問,其他的路徑都必須通過身份驗證。通過 formLogin() 定義當需要用戶登錄時候,轉到的登錄頁面。
-
configureGlobal(AuthenticationManagerBuilder auth) 方法,在內存中創建了一個用戶,該用戶的名稱爲user,密碼爲password,用戶角色爲USER。
新增登錄請求與頁面
在完成了Spring Security配置之後,我們還缺少登錄的相關內容。
HelloController中新增 /login 請求映射至 login.html
@Controller
public class HelloController {
// 省略之前的內容...
@RequestMapping("/login")
public String login() {
return "login";
}
}
新增登錄頁面: src/main/resources/templates/login.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>
<div th:if="${param.error}">
用戶名或密碼錯
</div>
<div th:if="${param.logout}">
您已註銷成功
</div>
<form th:action="@{/login}" method="post">
<div><label> 用戶名 : <input type="text" name="username"/> </label></div>
<div><label> 密 碼 : <input type="password" name="password"/> </label></div>
<div><input type="submit" value="登錄"/></div>
</form>
</body>
</html>
可以看到,實現了一個簡單的通過用戶名和密碼提交到 /login 的登錄方式。