01 - spring security自定义登录页面

spring security自定义登录页面

文档

  1. 00 - spring security框架使用

使用浏览器自带的登录页面

添加一个配置类WebSecurityConfig.java,后续内容也会在此类中进行配置

java 复制代码
package xin.yangshuai.springsecurity03.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.web.SecurityFilterChain;

@Configuration
// @EnableWebSecurity
public class WebSecurityConfig {

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {

        // 开启授权保护
        http.authorizeRequests(expressionInterceptUrlRegistry -> expressionInterceptUrlRegistry
                // 对所有请求开启授权保护
                .anyRequest()
                // 已经认证的请求会被自动授权
                .authenticated());

        // 使用基本授权方式(浏览器自带的登录页面,无默认登出页)
        http.httpBasic(Customizer.withDefaults());

        return http.build();
    }
}
  • 此方式由浏览器弹出默认登录页面进行登录认证
  • @EnableWebSecurity此处不用开启,依赖时已经自动开启
相关推荐
还是鼠鼠6 小时前
AI掘金头条新闻系统 (Toutiao News)-缓存相关推荐新闻
后端·python·mysql·fastapi·web
我命由我123456 小时前
方差(实例实操、与标准差的区别)
java·数据结构·算法·数据分析·java-ee·intellij-idea·idea
Java内核笔记6 小时前
Spring Security 源码解析(八)方法安全授权与自定义扩展:@EnableMethodSecurity、AOP、SpEL 全链路
java·后端
自由的裙子6 小时前
How-To: Using the N* Stack, part 4
java·jvm·oracle
逝水无殇6 小时前
C# 枚举(Enum)详解
开发语言·后端·c#
什巳7 小时前
JAVA练习275-乘积最大子数组
java·开发语言·数据结构·算法
lang201509287 小时前
Apache POI Word(docx) 实战教程:从入门到精通表格合并、图片插入与文档合并
java·开发语言·word
小牛不牛的程序员7 小时前
利用Claude Code构建自动化需求挖掘工具:从原理到实践
java·算法·自动化
树獭叔叔7 小时前
从 Search 到 Extract:AI Agent 网页工具架构拆解
后端·agent·ai编程
wear工程师7 小时前
腾讯天美一面:从 HashMap 到 Spring 事务,面试官这套连环问到底在考什么
java·面试