尚硅谷SpringSecurity框架教程(spring security源碼剖

對P11的/user/login的解釋:

application.properties的配置:
# 應用服務 WEB 訪問端口 server.port=8080 # 設置項目的web名稱和請求路徑名稱 #server.servlet.context-path=/ server.servlet.context-path=/security-springboot spring.application.name=security-springboot # 通過springboot配置,設置用戶名密碼,優(yōu)先級低于test2 spring.security.user.name=test1 #spring.security.user.password=test1 spring.security.user.password=$2a$10$aJTTiKJ1Y/AYc0dTl0DFJ.ICzIr.4Edqj5OkugHTvwT2yHbpE2QBy # 設置datasource spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.username=root spring.datasource.password=123456 spring.datasource.url=jdbc:mysql://localhost:3306/worklearn_springsecurity?useSSL=false&serverTimezone=Asia/Shanghai&characterEncoding=UTF-8 # 設置mybatis-plus mybatis-plus.configuration.map-underscore-to-camel-case=false mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl mybatis-plus.global-config.db-config.id-type=auto #mybatis-plus.mapper-locations=
WebSecurityConfig的configure()方法:
@Override protected void configure(HttpSecurity http) throws Exception { http.formLogin() //自定義登陸頁面; // 瀏覽器的訪問路徑需要填http://localhost:8080/security-springboot/mylogin.html .loginPage("/mylogin.html") /* /loginTest的含義, 我們自定義頁面。頁面的action需要是/security-springboot/loginTest 因為自定義頁面的post請求的開頭默認是http://localhost:8080,這一點要與重定向區(qū)分開! 我踩了大坑在這里! 回到正題:相當于我們把鍵盤輸入數(shù)據(jù)A放在自定義頁面的表單中 自定義頁面的表單,將數(shù)據(jù)A發(fā)送post請求到路徑http://localhost:8080/security-springboot/loginTest springSecurity接收到post請求,認為符合自定義登錄條件1, 并分析出有效路徑loginTest與loginProcessingUrl("/loginTest")一致,認為符合自定義登錄條件2 springSecurity就把自定義頁面表單發(fā)送的數(shù)據(jù)A當作springSecurity默認頁面的默認表單發(fā)送的數(shù)據(jù) springSecurity將數(shù)據(jù)進行處理成為數(shù)據(jù)B,并進行登陸驗證 springSecurity登錄驗證通過,則重定向地發(fā)送get請求到http://localhost:8080/security-springboot/test/login-success */ .loginProcessingUrl("/loginTest") //登陸成功后的重定向路徑; // 因為是重定向所以項目會自動加上/security-springboot;最終的瀏覽器的訪問路徑顯示的是http://localhost:8080/security-springboot/test/login-success .defaultSuccessUrl("/test/login-success").permitAll() .and().authorizeRequests() //自定義哪些controller的功能方法不需要登錄驗證; // 瀏覽器的訪問路徑需要填http://localhost:8080/security-springboot/test/r/r1 .antMatchers("/test/r/r1").permitAll() //自定義哪些controller的功能方法需要登錄驗證; // 需要登錄驗證的訪問路徑訪問后會自動重定向到http://localhost:8080/security-springboot/mylogin.html .anyRequest().authenticated() //關閉csrf防護 .and().csrf().disable(); // .successForwardUrl("/test/login-success"); }
mylogin.html頁面:
<html> <head> <meta charset="utf-8"> </head> <body> <h1 align="center">自定義登陸頁面</h1> <form action="/security-springboot/loginTest" method="post"> 用戶名:<input type="text" name="username"><br/> 密 碼:<input type="password" name="password"><br/> <input type="submit" value="登錄"> </form> </body> </html>
LoginController控制器類:
package com.worklearn.worklearn_springsecurity.controller; import org.springframework.web.bind.annotation.*; /** * @version: 1.0 * @Author: wuqiu * @date: 2023-08-28 16:55 * @description: worklearn_SpringSecurity -> com.worklearn.worklearn_springsecurity.controller */ @RestController @RequestMapping("/test") public class LoginController { /* 我們在使用springSecurity默認頁面的默認表單時,這個loginSuccess方法需要postMapping 因為springSecurity默認頁面的默認表單發(fā)送的就是post請求, 數(shù)據(jù)被springSecurity接收并進行登錄驗證,通過后,springSecurity發(fā)送post重定向到http://localhost:8080/security-springboot/test/login-success 我們在使用自定義頁面的自定義表單時,為了滿足”讓springSecurity把自定義頁面發(fā)送的表單數(shù)據(jù)A當作springSecurity默認頁面的默認表單發(fā)送的數(shù)據(jù)“ 我們的自定義頁面的自定義表單也需要是發(fā)送post請求, 數(shù)據(jù)被springSecurity接收并進行登錄驗證,通過后, springSecurity知道這次的登陸數(shù)據(jù)來源是自定義頁面的,所以不發(fā)post重定向,而是get重定向到http://localhost:8080/security-springboot/test/login-success */ @PostMapping(value = "/login-success",produces = {"text/plain;charset=UTF-8"}) // @PostMapping(value = "/login-success",produces = {"text/plain;charset=UTF-8"}) public String loginSuccess() { return "登陸成功"; } @GetMapping(value = "/r/r1",produces = {"text/plain;charset=UTF-8"}) public String r1() { return "開放的訪問資源r1"; } @GetMapping(value = "/r/r2",produces = {"text/plain;charset=UTF-8"}) public String r2() { return "私有的訪問資源r2"; } }
標簽: