Spring Boot中使用Spring Security如何實現安全控制?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
準備工作
首先,構建一個簡單的Web工程,以用于后續添加安全控制,也可以用之前Chapter3-1-2 做為基礎工程。若對如何使用Spring Boot構建Web應用,可以先閱讀 《Spring Boot開發Web應用》 一文。
Web層實現請求映射
@Controller public class HelloController { @RequestMapping("/") public String index() { return "index"; } @RequestMapping("/hello") public String hello() { return "hello"; } }
實現映射的頁面
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 的鏈接,顯然在這里沒有任何安全控制,所以點擊鏈接后就可以直接跳轉到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() .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配置之后,我們還缺少登錄的相關內容。
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 的登錄方式。
根據配置,Spring Security提供了一個過濾器來攔截請求并驗證用戶身份。如果用戶身份認證失敗,頁面就重定向到 /login?error ,并且頁面中會展現相應的錯誤信息。若用戶想要注銷登錄,可以通過訪問 /login?logout 請求,在完成注銷之后,頁面展現相應的成功消息。
到這里,我們啟用應用,并訪問 http://localhost:8080/ ,可以正常訪問。但是訪問 http://localhost:8080/hello 的時候被重定向到了 http://localhost:8080/login 頁面,因為沒有登錄,用戶沒有訪問權限,通過輸入用戶名user和密碼password進行登錄后,跳轉到了Hello World頁面,再也通過訪問 http://localhost:8080/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> <h2 th:inline="text">Hello [[${#httpServletRequest.remoteUser}]]!</h2> <form th:action="@{/logout}" method="post"> <input type="submit" value="注銷"/> </form> </body> </html>
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。