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此处不用开启,依赖时已经自动开启