-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1796c13
commit 9471d82
Showing
1 changed file
with
104 additions
and
0 deletions.
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
src/main/java/com/project/mapdagu/config/SecurityConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package com.project.mapdagu.config; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.project.mapdagu.domain.member.repository.MemberRepository; | ||
import com.project.mapdagu.jwt.filter.JwtAuthenticationProcessingFilter; | ||
import com.project.mapdagu.jwt.service.JwtService; | ||
import com.project.mapdagu.util.RedisUtil; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.security.authentication.AuthenticationManager; | ||
import org.springframework.security.authentication.ProviderManager; | ||
import org.springframework.security.authentication.dao.DaoAuthenticationProvider; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | ||
import org.springframework.security.config.http.SessionCreationPolicy; | ||
import org.springframework.security.crypto.factory.PasswordEncoderFactories; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
import org.springframework.security.web.SecurityFilterChain; | ||
import org.springframework.security.web.authentication.logout.LogoutFilter; | ||
import org.springframework.security.web.servlet.util.matcher.MvcRequestMatcher; | ||
import org.springframework.web.servlet.handler.HandlerMappingIntrospector; | ||
|
||
import static org.springframework.security.config.Customizer.withDefaults; | ||
|
||
@Slf4j | ||
@Configuration | ||
@EnableWebSecurity | ||
@RequiredArgsConstructor | ||
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 RedisUtil redisUtil; | ||
|
||
|
||
@Bean | ||
public SecurityFilterChain filterChain(HttpSecurity http, HandlerMappingIntrospector introspector) throws Exception { | ||
MvcRequestMatcher.Builder mvcMatcherBuilder = new MvcRequestMatcher.Builder(introspector); | ||
http | ||
.formLogin(formLogin -> formLogin.disable()) | ||
.httpBasic(httpBasic -> httpBasic.disable()) | ||
.csrf(csrf -> csrf.disable()) | ||
.cors(withDefaults()) | ||
.headers(headers -> headers.frameOptions(frameOptions -> frameOptions.disable())) | ||
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) | ||
.authorizeHttpRequests(request -> request.requestMatchers(mvcMatcherBuilder.pattern("/**")).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)) | ||
; | ||
|
||
return http.build(); | ||
} | ||
|
||
@Bean | ||
public PasswordEncoder passwordEncoder() { | ||
return PasswordEncoderFactories.createDelegatingPasswordEncoder(); | ||
} | ||
|
||
@Bean | ||
public AuthenticationManager authenticationManager() { | ||
DaoAuthenticationProvider provider = new DaoAuthenticationProvider(); | ||
provider.setPasswordEncoder(passwordEncoder()); | ||
// 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 JwtAuthenticationProcessingFilter jwtAuthenticationProcessingFilter() { | ||
JwtAuthenticationProcessingFilter jwtAuthenticationProcessingFilter = new JwtAuthenticationProcessingFilter(jwtService, memberRepository, redisUtil); | ||
return jwtAuthenticationProcessingFilter; | ||
} | ||
} |