diff --git a/src/main/java/tp/farming_springboot/api/AuthenticateController.java b/src/main/java/tp/farming_springboot/api/AuthenticateController.java index 48ad379..6cb4f33 100644 --- a/src/main/java/tp/farming_springboot/api/AuthenticateController.java +++ b/src/main/java/tp/farming_springboot/api/AuthenticateController.java @@ -7,18 +7,22 @@ import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; +import org.springframework.security.core.Authentication; import org.springframework.web.bind.annotation.*; import tp.farming_springboot.application.dto.response.TokenDto; import tp.farming_springboot.application.dto.request.UserAuthenDto; import tp.farming_springboot.application.dto.request.UserCreateDto; import tp.farming_springboot.application.AuthenticateService; import tp.farming_springboot.application.OtpService; +import tp.farming_springboot.domain.entity.User; +import tp.farming_springboot.domain.repository.UserRepository; import tp.farming_springboot.infra.SmsService; import tp.farming_springboot.application.UserService; import tp.farming_springboot.domain.exception.UserExistsException; import tp.farming_springboot.domain.exception.VerificationException; import java.nio.charset.Charset; +import java.util.Optional; @CrossOrigin @RestController @@ -30,6 +34,7 @@ public class AuthenticateController { private final SmsService smsService; private final AuthenticateService authenticateService; private final UserService userService; + private final UserRepository userRepository; public HttpHeaders HttpHeaderSetting(){ HttpHeaders headers = new HttpHeaders(); @@ -46,6 +51,13 @@ public ResponseEntity temp(@RequestBody UserAuthenDto logger){ } //renew tokens + @GetMapping("/gen-tokens") + public ResponseEntity sendTokens(Authentication authentication){ + Optional user = userRepository.findByPhone(authentication.getName()); + TokenDto tokenDto = authenticateService.getNewTokens(user.get().getPhone()); + ApiResponse message = new ApiResponse(ResultCode.OK,"Generating token success.", tokenDto); + return new ResponseEntity<>(message, HttpHeaderSetting(), HttpStatus.OK); + } //send otp number to user @PostMapping("/request-otp") diff --git a/src/main/java/tp/farming_springboot/config/jwt/AuthTokenFilter.java b/src/main/java/tp/farming_springboot/config/jwt/AuthTokenFilter.java index 52a5df8..e6b0fdf 100644 --- a/src/main/java/tp/farming_springboot/config/jwt/AuthTokenFilter.java +++ b/src/main/java/tp/farming_springboot/config/jwt/AuthTokenFilter.java @@ -17,7 +17,7 @@ import org.springframework.util.StringUtils; import org.springframework.web.filter.OncePerRequestFilter; import tp.farming_springboot.api.ResultCode; - +import java.util.HashMap; @RequiredArgsConstructor @Component @@ -42,12 +42,19 @@ public class AuthTokenFilter extends OncePerRequestFilter { protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws IOException { try { - String jwt = parseJwt(request); - if (jwtUtils.validateJwtToken(jwt)) { + HashMap map = parseJwt(request); + if(map.containsKey("access")){ + String jwt = map.get("access"); + jwtUtils.validateJwtToken(jwt); String username = jwtUtils.getUserNameFromJwtToken(jwt); jwtUtils.createAuthentication(username); } - + else if(map.containsKey("refresh")){ + String jwt = map.get("refresh"); + jwtUtils.validateJwtRefresh(jwt); + String username = jwtUtils.getUserNameFromJwtRefreshToken(jwt); + jwtUtils.createAuthentication(username); + } filterChain.doFilter(request, response); } catch(BadCredentialsException e) { @@ -84,14 +91,19 @@ protected boolean shouldNotFilter(HttpServletRequest request) { return EXCLUDE_URL.stream().anyMatch(exclude -> exclude.equalsIgnoreCase(request.getServletPath())); } - private String parseJwt(HttpServletRequest request) { + private HashMap parseJwt(HttpServletRequest request) { String headerAuth = request.getHeader("Authorization"); - + HashMap map = new HashMap<>(); if (StringUtils.hasText(headerAuth) && headerAuth.startsWith("Bearer ")) { - return headerAuth.substring(7); - } else { + map.put("access",headerAuth.substring(7)); + } + else if(StringUtils.hasText(headerAuth) && headerAuth.startsWith("Refresh ")){ + map.put("refresh",headerAuth.substring(8)); + } + else { throw new BadCredentialsException("토큰 정보가 헤더에 없습니다."); } - + return map; } + } \ No newline at end of file