SpringSecurity身份驗證基礎入門

對於沒有訪問權限的用戶須要轉到登陸表單頁面。要實現訪問控制的方法多種多樣,能夠經過Aop、攔截器實現,也能夠經過框架實現(如:Apache Shiro、Spring Security)。

pom.xml添加依賴html

 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配置類web

 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 import org.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。spring

 

控制器:安全

 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.htmlapp

 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.htmlide

 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>

 

運行:spring-boot

打開index.html,點擊這裏,若是沒有登陸進入登陸頁,已登陸跳轉到hello.htmlweb安全

轉載於:這篇文章post

相關文章
相關標籤/搜索