Skip to content

Commit

Permalink
chore: config 파일 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
sooyoungh committed Oct 11, 2023
1 parent 65a4467 commit b86ef80
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions src/main/java/com/pyonsnalcolor/config/SecurityConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.pyonsnalcolor.config;

import com.pyonsnalcolor.member.security.AuthUserDetailsService;
import com.pyonsnalcolor.member.security.JwtAuthenticationFilter;
import com.pyonsnalcolor.handler.JwtAccessDeniedHandler;
import com.pyonsnalcolor.handler.JwtAuthenticationEntryPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.web.client.RestTemplate;

@EnableWebSecurity
@Configuration
public class SecurityConfig {

@Autowired
JwtAuthenticationFilter jwtAuthenticationFilter;

@Autowired
JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint;

@Autowired
JwtAccessDeniedHandler jwtAccessDeniedHandler;

@Bean
public WebSecurityCustomizer webSecurityCustomizer() {
return (web) -> web.ignoring()
.antMatchers( "/resources/**",
"/v3/api-docs/**",
"/swagger-ui/**",
"/health-check"
);
}

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.httpBasic().disable()
.csrf().disable()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers("/auth/**", "/promotions/**", "/fcm/**", "/manage/**").permitAll()
.antMatchers("/member/**").hasRole("USER")
.anyRequest().authenticated()
.and()
.exceptionHandling((exceptions) -> exceptions
.authenticationEntryPoint(jwtAuthenticationEntryPoint)
.accessDeniedHandler(jwtAccessDeniedHandler))
.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);

return http.build();
}

@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}

@Bean
public UserDetailsService userDetailsService() {
return new AuthUserDetailsService();
}

@Bean
public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) {
return restTemplateBuilder.build();
}
}

0 comments on commit b86ef80

Please sign in to comment.