Skip to content

Commit

Permalink
refactor: 시큐리티 및 토큰 코드 리팩토링
Browse files Browse the repository at this point in the history
- 토큰 전체 코드 리팩토링
- 시큐리티 토큰으로 도메인명 변경
- 몇몇 잘못된 설정 변경

Related to: #95
  • Loading branch information
juwon-code committed Oct 7, 2024
1 parent f44bd5c commit 5016341
Show file tree
Hide file tree
Showing 19 changed files with 427 additions and 291 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.prgrms2.java.bitta.security;
package com.prgrms2.java.bitta.global.config;

import com.prgrms2.java.bitta.token.filter.TokenAuthenticationFilter;
import com.prgrms2.java.bitta.token.util.TokenProvider;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -20,11 +22,10 @@
@EnableWebSecurity
@RequiredArgsConstructor
public class SecurityConfig {

private final JwtTokenProvider jwtTokenProvider;
private final TokenProvider tokenProvider;

@Bean
public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception {
return httpSecurity
.httpBasic(basic -> basic.disable())
.csrf(csrf -> csrf.disable())
Expand All @@ -36,7 +37,7 @@ public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Excepti
.requestMatchers("/v3/api-docs/**", "/swagger-ui.html", "/swagger-ui/**", "/webjars/**").permitAll()
.requestMatchers("/images/**").permitAll()
.anyRequest().authenticated())
.addFilterBefore(new JwtAuthenticationFilter(jwtTokenProvider), UsernamePasswordAuthenticationFilter.class)
.addFilterBefore(new TokenAuthenticationFilter(tokenProvider), UsernamePasswordAuthenticationFilter.class)
.build();
}

Expand Down Expand Up @@ -68,7 +69,6 @@ public CorsConfigurationSource corsConfigurationSource() {

@Bean
public PasswordEncoder passwordEncoder() {
// BCrypt Encoder 사용
return PasswordEncoderFactories.createDelegatingPasswordEncoder();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.prgrms2.java.bitta.global.exception;

public enum AuthenticationException {
CANNOT_ACCESS(403, "해당 API에 대한 액세스 권한이 없습니다.");

private AuthenticationTaskException authenticationTaskException;

AuthenticationException(int code, String message) {
authenticationTaskException = new AuthenticationTaskException(code, message);
}

public AuthenticationTaskException get() {
return authenticationTaskException;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.prgrms2.java.bitta.global.exception;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class AuthenticationTaskException extends RuntimeException {
private int code;
private String message;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.prgrms2.java.bitta.global.util;

import com.prgrms2.java.bitta.member.entity.Role;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.StringUtils;


public class AuthenticationProvider {
public static String getUsername() {
final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

if (authentication == null || authentication.getName() == null) {
throw new RuntimeException("No authentication information.");
}

return authentication.getName();
}

public static Role getRoles() {
final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

if (authentication == null || authentication.getAuthorities().isEmpty()) {
throw new RuntimeException("");
}

String role = authentication.getAuthorities().iterator().next().getAuthority();

return Role.valueOf(role);
}
}

This file was deleted.

This file was deleted.

14 changes: 0 additions & 14 deletions src/main/java/com/prgrms2/java/bitta/security/JwtToken.java

This file was deleted.

145 changes: 0 additions & 145 deletions src/main/java/com/prgrms2/java/bitta/security/JwtTokenProvider.java

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 5016341

Please sign in to comment.