Spring Boot中使用Spring Security如何實現(xiàn)安全控制?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
公司主營業(yè)務(wù):成都網(wǎng)站設(shè)計、成都網(wǎng)站制作、外貿(mào)網(wǎng)站建設(shè)、移動網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。成都創(chuàng)新互聯(lián)是一支青春激揚、勤奮敬業(yè)、活力青春激揚、勤奮敬業(yè)、活力澎湃、和諧高效的團隊。公司秉承以“開放、自由、嚴謹、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團隊有機會用頭腦與智慧不斷的給客戶帶來驚喜。成都創(chuàng)新互聯(lián)推出靖遠免費做網(wǎng)站回饋大家。
準備工作
首先,構(gòu)建一個簡單的Web工程,以用于后續(xù)添加安全控制,也可以用之前Chapter3-1-2 做為基礎(chǔ)工程。若對如何使用Spring Boot構(gòu)建Web應(yīng)用,可以先閱讀 《Spring Boot開發(fā)Web應(yīng)用》 一文。
Web層實現(xiàn)請求映射
@Controller public class HelloController { @RequestMapping("/") public String index() { return "index"; } @RequestMapping("/hello") public String hello() { return "hello"; } }
實現(xiàn)映射的頁面
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> <h2>歡迎使用Spring Security!</h2> <p>點擊 <a th:href="@{/hello}" rel="external nofollow" >這里</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> <h2>Hello world!</h2> </body> </html>
可以看到在index.html中提供到 /hello 的鏈接,顯然在這里沒有任何安全控制,所以點擊鏈接后就可以直接跳轉(zhuǎn)到hello.html頁面。
整合Spring Security
在這一節(jié),我們將對 /hello 頁面進行權(quán)限控制,必須是授權(quán)用戶才能訪問。當沒有權(quán)限的用戶訪問后,跳轉(zhuǎn)到登錄頁面。
添加依賴
在pom.xml中添加如下配置,引入對Spring Security的依賴。
<dependencies> ... <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> ... </dependencies>
Spring Security配置
創(chuàng)建Spring Security的配置類 WebSecurityConfig ,具體如下:
@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"); } }
新增登錄請求與頁面
在完成了Spring Security配置之后,我們還缺少登錄的相關(guān)內(nèi)容。
HelloController中新增 /login 請求映射至 login.html
@Controller public class HelloController { // 省略之前的內(nèi)容... @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>
可以看到,實現(xiàn)了一個簡單的通過用戶名和密碼提交到 /login 的登錄方式。
根據(jù)配置,Spring Security提供了一個過濾器來攔截請求并驗證用戶身份。如果用戶身份認證失敗,頁面就重定向到 /login?error ,并且頁面中會展現(xiàn)相應(yīng)的錯誤信息。若用戶想要注銷登錄,可以通過訪問 /login?logout 請求,在完成注銷之后,頁面展現(xiàn)相應(yīng)的成功消息。
到這里,我們啟用應(yīng)用,并訪問 http://localhost:8080/ ,可以正常訪問。但是訪問 http://localhost:8080/hello 的時候被重定向到了 http://localhost:8080/login 頁面,因為沒有登錄,用戶沒有訪問權(quán)限,通過輸入用戶名user和密碼password進行登錄后,跳轉(zhuǎn)到了Hello World頁面,再也通過訪問 http://localhost:8080/login?logout ,就可以完成注銷操作。
為了讓整個過程更完成,我們可以修改 hello.html ,讓它輸出一些內(nèi)容,并提供“注銷”的鏈接。
<!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> <h2 th:inline="text">Hello [[${#httpServletRequest.remoteUser}]]!</h2> <form th:action="@{/logout}" method="post"> <input type="submit" value="注銷"/> </form> </body> </html>
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。
網(wǎng)站欄目:SpringBoot中使用SpringSecurity如何實現(xiàn)安全控制
文章出自:http://jinyejixie.com/article42/pocihc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、網(wǎng)站內(nèi)鏈、微信公眾號、外貿(mào)建站、企業(yè)網(wǎng)站制作、做網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)