diff --git a/core/src/main/java/com/example/core/config/_security/SecurityConfig.java b/core/src/main/java/com/example/core/config/_security/SecurityConfig.java index e6c2492..1c9efa6 100644 --- a/core/src/main/java/com/example/core/config/_security/SecurityConfig.java +++ b/core/src/main/java/com/example/core/config/_security/SecurityConfig.java @@ -25,80 +25,95 @@ @Configuration public class SecurityConfig { - @Bean - public Encryption encryption(Environment environment) { - return new AESEncryption(environment); - } - - @Bean - public PasswordEncoder passwordEncoder() { - return new BCryptPasswordEncoder(); - } - - @Bean - public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration) throws Exception { - return authenticationConfiguration.getAuthenticationManager(); - } - - @Bean - public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { - http - .csrf().disable() - - .headers().frameOptions().sameOrigin() - - .and().cors().configurationSource(configurationSource()) - - .and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS) - - .and().formLogin().disable() - - .httpBasic().disable() - - .apply(new SecurityFilterManager()) - - .and().exceptionHandling().authenticationEntryPoint( - (request, response, authException) -> - FilterResponse.unAuthorized(response, new Exception401(ErrorMessage.UN_AUTHORIZED))) - - .and().exceptionHandling().accessDeniedHandler( - (request, response, accessDeniedException) -> - FilterResponse.forbidden(response, new Exception403(ErrorMessage.FORBIDDEN))) - - .and().authorizeRequests( - expressionInterceptUrlRegistry -> - expressionInterceptUrlRegistry - .antMatchers("/api/admin/user/signIn", "/api/user/signin", "/api/user/findPassword", "/api/user/emailCheck").permitAll() - .antMatchers("/api/admin/**").hasRole("ADMIN") - .antMatchers("/api/user/**").hasRole("USER") - .anyRequest().authenticated()); - - return http.build(); - } - - private CorsConfigurationSource configurationSource() { - CorsConfiguration configuration = new CorsConfiguration(); - configuration.addAllowedHeader("*"); - configuration.addAllowedMethod("*"); - configuration.addAllowedOriginPattern("*"); - configuration.setAllowCredentials(true); - configuration.addExposedHeader("Authorization"); - - UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); - source.registerCorsConfiguration("/**", configuration); - - return source; - } - - public static class SecurityFilterManager - extends AbstractHttpConfigurer { - - @Override - public void configure(HttpSecurity builder) throws Exception { - AuthenticationManager authenticationManager = - builder.getSharedObject(AuthenticationManager.class); - builder.addFilter(new JwtAuthenticationFilter(authenticationManager)); - super.configure(builder); - } + @Bean + public Encryption encryption(Environment environment) { + return new AESEncryption(environment); + } + + @Bean + public PasswordEncoder passwordEncoder() { + return new BCryptPasswordEncoder(); + } + + @Bean + public AuthenticationManager authenticationManager( + AuthenticationConfiguration authenticationConfiguration) throws Exception { + return authenticationConfiguration.getAuthenticationManager(); + } + + @Bean + public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { + http.csrf() + .disable() + .headers() + .frameOptions() + .sameOrigin() + .and() + .cors() + .configurationSource(configurationSource()) + .and() + .sessionManagement() + .sessionCreationPolicy(SessionCreationPolicy.STATELESS) + .and() + .formLogin() + .disable() + .httpBasic() + .disable() + .apply(new SecurityFilterManager()) + .and() + .exceptionHandling() + .authenticationEntryPoint( + (request, response, authException) -> + FilterResponse.unAuthorized(response, new Exception401(ErrorMessage.UN_AUTHORIZED))) + .and() + .exceptionHandling() + .accessDeniedHandler( + (request, response, accessDeniedException) -> + FilterResponse.forbidden(response, new Exception403(ErrorMessage.FORBIDDEN))) + .and() + .authorizeRequests( + expressionInterceptUrlRegistry -> + expressionInterceptUrlRegistry + .antMatchers( + "/api/admin/user/signIn", + "/api/user/signin", + "/api/user/signup", + "/api/user/findPassword", + "/api/user/emailCheck") + .permitAll() + .antMatchers("/api/admin/**") + .hasRole("ADMIN") + .antMatchers("/api/user/**") + .hasRole("USER") + .anyRequest() + .authenticated()); + + return http.build(); + } + + private CorsConfigurationSource configurationSource() { + CorsConfiguration configuration = new CorsConfiguration(); + configuration.addAllowedHeader("*"); + configuration.addAllowedMethod("*"); + configuration.addAllowedOriginPattern("*"); + configuration.setAllowCredentials(true); + configuration.addExposedHeader("Authorization"); + + UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); + source.registerCorsConfiguration("/**", configuration); + + return source; + } + + public static class SecurityFilterManager + extends AbstractHttpConfigurer { + + @Override + public void configure(HttpSecurity builder) throws Exception { + AuthenticationManager authenticationManager = + builder.getSharedObject(AuthenticationManager.class); + builder.addFilter(new JwtAuthenticationFilter(authenticationManager)); + super.configure(builder); } + } }