From 2f999d7e7e57f7e28fab2ba5280d73c57746ff63 Mon Sep 17 00:00:00 2001 From: kyeong-hyeok Date: Thu, 3 Aug 2023 16:30:33 +0900 Subject: [PATCH] =?UTF-8?q?chore:=20Spring=20Security=20=EC=84=A4=EC=A0=95?= =?UTF-8?q?=20=ED=8C=8C=EC=9D=BC=20=EC=83=9D=EC=84=B1=20(#4)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mapdagu/config/SecurityConfig.java | 76 +++++++++++-------- 1 file changed, 44 insertions(+), 32 deletions(-) diff --git a/src/main/java/com/project/mapdagu/config/SecurityConfig.java b/src/main/java/com/project/mapdagu/config/SecurityConfig.java index 2ff6574..45d2fa6 100644 --- a/src/main/java/com/project/mapdagu/config/SecurityConfig.java +++ b/src/main/java/com/project/mapdagu/config/SecurityConfig.java @@ -1,7 +1,14 @@ package com.project.mapdagu.config; import com.fasterxml.jackson.databind.ObjectMapper; +import com.project.mapdagu.domain.auth.filter.CustomJsonAuthenticationFilter; +import com.project.mapdagu.domain.auth.handler.LoginFailureHandler; +import com.project.mapdagu.domain.auth.handler.LoginSuccessHandler; +import com.project.mapdagu.domain.auth.service.LoginService; import com.project.mapdagu.domain.member.repository.MemberRepository; +import com.project.mapdagu.domain.oauth2.handler.OAuth2LoginFailureHandler; +import com.project.mapdagu.domain.oauth2.handler.OAuth2LoginSuccessHandler; +import com.project.mapdagu.domain.oauth2.service.CustomOAuth2UserService; import com.project.mapdagu.jwt.filter.JwtAuthenticationProcessingFilter; import com.project.mapdagu.jwt.service.JwtService; import com.project.mapdagu.util.RedisUtil; @@ -33,10 +40,10 @@ public class SecurityConfig { private final JwtService jwtService; private final MemberRepository memberRepository; private final ObjectMapper objectMapper; -// private final LoginService loginService; -// private final OAuth2LoginSuccessHandler oAuth2LoginSuccessHandler; -// private final OAuth2LoginFailureHandler oAuth2LoginFailureHandler; -// private final CustomOAuth2UserService customOauth2UserService; + private final LoginService loginService; + private final OAuth2LoginSuccessHandler oAuth2LoginSuccessHandler; + private final OAuth2LoginFailureHandler oAuth2LoginFailureHandler; + private final CustomOAuth2UserService customOauth2UserService; private final RedisUtil redisUtil; @@ -50,15 +57,20 @@ public SecurityFilterChain filterChain(HttpSecurity http, HandlerMappingIntrospe .cors(withDefaults()) .headers(headers -> headers.frameOptions(frameOptions -> frameOptions.disable())) .sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) - .authorizeHttpRequests(request -> request.requestMatchers(mvcMatcherBuilder.pattern("/**")).permitAll() + .authorizeHttpRequests(request -> + request.requestMatchers(mvcMatcherBuilder.pattern("/login")).permitAll() + .requestMatchers(mvcMatcherBuilder.pattern("/sign-up")).permitAll() + .requestMatchers(mvcMatcherBuilder.pattern("/h2-console/**")).permitAll() + .requestMatchers(mvcMatcherBuilder.pattern("/css/**")).permitAll() + .requestMatchers(mvcMatcherBuilder.pattern("/js/**")).permitAll() + .requestMatchers(mvcMatcherBuilder.pattern("/images/**")).permitAll() + .requestMatchers(mvcMatcherBuilder.pattern("/index.html")).permitAll() .anyRequest().authenticated()) -// .oauth2Login(oauth2Login -> oauth2Login.successHandler(oAuth2LoginSuccessHandler) -// .failureHandler(oAuth2LoginFailureHandler) -// .userInfoEndpoint(userInfoEndPoint -> userInfoEndPoint.userService(customOauth2UserService))) -// .addFilterAfter(customJsonUsernamePasswordAuthenticationFilter(), LogoutFilter.class) -// .addFilterBefore(jwtAuthenticationProcessingFilter(), CustomJsonUsernamePasswordAuthenticationFilter.class) -// .exceptionHandling(exception -> exception.accessDeniedHandler(jwtAccessDeniedHandler)) - ; + .oauth2Login(oauth2Login -> oauth2Login.successHandler(oAuth2LoginSuccessHandler) + .failureHandler(oAuth2LoginFailureHandler) + .userInfoEndpoint(userInfoEndPoint -> userInfoEndPoint.userService(customOauth2UserService))) + .addFilterAfter(customJsonUsernamePasswordAuthenticationFilter(), LogoutFilter.class) + .addFilterBefore(jwtAuthenticationProcessingFilter(), CustomJsonAuthenticationFilter.class); return http.build(); } @@ -72,29 +84,29 @@ public PasswordEncoder passwordEncoder() { public AuthenticationManager authenticationManager() { DaoAuthenticationProvider provider = new DaoAuthenticationProvider(); provider.setPasswordEncoder(passwordEncoder()); -// provider.setUserDetailsService(loginService); + provider.setUserDetailsService(loginService); return new ProviderManager(provider); } -// @Bean -// public LoginSuccessHandler loginSuccessHandler() { -// return new LoginSuccessHandler(jwtService, userRepository); -// } -// -// @Bean -// public LoginFailureHandler loginFailureHandler() { -// return new LoginFailureHandler(); -// } -// -// @Bean -// public CustomJsonUsernamePasswordAuthenticationFilter customJsonUsernamePasswordAuthenticationFilter() { -// CustomJsonUsernamePasswordAuthenticationFilter customJsonUsernamePasswordLoginFilter -// = new CustomJsonUsernamePasswordAuthenticationFilter(objectMapper); -// customJsonUsernamePasswordLoginFilter.setAuthenticationManager(authenticationManager()); -// customJsonUsernamePasswordLoginFilter.setAuthenticationSuccessHandler(loginSuccessHandler()); -// customJsonUsernamePasswordLoginFilter.setAuthenticationFailureHandler(loginFailureHandler()); -// return customJsonUsernamePasswordLoginFilter; -// } + @Bean + public LoginSuccessHandler loginSuccessHandler() { + return new LoginSuccessHandler(jwtService, memberRepository); + } + + @Bean + public LoginFailureHandler loginFailureHandler() { + return new LoginFailureHandler(); + } + + @Bean + public CustomJsonAuthenticationFilter customJsonUsernamePasswordAuthenticationFilter() { + CustomJsonAuthenticationFilter customJsonUsernamePasswordLoginFilter + = new CustomJsonAuthenticationFilter(objectMapper); + customJsonUsernamePasswordLoginFilter.setAuthenticationManager(authenticationManager()); + customJsonUsernamePasswordLoginFilter.setAuthenticationSuccessHandler(loginSuccessHandler()); + customJsonUsernamePasswordLoginFilter.setAuthenticationFailureHandler(loginFailureHandler()); + return customJsonUsernamePasswordLoginFilter; + } @Bean public JwtAuthenticationProcessingFilter jwtAuthenticationProcessingFilter() {