1
1
package com .codingapi .springboot .security ;
2
2
3
3
import com .codingapi .springboot .security .configurer .HttpSecurityConfigurer ;
4
- import com .codingapi .springboot .security .dto .request .LoginRequest ;
5
4
import com .codingapi .springboot .security .filter .*;
6
5
import com .codingapi .springboot .security .handler .ServletExceptionHandler ;
7
6
import com .codingapi .springboot .security .jwt .Jwt ;
25
24
import org .springframework .web .servlet .config .annotation .CorsRegistry ;
26
25
import org .springframework .web .servlet .config .annotation .WebMvcConfigurer ;
27
26
28
- import javax .servlet .http .HttpServletRequest ;
29
- import javax .servlet .http .HttpServletResponse ;
30
-
31
27
@ Configuration
32
28
@ EnableMethodSecurity
33
29
public class AutoConfiguration {
@@ -66,21 +62,20 @@ public HandlerExceptionResolver servletExceptionHandler() {
66
62
@ Bean
67
63
@ ConditionalOnMissingBean
68
64
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 ) -> {
72
66
73
- }
74
67
};
75
68
}
76
69
77
70
@ Bean
78
71
@ 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 {
80
74
//before add addCorsMappings to enable cors.
81
75
http .cors ();
82
-
83
- http .csrf ().disable ();
76
+ if (properties .isDisableCsrf () ){
77
+ http .csrf ().disable ();
78
+ }
84
79
http .apply (new HttpSecurityConfigurer (jwt ,loginHandler ,properties ));
85
80
http
86
81
.exceptionHandling ()
@@ -108,7 +103,8 @@ public SecurityFilterChain filterChain(HttpSecurity http, Jwt jwt,SecurityLoginH
108
103
109
104
@ Bean
110
105
@ ConditionalOnMissingBean
111
- public AuthenticationProvider authenticationProvider (UserDetailsService userDetailsService , PasswordEncoder passwordEncoder ) {
106
+ public AuthenticationProvider authenticationProvider (UserDetailsService userDetailsService ,
107
+ PasswordEncoder passwordEncoder ) {
112
108
DaoAuthenticationProvider provider = new DaoAuthenticationProvider ();
113
109
provider .setUserDetailsService (userDetailsService );
114
110
provider .setPasswordEncoder (passwordEncoder );
@@ -124,17 +120,20 @@ public Jwt jwt(SecurityJwtProperties properties) {
124
120
125
121
126
122
@ Bean
127
- public WebMvcConfigurer corsConfigurer () {
123
+ public WebMvcConfigurer corsConfigurer (SecurityJwtProperties securityJwtProperties ) {
128
124
return new WebMvcConfigurer () {
129
125
@ Override
130
126
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
+ }
138
137
}
139
138
};
140
139
}
0 commit comments