Skip to content

Commit

Permalink
[#152] Fix: 회원가입 권한 403 문제 해결
Browse files Browse the repository at this point in the history
  • Loading branch information
tkguswls1106 committed Jun 14, 2024
1 parent b690831 commit 6935183
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,13 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
.authorizeHttpRequests(authorizeRequests -> {
authorizeRequests
.requestMatchers(HttpMethod.OPTIONS, "/**").permitAll()
.requestMatchers(HttpMethod.PUT, "/signup").hasAuthority("ROLE_GUEST") // '/signup' api는 ROLE_GUEST 권한 로그인 사용자만 사용 가능. 이는 DB 속성값 & 헤더의 jwt 토큰에 등록해둔 권한도 바꾸어 재발급 받아야 한다.

// .requestMatchers("/", "/error", "/favicon.ico", "/v3/api-docs/**", "/swagger-ui/**", "/swagger/**", "/health").permitAll()
// .requestMatchers("/ws/**", "/oauth2/**", "/reissue", "/signup").permitAll();
.requestMatchers("/**").permitAll(); // Test 용도
// .requestMatchers("/**").permitAll() // Test 용도
.requestMatchers("/", "/error", "/favicon.ico", "/swagger-ui/**", "/v3/api-docs/**", "/swagger/**", "/health").permitAll()
.requestMatchers("/ws/**", "/oauth2/**", "/reissue").permitAll()

.anyRequest().hasAnyAuthority("ROLE_USER", "ROLE_ADMIN"); // permit 지정한 경로들 외에는 전부 USER나 ADMIN 권한이 있어야지 url을 이용 가능하다. (GUEST 불가능)
})

.exceptionHandling(exceptionHandling -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
@RequiredArgsConstructor
public class OAuth2LoginSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {

private final TokenProvider tokenProvider;
private final AuthService authService;
private final TokenProvider tokenProvider;


@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import com.sajang.devracebackend.service.AuthService;
import com.sajang.devracebackend.service.AwsS3Service;
import com.sajang.devracebackend.service.UserService;
import com.sajang.devracebackend.util.SecurityUtil;
import io.jsonwebtoken.JwtException;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.Authentication;
Expand Down Expand Up @@ -46,8 +45,8 @@ public AuthDto.SignupResponse signup(MultipartFile imageFile, AuthDto.SignupRequ
User user = userService.findLoginUser();

// 회원가입 권한 예외처리 (signup은 Role이 GUEST인 사용자만 이용가능한 API임.)
boolean isHasGuestRole = SecurityUtil.isHasRole(Role.ROLE_GUEST.name());
if(isHasGuestRole == false || !user.getRole().equals(Role.ROLE_GUEST) || user.getBojId() != null) {
if(!user.getRole().equals(Role.ROLE_GUEST)) {
// 이 로직을 SecurityConfig의 hasAuthority("ROLE_GUEST") 외에도 여기 또 써줘야하는 이유는,
// reissue로 인한 재발급 이후에도 이전 엑세스 토큰으로 '/signup' 경로에 다시 접근할 경우, 토큰 내의 권한은 GUEST가 맞겠지만 DB 내의 권한은 USER이기에 이러한 비정상적인 접근을 방지할 수 있기 때문임.
throw new Exception400.UserBadRequest("이미 가입완료 되어있는 사용자입니다.");
}
Expand Down
10 changes: 0 additions & 10 deletions src/main/java/com/sajang/devracebackend/util/SecurityUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;

@Slf4j
Expand All @@ -18,13 +17,4 @@ public static Long getCurrentMemberId() { // 현재 로그인중인 사용자
}
return Long.parseLong(authentication.getName());
}

public static boolean isHasRole(String roleName) {
final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

if (authentication == null) {
throw new RuntimeException("Security Context에 인증 정보가 없습니다.");
}
return authentication.getAuthorities().contains(new SimpleGrantedAuthority(roleName));
}
}

0 comments on commit 6935183

Please sign in to comment.