【SpringSecurity 】SpringSecurity 自定义登录页面

一、配置

java 复制代码
package com.boot.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.web.SecurityFilterChain;

/**
 * @EnableWebSecurity:SpringSecurity的配置类 开启SpringSecurity【自带大量过滤器链:责任链模式】
 */
@Configuration //
@EnableWebSecurity //5.x中@EnableWebSecurity自带@Configuration
public class SecurityConfig {
    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        return http
                .authorizeHttpRequests(authorizeHttpRequests-> //在这个后面开始配置URL相关的【URL访问权限控制相关的】
                        authorizeHttpRequests.requestMatchers("/login").permitAll() //permitAll:授予所有权限【匿名可以访问的、不用登录就可以访问】
                                .anyRequest() //任何的请求
                                .authenticated() //需要认证【登录】后才能访问
                )

                .formLogin(formLogin->
                        formLogin.loginPage("/login") //登录页面
                                .loginProcessingUrl("/login").permitAll() //登录接口可以匿名访问
                                .defaultSuccessUrl("/index") //登录成功访问/index页面

                )
                .csrf(Customizer.withDefaults()) //关闭跨域漏洞攻击防护
                .logout(logout->logout.deleteCookies("JSESSIONID").invalidateHttpSession(true).logoutSuccessUrl("/index")) //退出登录接口
                .build();

    }
}

二、登录控制器

java 复制代码
package security03.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class LoginController {

	@GetMapping("/login")
	public String login(){
		return "login";
	}
}

三、登录页面

html 复制代码
<!DOCTYPE html>
<html xmlns:th="https://www.thymeleaf.org">
    <head>
        <title>请登录</title>
    </head>
    <body>
        <div>
            <form th:action="@{/login}" method="post">
                <p>
                    <span>用户名:</span>
                    <input type="text" id="username" name="username">
                </p>
                <p>
                    <span>密码:</span>
                    <input type="password" id="password" name="password">
                </p>

                <!-- 不使用 th:action 属性 和 不关闭csrf 的情况下,需要放开下面的标签 -->
                <!--<input th:name="${_csrf.parameterName}" type="hidden" th:value="${_csrf.token}"/>-->

                <input type="submit" value="登录" />
            </form>
        </div>
    </body>
</html>

三、退出:注意,退出是post请求!!!

html 复制代码
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>系统首页</title>
</head>
<body>
<h1 style="background-color: goldenrod">欢迎访问系统</h1>
<form th:action="@{/logout}" method="post">
    <input type="submit" value="退出系统"/>
</form>
</body>
</html>
相关推荐
金銀銅鐵4 小时前
带您看懂字节码指令(番外二):递归里有什么——三层 fact 帧怎么叠起来
java·jvm·后端
两点王爷6 小时前
Java 面试高频:JWT、Cookie、Session、Token 鉴权详解
java·开发语言·面试
悟道书生7 小时前
hashMap/hashSet/hashCode详解
java
nice_lcj5209 小时前
依赖注入的三种方式:构造器、Setter、字段怎么选
java·spring·面试·依赖注入·字段注入·构造器注入
噢,我明白了9 小时前
实际项目导出方式应用
java·状态模式·导出
جيون داد ناالام ميづ10 小时前
七、MyBatis 源码剖析:核心流程拆解(初始化 + SQL 执行)
java·sql·mybatis
ClkLog-开源埋点用户分析10 小时前
【产品说明】【埋点分析】ClkLog 商业版 Java 运行环境安全更新说明
java·开发语言
秋饼11 小时前
Spring Framework 7 内置弹性实战:@Retryable 语义翻转、虚拟线程流量放大与 LLM 网关治理
java·ai·技术分享·后端开发
RuoyiOffice11 小时前
Flowable只是起点:一套OA背后的单据、资源与协作设计
java·vue.js·spring boot·flowable·企业管理·oa·ruoyi office