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此处不用开启,依赖时已经自动开启
相关推荐
咸鱼2.06 分钟前
【java入门到放弃】XXL-JOB
java
爱滑雪的码农7 分钟前
Java基础十一 流(Stream)、文件(File)和IO
java·开发语言·python
叶小鸡9 分钟前
Java 篇-项目实战-天机学堂(从0到1)-day11
java·开发语言
light blue bird16 分钟前
MES/ERP 工序 BOM 协同多节点工站组件
java·jvm·oracle
用户94161469336518 分钟前
Python 实时监控 A 股行情并自动筛选强势股(REST + WebSocket 两种方案)
后端·数据分析
Pkmer18 分钟前
古法编程: 适配器模式
java·设计模式
Java编程爱好者19 分钟前
吃透 ForkJoinPool:工作窃取底层原理,一次性讲透
后端
longxibo25 分钟前
【Flowable 7.2 源码深度解析与实战】
java·后端·流程图
norq juox25 分钟前
Spring 中集成Hibernate
java·spring·hibernate