SpringSecurity身份驗證基礎入門
阿新 • • 發佈:2018-08-18
logo submit world! authorize group author port glob oba
pom.xml添加依賴
1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-web</artifactId> 4 </dependency> 5 6 <dependency> 7 <groupId>org.springframework.boot</groupId> 8 <artifactId>spring-boot-starter-thymeleaf</artifactId> 9 </dependency> 10 <dependency> 11 <groupId>org.springframework.boot</groupId> 12 <artifactId>spring-boot-starter-security</artifactId>13 </dependency>
創建SpringSecurity配置類
1 import org.springframework.beans.factory.annotation.Autowired; 2 import org.springframework.context.annotation.Configuration; 3 import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 4 importorg.springframework.security.config.annotation.web.builders.HttpSecurity; 5 import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; 6 import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 7 8 @Configuration 9 @EnableWebSecurity 10 public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 11 12 @Override 13 protected void configure(HttpSecurity http) throws Exception { 14 http 15 .authorizeRequests() 16 .antMatchers("/", "/home").permitAll() 17 .anyRequest().authenticated() 18 .and() 19 .formLogin() 20 .loginPage("/login") 21 .permitAll() 22 .and() 23 .logout() 24 .permitAll(); 25 } 26 27 @Autowired 28 public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 29 //inMemoryAuthentication 從內存中獲取 30 auth 31 .inMemoryAuthentication() 32 .passwordEncoder(new BCryptPasswordEncoder()) 33 .withUser("admin") 34 .password(new BCryptPasswordEncoder() 35 .encode("123456")).roles("USER"); 36 } 37 }
通過@EnableWebSecurity註解開啟Spring Security的功能
繼承WebSecurityConfigurerAdapter,並重寫它的方法來設置一些web安全的細節
configure(HttpSecurity http)方法,通過authorizeRequests()定義哪些URL需要被保護、哪些不需要被保護。例如以上代碼指定了/和/home不需要任何認證就可以訪問,其他的路徑都必須通過身份驗證。
通過formLogin()定義當需要用戶登錄時候,轉到的登錄頁面。
configureGlobal(AuthenticationManagerBuilder auth)方法,在內存中創建了一個用戶,該用戶的名稱為admin,密碼為123456,用戶角色為USER。
控制器:
1 @Controller 2 public class HelloController { 3 4 @RequestMapping("/") 5 public String index() { 6 return "index"; 7 } 8 9 @RequestMapping("/hello") 10 public String hello() { 11 return "hello"; 12 } 13 14 @RequestMapping(value = "/login", method = RequestMethod.GET) 15 public String login() { 16 return "login"; 17 } 18 19 }
index.html
1 <!DOCTYPE html> 2 <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" 3 xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"> 4 <head> 5 <title>Spring Security入門</title> 6 </head> 7 <body> 8 <h1>歡迎使用Spring Security!</h1> 9 10 <p>點擊 <a th:href="@{/hello}">這裏</a> 打個招呼吧</p> 11 </body> 12 </html>
hello.html
1 <!DOCTYPE html> 2 <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" 3 xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"> 4 <head> 5 <title>Hello World!</title> 6 </head> 7 <body> 8 <h1 th:inline="text">Hello [[${#httpServletRequest.remoteUser}]]!</h1> 9 <form th:action="@{/logout}" method="post"> 10 <input type="submit" value="註銷"/> 11 </form> 12 </body> 13 </html>
login.html
1 <!DOCTYPE html> 2 <html xmlns="http://www.w3.org/1999/xhtml" 3 xmlns:th="http://www.thymeleaf.org" 4 xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"> 5 <head> 6 <title>Spring Security Example </title> 7 </head> 8 <body> 9 <div th:if="${param.error}"> 10 用戶名或密碼錯 11 </div> 12 <div th:if="${param.logout}"> 13 您已註銷成功 14 </div> 15 <form th:action="@{/login}" method="post"> 16 <div><label> 用戶名 : <input type="text" name="username"/> </label></div> 17 <div><label> 密 碼 : <input type="password" name="password"/> </label></div> 18 <div><input type="submit" value="登錄"/></div> 19 </form> 20 </body> 21 </html>
運行:
打開index.html,點擊這裏,如果沒有登錄進入登錄頁,已登錄跳轉到hello.html
轉載於:這篇文章
SpringSecurity身份驗證基礎入門