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此处不用开启,依赖时已经自动开启
相关推荐
挽安6213 分钟前
Spring Boot自动配置原理:从@EnableAutoConfiguration源码一步步看懂
后端
深入云栈11 分钟前
Netty 4.2.x 源码深度解析 (十二):NIO传输——NioSocketChannel与NioServerSocketChannel的IO读写实现
后端
王的宝库20 分钟前
GO常用标准库包
开发语言·后端·golang
php@king23 分钟前
hyperf初步认识和安装
后端
LEE35 分钟前
别再堆 AGENTS.md 了:前端团队如何把 AI Coding 做成一套可执行的工程系统
前端·后端
geovindu36 分钟前
python: Face Recognition
开发语言·后端·python·人脸识别
我命由我1234542 分钟前
Windows 操作系统 - 启用 D 盘虚拟内存
java·运维·服务器·windows·学习·java-ee·运维开发
工具派1 小时前
markdown在线编辑器怎么选?渲染管线的3个坑和md转PDF跑版记录
前端·后端
xcl09251 小时前
智慧场馆解决方案系统源码:从架构设计到部署实战
java·spring boot
liangbo71 小时前
06-JVM垃圾回收器之CMS
java·jvm