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此处不用开启,依赖时已经自动开启
相关推荐
码云社区4 分钟前
Java租房平台源码方案:Vue 3房源审核、UniApp预约看房与房东管理
java·vue.js
考虑考虑1 小时前
zookeeper认证
后端·zookeeper
掘金者阿豪1 小时前
数据库选型到底应该看什么?从业务需求到技术架构的实践分析
前端·后端
codigger2 小时前
ThreadLocal 会内存泄露吗?
java·内存泄漏·threadlocal
挖掘狂人2 小时前
ThreadLocal 会内存泄露吗?
java·安全·架构
苍何2 小时前
豆包终于想通了...
后端
AAA修煤气灶刘哥2 小时前
Agent 都能干活了,软件还需要那么多按钮吗?
人工智能·后端·架构
未秃头的程序猿2 小时前
给新模块加了个 @MapperScan,启动崩了,最后发现跟 MapperScan 没关系
java·后端·mybatis
苍何2 小时前
不用 Opus5.5 ,即梦也能出爆款 vibe 视频了
后端
SimonKing2 小时前
WorkBuddy又推出新的限免模型(Hy4 flash)了,羊毛可以薅起来了
java·后端·程序员