最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

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

2023-08-29 17:37 作者:雪虬出云  | 我要投稿

對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/>
        密&nbsp;&nbsp;&nbsp;碼:<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";
    }
}



尚硅谷SpringSecurity框架教程(spring security源碼剖的評論 (共 條)

分享到微博請遵守國家法律
怀远县| 涪陵区| 银川市| 依安县| 贵阳市| 吴桥县| 肃宁县| 道真| 广元市| 柞水县| 日照市| 黄龙县| 天峻县| 凉山| 龙江县| 石景山区| 巫溪县| 神农架林区| 和顺县| 武鸣县| 铁岭市| 花垣县| 连平县| 云阳县| 牟定县| 慈利县| 绍兴县| 右玉县| 涿鹿县| 余姚市| 湘西| 长治市| 芷江| 凤冈县| 康乐县| 彭山县| 鸡泽县| 崇阳县| 扶余县| 黄骅市| 永丰县|