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此处不用开启,依赖时已经自动开启
相关推荐
GreenTea4 分钟前
一文搞懂Harness Engineering与Meta-Harness
前端·人工智能·后端
win x1 小时前
Redis 使用~如何在Java中连接使用redis
java·数据库·redis
星晨雪海1 小时前
基于 @Resource 的支付 Service 多实现类完整示例
java·开发语言
阿维的博客日记1 小时前
什么是逃逸分析
java·juc
Ricky_Theseus2 小时前
C++右值引用
java·开发语言·c++
Rick19932 小时前
Java内存参数解析
java·开发语言·jvm
我是大猴子2 小时前
Spring代理类为何依赖注入失效?
java·后端·spring
勿忘,瞬间2 小时前
多线程之进阶修炼
java·开发语言
014-code2 小时前
线程池参数怎么配才不翻车
java
码事漫谈2 小时前
手把手带你部署本地模型,让你Token自由(小白专属)
前端·后端