我們?cè)诰帉?xiě)Web應(yīng)用時(shí),經(jīng)常需要對(duì)頁(yè)面做一些安全控制,比如:對(duì)于沒(méi)有訪問(wèn)權(quán)限的用戶需要轉(zhuǎn)到登錄表單頁(yè)面。要實(shí)現(xiàn)訪問(wèn)控制的方法多種多樣,可以通過(guò)Aop、攔截器實(shí)現(xiàn),也可以通過(guò)框架實(shí)現(xiàn)(如:Apache Shiro、spring Security)。
本文將具體介紹在Spring Boot中如何使用Spring Security進(jìn)行安全控制。
準(zhǔn)備工作
首先,構(gòu)建一個(gè)簡(jiǎn)單的Web工程,以用于后續(xù)添加安全控制,也可以用之前Chapter3-1-2 做為基礎(chǔ)工程。若對(duì)如何使用Spring Boot構(gòu)建Web應(yīng)用,可以先閱讀 《Spring Boot開(kāi)發(fā)Web應(yīng)用》 一文。
Web層實(shí)現(xiàn)請(qǐng)求映射
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
@Controller public class HelloController { @RequestMapping ( "/" ) public String index() { return "index" ; } @RequestMapping ( "/hello" ) public String hello() { return "hello" ; } } |
- / :映射到index.html
- /hello :映射到hello.html
實(shí)現(xiàn)映射的頁(yè)面
src/main/resources/templates/index.html
1
2
3
4
5
6
7
8
9
10
|
<!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入門(mén)</ title > </ head > < body > < h1 >歡迎使用Spring Security!</ h1 > < p >點(diǎn)擊 < a th:href = "@{/hello}" rel = "external nofollow" >這里</ a > 打個(gè)招呼吧</ p > </ body > </ html > |
src/main/resources/templates/hello.html
1
2
3
4
5
6
7
8
9
10
|
<!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 的鏈接,顯然在這里沒(méi)有任何安全控制,所以點(diǎn)擊鏈接后就可以直接跳轉(zhuǎn)到hello.html頁(yè)面。
整合Spring Security
在這一節(jié),我們將對(duì) /hello 頁(yè)面進(jìn)行權(quán)限控制,必須是授權(quán)用戶才能訪問(wèn)。當(dāng)沒(méi)有權(quán)限的用戶訪問(wèn)后,跳轉(zhuǎn)到登錄頁(yè)面。
添加依賴
在pom.xml中添加如下配置,引入對(duì)Spring Security的依賴。
1
2
3
4
5
6
7
8
|
< dependencies > ... < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-security</ artifactId > </ dependency > ... </ dependencies > |
Spring Security配置
創(chuàng)建Spring Security的配置類(lèi) WebSecurityConfig ,具體如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
@Configuration @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers( "/" , "/home" ).permitAll() .anyRequest().authenticated() .and() .formLogin() .loginPage( "/login" ) .permitAll() .and() .logout() .permitAll(); } @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth .inMemoryAuthentication() .withUser( "user" ).password( "password" ).roles( "USER" ); } } |
- 通過(guò) @EnableWebMvcSecurity 注解開(kāi)啟Spring Security的功能
- 繼承 WebSecurityConfigurerAdapter ,并重寫(xiě)它的方法來(lái)設(shè)置一些web安全的細(xì)節(jié)
-
configure(HttpSecurity http) 方法
- 通過(guò) authorizeRequests() 定義哪些URL需要被保護(hù)、哪些不需要被保護(hù)。例如以上代碼指定了 / 和 /home 不需要任何認(rèn)證就可以訪問(wèn),其他的路徑都必須通過(guò)身份驗(yàn)證。
- 通過(guò) formLogin() 定義當(dāng)需要用戶登錄時(shí)候,轉(zhuǎn)到的登錄頁(yè)面。
- configureGlobal(AuthenticationManagerBuilder auth) 方法,在內(nèi)存中創(chuàng)建了一個(gè)用戶,該用戶的名稱(chēng)為user,密碼為password,用戶角色為USER。
新增登錄請(qǐng)求與頁(yè)面
在完成了Spring Security配置之后,我們還缺少登錄的相關(guān)內(nèi)容。
HelloController中新增 /login 請(qǐng)求映射至 login.html
1
2
3
4
5
6
7
8
9
10
11
|
@Controller public class HelloController { // 省略之前的內(nèi)容... @RequestMapping ( "/login" ) public String login() { return "login" ; } } |
新增登錄頁(yè)面: src/main/resources/templates/login.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<!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}" > 用戶名或密碼錯(cuò) </ div > < div th:if = "${param.logout}" > 您已注銷(xiāo)成功 </ 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 > |
可以看到,實(shí)現(xiàn)了一個(gè)簡(jiǎn)單的通過(guò)用戶名和密碼提交到 /login 的登錄方式。
根據(jù)配置,Spring Security提供了一個(gè)過(guò)濾器來(lái)攔截請(qǐng)求并驗(yàn)證用戶身份。如果用戶身份認(rèn)證失敗,頁(yè)面就重定向到 /login?error ,并且頁(yè)面中會(huì)展現(xiàn)相應(yīng)的錯(cuò)誤信息。若用戶想要注銷(xiāo)登錄,可以通過(guò)訪問(wèn) /login?logout 請(qǐng)求,在完成注銷(xiāo)之后,頁(yè)面展現(xiàn)相應(yīng)的成功消息。
到這里,我們啟用應(yīng)用,并訪問(wèn) http://localhost:8080/ ,可以正常訪問(wèn)。但是訪問(wèn) http://localhost:8080/hello 的時(shí)候被重定向到了 http://localhost:8080/login 頁(yè)面,因?yàn)闆](méi)有登錄,用戶沒(méi)有訪問(wèn)權(quán)限,通過(guò)輸入用戶名user和密碼password進(jìn)行登錄后,跳轉(zhuǎn)到了Hello World頁(yè)面,再也通過(guò)訪問(wèn) http://localhost:8080/login?logout ,就可以完成注銷(xiāo)操作。
為了讓整個(gè)過(guò)程更完成,我們可以修改 hello.html ,讓它輸出一些內(nèi)容,并提供“注銷(xiāo)”的鏈接。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<!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 = "注銷(xiāo)" /> </ form > </ body > </ html > |
本文通過(guò)一個(gè)最簡(jiǎn)單的示例完成了對(duì)Web應(yīng)用的安全控制,Spring Security提供的功能還遠(yuǎn)不止于此,更多Spring Security的使用可參見(jiàn) Spring Security Reference 。
完整示例: Chapter4-3-1
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://blog.csdn.net/hj7jay/article/details/51730284