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此处不用开启,依赖时已经自动开启
相关推荐
indexsunny几秒前
互联网大厂Java面试实战:Spring Boot与微服务在电商场景的应用解析
java·spring boot·redis·微服务·kafka·gradle·maven
幽络源小助理5 分钟前
SpringBoot+小程序高校素拓分管理系统源码 – 幽络源免费分享
spring boot·后端·小程序
程序员爱钓鱼5 分钟前
Node.js 编程实战:测试与调试 —— 日志与监控方案
前端·后端·node.js
雄大9 分钟前
使用 QWebChannel 实现 JS 与 C++ 双向通信(超详细 + 踩坑总结 + Demo)
后端
smileNicky11 分钟前
Lombok @Data 在 IDEA 中运行报错解决方案
java·ide·intellij-idea
计算机学姐11 分钟前
基于SpringBoot的汉服租赁系统【颜色尺码套装+个性化推荐算法+数据可视化统计】
java·vue.js·spring boot·后端·mysql·信息可视化·推荐算法
回家路上绕了弯12 分钟前
定期归档历史数据实战指南:从方案设计到落地优化
分布式·后端
+VX:Fegn089512 分钟前
计算机毕业设计|基于springboot + vue建筑材料管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
掘金者阿豪13 分钟前
Redis `WRONGTYPE` 错误的原因及解决方法
后端
qq_54702617914 分钟前
Maven 仓库管理
java·maven