添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
配置
@Configuration
@EnableWebSecurity
@EnableMethodSecurity(securedEnabled = true)
public class SecurityConfiguration {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.csrf().disable()
.authorizeHttpRequests()
.requestMatchers(HttpMethod.OPTIONS, "/**").permitAll()
.requestMatchers(new AntPathRequestMatcher("/h2-console/**")).permitAll()
.anyRequest().authenticated();
return http.build();
}
}