Spring security simple implementation

This commit is contained in:
myh 2023-11-29 17:25:46 +08:00
parent 05918dd80b
commit 82b58576b4

View File

@ -0,0 +1,31 @@
package com.example.springdemo.security;
import org.jetbrains.annotations.NotNull;
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.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.config.annotation.web.configurers.LogoutConfigurer;
import org.springframework.security.web.SecurityFilterChain;
@Configuration
@EnableWebSecurity // Enable Spring Security
public class DefaultSecurityConfigure {
@Bean
public SecurityFilterChain defaultSecurityFilterChain(@NotNull HttpSecurity http) throws Exception {
var ignoreUrls = new String[]{""};
var authedUrls = new String[]{"/users"};
http.authorizeHttpRequests(
(req) -> req.requestMatchers(ignoreUrls).permitAll()
)
.authorizeHttpRequests(
(req) -> req.requestMatchers(authedUrls).authenticated()
)
.formLogin(Customizer.withDefaults())
.csrf(AbstractHttpConfigurer::disable)
.logout(LogoutConfigurer::permitAll);
return http.build();
}
}