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此处不用开启,依赖时已经自动开启
相关推荐
左左右右左右摇晃10 分钟前
Java并发——synchronized锁
java·开发语言
唐叔在学习15 分钟前
Python桌面端应用最小化托盘开发实践
后端·python·程序员
yuhaiqiang31 分钟前
被 AI 忽悠后,开始怀念搜索引擎了?
前端·后端·面试
sxlishaobin1 小时前
Java I/O 模型详解:BIO、NIO、AIO
java·开发语言·nio
二闹1 小时前
Python文件读取三巨头你该选择哪一个?
后端·python
彭于晏Yan1 小时前
Spring AI(二):入门使用
java·spring boot·spring·ai
有一个好名字1 小时前
vibe codeing 开发流程
java
兑生1 小时前
【灵神题单·贪心】3745. 三元素表达式的最大值 | 排序贪心 | Java
java·开发语言
苏三说技术1 小时前
推荐几个牛逼的AI Agent项目
后端
polaris06301 小时前
Windows操作系统部署Tomcat详细讲解
java·windows·tomcat