Skip to content

Commit

Permalink
chore: Spring Security 설정 파일 생성 (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyeong-hyeok committed Aug 3, 2023
1 parent 4643c8d commit 2f999d7
Showing 1 changed file with 44 additions and 32 deletions.
76 changes: 44 additions & 32 deletions src/main/java/com/project/mapdagu/config/SecurityConfig.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;


Expand All @@ -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();
}
Expand All @@ -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() {
Expand Down

0 comments on commit 2f999d7

Please sign in to comment.