Skip to content
This repository has been archived by the owner on Nov 27, 2024. It is now read-only.

Commit

Permalink
토큰 재발급 추가 및 클래스명 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
ori0o0p committed Jan 31, 2024
1 parent 05050ba commit edc2fcf
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<LoginResponse> login(@RequestBody LoginRequest request) {
public Mono<TokenResponse> login(@RequestBody LoginRequest request) {
return loginService.execute(request);
}

Expand All @@ -32,4 +36,9 @@ public Mono<Void> signup(@RequestBody SignupRequest request) {
return signupService.execute(request);
}

@PostMapping("/reissue")
public Mono<TokenResponse> reissue(@RequestBody ReissueRequest request) {
return tokenizer.reissue(request);
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -22,14 +22,14 @@ public Login(UserRepository userRepository, PasswordEncoder passwordEncoder, Tok
this.tokenizer = tokenizer;
}

public Mono<LoginResponse> execute(LoginRequest request) {
public Mono<TokenResponse> 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())));
}
Expand Down
31 changes: 26 additions & 5 deletions src/main/java/com/example/daemawiki/global/security/Tokenizer.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -28,7 +28,7 @@ public Mono<String> 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()
Expand Down Expand Up @@ -72,4 +72,25 @@ public Authentication getAuthentication(String token) {
details, null, details.getAuthorities());
}

public Mono<TokenResponse> 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;
}
}

}

0 comments on commit edc2fcf

Please sign in to comment.