Skip to content

Commit

Permalink
feat: 캐릭터의 정보를 NEXON OPEN API에서 검색해 DB에 저장하는 로직 권한 부여
Browse files Browse the repository at this point in the history
  • Loading branch information
yechan-kim committed Oct 1, 2024
1 parent 360e007 commit 66522ec
Showing 1 changed file with 75 additions and 66 deletions.
141 changes: 75 additions & 66 deletions src/main/java/site/dpbr/dsjs/global/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,70 +31,79 @@
@EnableWebSecurity
@RequiredArgsConstructor
public class SecurityConfig {
private final JwtProvider tokenProvider;

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

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.httpBasic(AbstractHttpConfigurer::disable)
.cors((cors) -> cors
.configurationSource(corsConfigurationSource())
)
.csrf(AbstractHttpConfigurer::disable)
.formLogin(AbstractHttpConfigurer::disable)
.sessionManagement((sessionManagement) ->
sessionManagement.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
);

http
.authorizeHttpRequests((authorize) ->
authorize
.requestMatchers("/v3/api-docs/**", "/swagger-ui/**").permitAll() // API 명세서

.requestMatchers("v1/admin/test-register", "v1/admin/register","v1/admin/login").permitAll() //관리자 로그인
.requestMatchers("v1/admin/refresh").permitAll() // 토큰 재발급

.requestMatchers("v1/character/uploadAndFetch", "v1/character/update", "v1/character/export-characters/**", "v1/character/find-all").hasAnyAuthority(
Role.ROLE_ADMIN.getRole(), Role.ROLE_HEAD_PERSONNEL.getRole(), Role.ROLE_PERSONNEL.getRole(), Role.ROLE_PRESIDENT.getRole(), Role.ROLE_VICE_PRESIDENT.getRole()) // 캐릭터 정보 업로드 및 추출
.requestMatchers("v1/character/search").permitAll() // 캐릭터 정보 검색

.anyRequest().authenticated()
);

http
.exceptionHandling(exceptionHandlingCustomizer ->
exceptionHandlingCustomizer
.authenticationEntryPoint(new HttpStatusEntryPoint(FORBIDDEN))
.accessDeniedHandler(new AccessDeniedHandlerImpl())
);

http
.addFilterBefore(new TokenAuthenticationFilter(tokenProvider),
UsernamePasswordAuthenticationFilter.class)
.addFilterBefore(new ExceptionHandleFilter(),
TokenAuthenticationFilter.class);

return http.build();
}

@Bean
CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(Arrays.asList(
"http://localhost:8080",
"http://maplewind.kro.kr/",
"http://www.maplewind.kro.kr/"));
configuration.setAllowedMethods(Arrays.asList("HEAD", "GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"));
configuration.setAllowCredentials(true);
configuration.setAllowedHeaders(List.of("*"));

UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}
private final JwtProvider tokenProvider;

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

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.httpBasic(AbstractHttpConfigurer::disable)
.cors((cors) -> cors
.configurationSource(corsConfigurationSource())
)
.csrf(AbstractHttpConfigurer::disable)
.formLogin(AbstractHttpConfigurer::disable)
.sessionManagement((sessionManagement) ->
sessionManagement.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
);

http
.authorizeHttpRequests((authorize) ->
authorize
.requestMatchers("/v3/api-docs/**", "/swagger-ui/**")
.permitAll() // API 명세서

.requestMatchers("v1/admin/test-register", "v1/admin/register", "v1/admin/login")
.permitAll() //관리자 로그인
.requestMatchers("v1/admin/refresh")
.permitAll() // 토큰 재발급

.requestMatchers("v1/character/uploadAndFetch", "v1/character/update",
"v1/character/export-characters/**", "v1/character/find-all",
"v1/character/uploadAndFetchAllCharacter")
.hasAnyAuthority(
Role.ROLE_ADMIN.getRole(), Role.ROLE_HEAD_PERSONNEL.getRole(), Role.ROLE_PERSONNEL.getRole(),
Role.ROLE_PRESIDENT.getRole(), Role.ROLE_VICE_PRESIDENT.getRole()) // 캐릭터 정보 업로드 및 추출
.requestMatchers("v1/character/search")
.permitAll() // 캐릭터 정보 검색

.anyRequest()
.authenticated()
);

http
.exceptionHandling(exceptionHandlingCustomizer ->
exceptionHandlingCustomizer
.authenticationEntryPoint(new HttpStatusEntryPoint(FORBIDDEN))
.accessDeniedHandler(new AccessDeniedHandlerImpl())
);

http
.addFilterBefore(new TokenAuthenticationFilter(tokenProvider),
UsernamePasswordAuthenticationFilter.class)
.addFilterBefore(new ExceptionHandleFilter(),
TokenAuthenticationFilter.class);

return http.build();
}

@Bean
CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(Arrays.asList(
"http://localhost:8080",
"http://maplewind.kro.kr/",
"http://www.maplewind.kro.kr/"));
configuration.setAllowedMethods(Arrays.asList("HEAD", "GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"));
configuration.setAllowCredentials(true);
configuration.setAllowedHeaders(List.of("*"));

UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}
}

0 comments on commit 66522ec

Please sign in to comment.