Skip to content

Commit

Permalink
fix: security upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
ekoz committed Apr 18, 2024
1 parent 86fd419 commit 6edaadc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 20 deletions.
10 changes: 0 additions & 10 deletions src/main/java/com/ibothub/love/template/config/WebMvcConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

Expand Down Expand Up @@ -41,14 +40,5 @@ public void configureMessageConverters(List<HttpMessageConverter<?>> converters)
converters.add(jackson2HttpMessageConverter);
}

@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedMethods("GET", "POST", "PUT", "DELETE", "HEAD", "OPTIONS")
.allowedOrigins("*")
.allowedHeaders("*")
.allowCredentials(Boolean.TRUE);
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
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.configuration.WebSecurityCustomizer;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.config.annotation.web.configurers.HeadersConfigurer;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
Expand All @@ -26,9 +26,12 @@
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

/**
* @author eko.zhan
*/
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableMethodSecurity
public class WebSecurityConfig {

@Autowired
Expand Down Expand Up @@ -77,15 +80,15 @@ WebSecurityCustomizer webSecurityCustomizer() {
@Bean
SecurityFilterChain configure(HttpSecurity http) throws Exception {
http
.cors(corsCustomizer -> corsCustomizer.configurationSource(corsConfigurationSource()))
.cors(Customizer.withDefaults())
// csrf
.csrf(AbstractHttpConfigurer::disable)
.exceptionHandling(exceptionHandlingCustomizer -> exceptionHandlingCustomizer
.authenticationEntryPoint(new KbsAuthenticationEntryPoint())
.accessDeniedHandler(new KbsAccessDeniedHandler())
)
// X-Frame-Options default is deny
.headers(httpSecurityHeadersConfigurer -> httpSecurityHeadersConfigurer.frameOptions(HeadersConfigurer.FrameOptionsConfig::disable))
// .headers(httpSecurityHeadersConfigurer -> httpSecurityHeadersConfigurer.frameOptions(HeadersConfigurer.FrameOptionsConfig::disable))
.userDetailsService(kbsUserDetailsService)
.anonymous(AbstractHttpConfigurer::disable)
.authorizeHttpRequests(requests -> requests
Expand All @@ -111,25 +114,27 @@ SecurityFilterChain configure(HttpSecurity http) throws Exception {
}

@Bean
CorsConfigurationSource corsConfigurationSource() {
final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
public CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration corsConfiguration = new CorsConfiguration();
// 允许跨域访问的域名
corsConfiguration.addAllowedOrigin("*");
corsConfiguration.addAllowedOriginPattern("*");
// 请求头
corsConfiguration.addAllowedHeader("*");
// 请求方法
corsConfiguration.addAllowedMethod(HttpMethod.DELETE);
corsConfiguration.addAllowedMethod(HttpMethod.POST);
corsConfiguration.addAllowedMethod(HttpMethod.GET);
corsConfiguration.addAllowedMethod(HttpMethod.POST);
corsConfiguration.addAllowedMethod(HttpMethod.PATCH);
corsConfiguration.addAllowedMethod(HttpMethod.PUT);
corsConfiguration.addAllowedMethod(HttpMethod.DELETE);
corsConfiguration.addAllowedMethod(HttpMethod.OPTIONS);
// 预检请求的有效期,单位为秒。
corsConfiguration.setMaxAge(3600L);
// 是否支持安全证书
corsConfiguration.setAllowCredentials(true);

UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", corsConfiguration);
return source;
}

}

0 comments on commit 6edaadc

Please sign in to comment.