11package com .codingapi .springboot .security ;
22
33import com .codingapi .springboot .security .configurer .HttpSecurityConfigurer ;
4- import com .codingapi .springboot .security .dto .request .LoginRequest ;
54import com .codingapi .springboot .security .filter .*;
65import com .codingapi .springboot .security .handler .ServletExceptionHandler ;
76import com .codingapi .springboot .security .jwt .Jwt ;
2524import org .springframework .web .servlet .config .annotation .CorsRegistry ;
2625import org .springframework .web .servlet .config .annotation .WebMvcConfigurer ;
2726
28- import javax .servlet .http .HttpServletRequest ;
29- import javax .servlet .http .HttpServletResponse ;
30-
3127@ Configuration
3228@ EnableMethodSecurity
3329public class AutoConfiguration {
@@ -66,21 +62,20 @@ public HandlerExceptionResolver servletExceptionHandler() {
6662 @ Bean
6763 @ ConditionalOnMissingBean
6864 public SecurityLoginHandler securityLoginHandler (){
69- return new SecurityLoginHandler () {
70- @ Override
71- public void preHandle (HttpServletRequest request , HttpServletResponse response , LoginRequest handler ) throws Exception {
65+ return (request , response , handler ) -> {
7266
73- }
7467 };
7568 }
7669
7770 @ Bean
7871 @ ConditionalOnMissingBean
79- public SecurityFilterChain filterChain (HttpSecurity http , Jwt jwt ,SecurityLoginHandler loginHandler , SecurityJwtProperties properties ) throws Exception {
72+ public SecurityFilterChain filterChain (HttpSecurity http , Jwt jwt ,SecurityLoginHandler loginHandler ,
73+ SecurityJwtProperties properties ) throws Exception {
8074 //before add addCorsMappings to enable cors.
8175 http .cors ();
82-
83- http .csrf ().disable ();
76+ if (properties .isDisableCsrf () ){
77+ http .csrf ().disable ();
78+ }
8479 http .apply (new HttpSecurityConfigurer (jwt ,loginHandler ,properties ));
8580 http
8681 .exceptionHandling ()
@@ -108,7 +103,8 @@ public SecurityFilterChain filterChain(HttpSecurity http, Jwt jwt,SecurityLoginH
108103
109104 @ Bean
110105 @ ConditionalOnMissingBean
111- public AuthenticationProvider authenticationProvider (UserDetailsService userDetailsService , PasswordEncoder passwordEncoder ) {
106+ public AuthenticationProvider authenticationProvider (UserDetailsService userDetailsService ,
107+ PasswordEncoder passwordEncoder ) {
112108 DaoAuthenticationProvider provider = new DaoAuthenticationProvider ();
113109 provider .setUserDetailsService (userDetailsService );
114110 provider .setPasswordEncoder (passwordEncoder );
@@ -124,17 +120,20 @@ public Jwt jwt(SecurityJwtProperties properties) {
124120
125121
126122 @ Bean
127- public WebMvcConfigurer corsConfigurer () {
123+ public WebMvcConfigurer corsConfigurer (SecurityJwtProperties securityJwtProperties ) {
128124 return new WebMvcConfigurer () {
129125 @ Override
130126 public void addCorsMappings (CorsRegistry registry ) {
131- registry .addMapping ("/**" )
132- .allowedHeaders ("*" )
133- .allowedMethods ("*" )
134- .exposedHeaders ("Authorization" , "x-xsrf-token" , "Access-Control-Allow-Headers" , "Origin" , "Accept,X-Requested-With" ,
135- "Content-Type" , "Access-Control-Request-Method" , "Access-Control-Request-Headers" )
136- .maxAge (1800L )
137- .allowedOrigins ("*" );
127+ if (securityJwtProperties .isDisableCors ()) {
128+ registry .addMapping ("/**" )
129+ .allowedHeaders ("*" )
130+ .allowedMethods ("*" )
131+ .exposedHeaders ("Authorization" , "x-xsrf-token" , "Access-Control-Allow-Headers" , "Origin" ,
132+ "Accept,X-Requested-With" , "Content-Type" , "Access-Control-Request-Method" ,
133+ "Access-Control-Request-Headers" )
134+ .maxAge (1800L )
135+ .allowedOrigins ("*" );
136+ }
138137 }
139138 };
140139 }
0 commit comments