前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【SpringSecurity 】SpringSecurity 自定义登录页面

【SpringSecurity 】SpringSecurity 自定义登录页面

作者头像
陶然同学
发布2023-11-06 08:41:02
1430
发布2023-11-06 08:41:02
举报
文章被收录于专栏:陶然同学博客陶然同学博客

一、配置

代码语言:javascript
复制
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();

    }
}

二、登录控制器

代码语言:javascript
复制
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";
	}
}

三、登录页面

代码语言:javascript
复制
<!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请求!!!

代码语言:javascript
复制
<!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>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2023-11-05,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、配置
  • 二、登录控制器
  • 三、登录页面
  • 三、退出:注意,退出是post请求!!!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档