diff --git a/src/main/java/com/example/daemawiki/domain/auth/api/AuthController.java b/src/main/java/com/example/daemawiki/domain/auth/api/AuthController.java index 62ab0bb0..6fa367e8 100644 --- a/src/main/java/com/example/daemawiki/domain/auth/api/AuthController.java +++ b/src/main/java/com/example/daemawiki/domain/auth/api/AuthController.java @@ -1,10 +1,12 @@ package com.example.daemawiki.domain.auth.api; import com.example.daemawiki.domain.auth.dto.LoginRequest; -import com.example.daemawiki.domain.auth.dto.LoginResponse; +import com.example.daemawiki.domain.auth.dto.ReissueRequest; +import com.example.daemawiki.domain.auth.dto.TokenResponse; import com.example.daemawiki.domain.auth.dto.SignupRequest; import com.example.daemawiki.domain.auth.service.Login; import com.example.daemawiki.domain.auth.service.Signup; +import com.example.daemawiki.global.security.Tokenizer; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; @@ -16,14 +18,16 @@ public class AuthController { private final Login loginService; private final Signup signupService; + private final Tokenizer tokenizer; - public AuthController(Login loginService, Signup signupService) { + public AuthController(Login loginService, Signup signupService, Tokenizer tokenizer) { this.loginService = loginService; this.signupService = signupService; + this.tokenizer = tokenizer; } @PostMapping("/login") - public Mono login(@RequestBody LoginRequest request) { + public Mono login(@RequestBody LoginRequest request) { return loginService.execute(request); } @@ -32,4 +36,9 @@ public Mono signup(@RequestBody SignupRequest request) { return signupService.execute(request); } + @PostMapping("/reissue") + public Mono reissue(@RequestBody ReissueRequest request) { + return tokenizer.reissue(request); + } + } diff --git a/src/main/java/com/example/daemawiki/domain/auth/service/Login.java b/src/main/java/com/example/daemawiki/domain/auth/service/Login.java index e583a520..795648f5 100644 --- a/src/main/java/com/example/daemawiki/domain/auth/service/Login.java +++ b/src/main/java/com/example/daemawiki/domain/auth/service/Login.java @@ -1,7 +1,7 @@ package com.example.daemawiki.domain.auth.service; import com.example.daemawiki.domain.auth.dto.LoginRequest; -import com.example.daemawiki.domain.auth.dto.LoginResponse; +import com.example.daemawiki.domain.auth.dto.TokenResponse; import com.example.daemawiki.domain.user.repository.UserRepository; import com.example.daemawiki.global.exception.PasswordMismatchException; import com.example.daemawiki.global.exception.UserNotFoundException; @@ -22,14 +22,14 @@ public Login(UserRepository userRepository, PasswordEncoder passwordEncoder, Tok this.tokenizer = tokenizer; } - public Mono execute(LoginRequest request) { + public Mono execute(LoginRequest request) { return userRepository.findByEmail(request.email()) .switchIfEmpty(Mono.error(UserNotFoundException.EXCEPTION)) .flatMap(user -> Mono.just(user) - .filter(u -> passwordEncoder.matches(u.getPassword(), request.password())) + .filter(u -> passwordEncoder.matches(request.password(), u.getPassword())) .switchIfEmpty(Mono.error(PasswordMismatchException.EXCEPTION)) .flatMap(u -> tokenizer.createToken(u.getEmail()) - .map(token -> LoginResponse.builder() + .map(token -> TokenResponse.builder() .token(token) .build()))); } diff --git a/src/main/java/com/example/daemawiki/global/security/Tokenizer.java b/src/main/java/com/example/daemawiki/global/security/Tokenizer.java index 6c5537c5..aba86b6e 100644 --- a/src/main/java/com/example/daemawiki/global/security/Tokenizer.java +++ b/src/main/java/com/example/daemawiki/global/security/Tokenizer.java @@ -1,9 +1,9 @@ package com.example.daemawiki.global.security; -import io.jsonwebtoken.Claims; -import io.jsonwebtoken.Jws; -import io.jsonwebtoken.Jwts; -import io.jsonwebtoken.SignatureAlgorithm; +import com.example.daemawiki.domain.auth.dto.ReissueRequest; +import com.example.daemawiki.domain.auth.dto.TokenResponse; +import com.example.daemawiki.global.exception.InvalidTokenException; +import io.jsonwebtoken.*; import org.springframework.beans.factory.annotation.Value; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; @@ -28,7 +28,7 @@ public Mono createToken(String user) { private String tokenize(String user) { Calendar calendar = Calendar.getInstance(); - calendar.add(Calendar.HOUR, 2); + calendar.add(Calendar.HOUR, 3); Date expiresAt = calendar.getTime(); Claims claims = Jwts.claims() @@ -72,4 +72,25 @@ public Authentication getAuthentication(String token) { details, null, details.getAuthorities()); } + public Mono reissue(ReissueRequest request) { + try { + Claims claims = parseClaims(request.token()); + String user = claims.getSubject(); + String newToken = tokenize(user); + + return Mono.just(TokenResponse.builder() + .token(newToken) + .build()); + } catch (ExpiredJwtException e) { + String user = e.getClaims().getSubject(); + String newToken = tokenize(user); + + return Mono.just(TokenResponse.builder() + .token(newToken) + .build()); + } catch (JwtException e) { + throw InvalidTokenException.EXCEPTION; + } + } + }