-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[#11] JWT 설정 #13
Merged
Merged
[#11] JWT 설정 #13
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
355b975
Feat: JWT 초기설저
austinhong22 493c0c2
Feat: JWT service 생성
austinhong22 ee5a2bb
refactor: JWT 불필요한 코드 제거
austinhong22 cffec37
refactor: userservice login 삭제
austinhong22 7a0bcb0
feat: jwtServiceTest 작성
austinhong22 8f90d58
feat: jwt 설정
austinhong22 14dea18
refactor:Security Config 변경
austinhong22 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,14 +2,18 @@ | |
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.whalewatch.domain.User; | ||
import com.whalewatch.dto.TokenResponseDto; | ||
import com.whalewatch.dto.UserDto; | ||
import com.whalewatch.repository.JwtTokenRepository; | ||
import com.whalewatch.repository.UserRepository; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
import org.springframework.test.context.TestPropertySource; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
|
||
import java.util.List; | ||
|
@@ -22,6 +26,11 @@ | |
|
||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
@AutoConfigureMockMvc | ||
@TestPropertySource(properties = { | ||
"jwt.secret-key=zTjEp7AUmDS+bUZKV5OFIVUtFL7EQCMflxiZ3gxpxo0=", | ||
"jwt.access-token-validity-in-seconds=600", | ||
"jwt.refresh-token-validity-in-seconds=1209600" | ||
}) | ||
public class UserControllerTest { | ||
|
||
@Autowired | ||
|
@@ -33,18 +42,27 @@ public class UserControllerTest { | |
@Autowired | ||
private ObjectMapper objectMapper; | ||
|
||
@Autowired | ||
private JwtTokenRepository jwtTokenRepository; | ||
|
||
@Autowired | ||
private PasswordEncoder passwordEncoder; | ||
|
||
@BeforeEach | ||
void setup() { | ||
jwtTokenRepository.deleteAll(); | ||
userRepository.deleteAll(); | ||
userRepository.save(new User("[email protected]", "tester", "1234")); | ||
|
||
String hashed = passwordEncoder.encode("1234"); | ||
userRepository.save(new User("[email protected]", "tester", hashed)); | ||
} | ||
|
||
@Test | ||
void testRegisterUser() throws Exception { | ||
// given | ||
UserDto request = new UserDto(); | ||
request.setEmail("test@test.com"); | ||
request.setUsername("tester"); | ||
request.setEmail("test2@test.com"); | ||
request.setUsername("tester2"); | ||
request.setPassword("1234"); | ||
|
||
// when | ||
|
@@ -53,8 +71,8 @@ void testRegisterUser() throws Exception { | |
.content(objectMapper.writeValueAsString(request))) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.id").exists()) | ||
.andExpect(jsonPath("$.email").value("test@test.com")) | ||
.andExpect(jsonPath("$.username").value("tester")); | ||
.andExpect(jsonPath("$.email").value("test2@test.com")) | ||
.andExpect(jsonPath("$.username").value("tester2")); | ||
|
||
List<User> all = userRepository.findAll(); | ||
assertEquals(2, all.size()); | ||
|
@@ -72,20 +90,63 @@ void testLoginUser() throws Exception { | |
.contentType(MediaType.APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsString(request))) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.id").exists()) | ||
.andExpect(jsonPath("$.email").value("[email protected]")) | ||
.andExpect(jsonPath("$.username").value("tester")); | ||
.andExpect(jsonPath("$.accessToken").exists()) | ||
.andExpect(jsonPath("$.refreshToken").exists()); | ||
} | ||
|
||
@Test | ||
void testGetUserInfo() throws Exception { | ||
// given | ||
User first = userRepository.findAll().get(0); | ||
User user = userRepository.findAll().get(0); | ||
|
||
// 로그인 | ||
UserDto loginRequest = new UserDto(); | ||
loginRequest.setEmail("[email protected]"); | ||
loginRequest.setPassword("1234"); | ||
|
||
String loginResponse = mockMvc.perform(post("/api/users/login") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsString(loginRequest))) | ||
.andExpect(status().isOk()) | ||
.andReturn().getResponse().getContentAsString(); | ||
|
||
TokenResponseDto tokens = objectMapper.readValue(loginResponse, TokenResponseDto.class); | ||
String accessToken = tokens.getAccessToken(); | ||
|
||
// when & then | ||
mockMvc.perform(get("/api/users/" + first.getId())) | ||
mockMvc.perform(get("/api/users/" + user.getId()) | ||
.header("Authorization", "Bearer " + accessToken)) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.email").value("[email protected]")) | ||
.andExpect(jsonPath("$.username").value("tester")); | ||
} | ||
|
||
@Test | ||
void testRefreshToken() throws Exception { | ||
//given | ||
UserDto loginRequest = new UserDto(); | ||
loginRequest.setEmail("[email protected]"); | ||
loginRequest.setPassword("1234"); | ||
|
||
//when | ||
String loginResponse = mockMvc.perform(post("/api/users/login") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsString(loginRequest))) | ||
.andExpect(status().isOk()) | ||
.andReturn().getResponse().getContentAsString(); | ||
|
||
TokenResponseDto tokens = objectMapper.readValue(loginResponse, TokenResponseDto.class); | ||
String refreshToken = tokens.getRefreshToken(); | ||
|
||
//given - accesstoken 재발급 | ||
TokenResponseDto refreshRequest = new TokenResponseDto("", refreshToken); | ||
|
||
// then | ||
mockMvc.perform(post("/api/users/refresh") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsString(refreshRequest))) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.accessToken").exists()) | ||
.andExpect(jsonPath("$.refreshToken").value(refreshToken)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,18 @@ | ||
plugins { | ||
id 'java' | ||
id 'org.springframework.boot' version '3.3.4' | ||
id 'io.spring.dependency-management' version '1.1.6' | ||
} | ||
|
||
dependencies { | ||
// Spring Security | ||
implementation 'org.springframework.boot:spring-boot-starter-security' | ||
|
||
// JWT 설정 | ||
implementation 'io.jsonwebtoken:jjwt-api:0.11.5' | ||
runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.11.5' | ||
runtimeOnly 'io.jsonwebtoken:jjwt-jackson:0.11.5' | ||
|
||
implementation 'org.springframework.boot:spring-boot-starter-web' | ||
testImplementation 'org.springframework.boot:spring-boot-starter-test' | ||
} |
50 changes: 50 additions & 0 deletions
50
common/src/main/java/com/whalewatch/config/JwtAuthentication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.whalewatch.config; | ||
|
||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.util.StringUtils; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
import java.io.IOException; | ||
|
||
public class JwtAuthentication extends OncePerRequestFilter { | ||
|
||
private final JwtTokenProvider tokenProvider; | ||
|
||
public JwtAuthentication(JwtTokenProvider tokenProvider) { | ||
this.tokenProvider = tokenProvider; | ||
} | ||
|
||
@Override | ||
protected void doFilterInternal(HttpServletRequest request, | ||
HttpServletResponse response, | ||
FilterChain filterChain) | ||
throws IOException, jakarta.servlet.ServletException { | ||
|
||
String jwt = resolveToken(request); | ||
|
||
if (StringUtils.hasText(jwt) && tokenProvider.validateToken(jwt)) { | ||
String email = tokenProvider.getEmailFromToken(jwt); | ||
|
||
// DB 조회 후 권한 설정 가능 | ||
UsernamePasswordAuthenticationToken authentication = | ||
new UsernamePasswordAuthenticationToken(email, null, null); | ||
|
||
SecurityContextHolder.getContext().setAuthentication(authentication); | ||
} | ||
|
||
filterChain.doFilter(request, response); | ||
} | ||
|
||
private String resolveToken(HttpServletRequest request) { | ||
String bearerToken = request.getHeader("Authorization"); | ||
// "Authorization: Bearer <토큰값>" 형태일 때 추출 | ||
if (StringUtils.hasText(bearerToken) && bearerToken.startsWith("Bearer ")) { | ||
return bearerToken.substring(7); | ||
} | ||
return null; | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
common/src/main/java/com/whalewatch/config/JwtTokenProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.whalewatch.config; | ||
|
||
import io.jsonwebtoken.JwtException; | ||
import io.jsonwebtoken.Jwts; | ||
import io.jsonwebtoken.SignatureAlgorithm; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Date; | ||
|
||
@Component | ||
public class JwtTokenProvider { | ||
@Value("${jwt.secret-key}") | ||
private String secretKey; | ||
|
||
@Value("${jwt.access-token-validity-in-seconds}") | ||
private long accessTokenValidity; | ||
|
||
@Value("${jwt.refresh-token-validity-in-seconds}") | ||
private long refreshTokenValidity; | ||
|
||
public JwtTokenProvider() { | ||
} | ||
|
||
// Access Token 생성 | ||
public String generateAccessToken(String email) { | ||
Date now = new Date(); | ||
Date expiry = new Date(now.getTime() + accessTokenValidity * 1000); | ||
|
||
return Jwts.builder() | ||
.setSubject(email) | ||
.setIssuedAt(now) | ||
.setExpiration(expiry) | ||
.signWith(SignatureAlgorithm.HS256, secretKey) | ||
.compact(); | ||
} | ||
|
||
// Refresh Token 생성 | ||
public String generateRefreshToken(String email) { | ||
Date now = new Date(); | ||
Date expiry = new Date(now.getTime() + refreshTokenValidity * 1000); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Refresh token의 만료시간이랑 access token의 만료시간이 같으면, refresh를 못하지 않을까요? access token으로 통신하다가 access token이 만료되면, refresh token을 써서 갱신하는 방식일 것 같은데요 ... |
||
|
||
return Jwts.builder() | ||
.setSubject(email) | ||
.setIssuedAt(now) | ||
.setExpiration(expiry) | ||
.signWith(SignatureAlgorithm.HS256, secretKey) | ||
.compact(); | ||
} | ||
|
||
// 토큰에서 Subject 추출 | ||
public String getEmailFromToken(String token) { | ||
return Jwts.parserBuilder() | ||
.setSigningKey(secretKey) | ||
.build() | ||
.parseClaimsJws(token) | ||
.getBody() | ||
.getSubject(); | ||
} | ||
|
||
// 토큰 유효성 검증 | ||
public boolean validateToken(String token) { | ||
try { | ||
Jwts.parserBuilder() | ||
.setSigningKey(secretKey) | ||
.build() | ||
.parseClaimsJws(token); | ||
return true; | ||
} catch (JwtException | IllegalArgumentException e) { | ||
// 유효하지 않은 토큰 | ||
} | ||
return false; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ConfigurationProperties