Skip to content

Commit

Permalink
fix: jwt subject 변경으로 인한 Security 코드 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
jihwan2da committed Oct 21, 2023
1 parent bfc7bd3 commit 77358de
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 16 deletions.
11 changes: 4 additions & 7 deletions src/main/java/com/gloddy/server/auth/jwt/JwtAuthentication.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package com.gloddy.server.auth.jwt;


import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.security.auth.Subject;
import java.util.Collection;
Expand All @@ -15,10 +12,10 @@
public class JwtAuthentication implements Authentication {

private final JwtUserAdapter userDetails;
private final String phoneNumber;
private final String userId;

public static Authentication of(JwtUserAdapter userDetails, String phoneNumber) {
return new JwtAuthentication(userDetails, phoneNumber);
public static Authentication of(JwtUserAdapter userDetails, String userId) {
return new JwtAuthentication(userDetails, userId);
}


Expand Down Expand Up @@ -54,7 +51,7 @@ public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentExce

@Override
public String getName() {
return phoneNumber;
return userId.toString();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public String extractToken(HttpServletRequest request, String headerKey) {
return request.getHeader(headerKey);
}

public String extractPhoneNumberFromToken(String token, String key) {
public String extractSubjectFromToken(String token, String key) {
Claims claims = getClaims(token, key);
return claims.getSubject();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public String getPassword() {

@Override
public String getUsername() {
return user.getPhone().getPhoneNumber();
return user.getId().toString();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
throw new UserBusinessException(TOKEN_BLANK);
}

String phoneNumber = jwtTokenExtractor.extractPhoneNumberFromToken(token, KEY);
Authentication authentication = JwtAuthentication.of(null, phoneNumber);
String userId = jwtTokenExtractor.extractSubjectFromToken(token, KEY);
Authentication authentication = JwtAuthentication.of(null, userId);

Authentication authenticated = authenticationProvider.authenticate(authentication);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@ public class UserDetailsServiceImpl implements UserDetailsService {
private final UserQueryHandler userQueryHandler;

@Override
public UserDetails loadUserByUsername(String phoneNumber) throws UsernameNotFoundException {

User user = userQueryHandler.findByPhone(new Phone(phoneNumber))
.orElseThrow(() -> new UserBusinessException(ErrorCode.USER_NOT_FOUND));

public UserDetails loadUserByUsername(String userId) throws UsernameNotFoundException {
User user = userQueryHandler.findById(Long.parseLong(userId));
return JwtUserAdapter.from(user);
}
}

0 comments on commit 77358de

Please sign in to comment.