From a543673e8b15e2c3d34908a5144529d2d03b761b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Weitzel?= Date: Tue, 2 Aug 2022 13:11:19 -0300 Subject: [PATCH] Updated RefreshToken --- pom.xml | 27 ++++++++++----- .../security/controllers/AuthController.java | 17 ++++++++++ .../produc/sup/security/jwt/JwtProvider.java | 33 ++++++++++++++++++- src/main/resources/application.properties | 2 +- 4 files changed, 68 insertions(+), 11 deletions(-) diff --git a/pom.xml b/pom.xml index 56061b8..5b8c1bd 100644 --- a/pom.xml +++ b/pom.xml @@ -39,8 +39,8 @@ javax.xml.bind jaxb-api - - + + @@ -54,15 +54,24 @@ javax.annotation javax.annotation-api - + - - org.springframework.boot - spring-boot-starter-validation - 2.7.0 - - + + org.springframework.boot + spring-boot-starter-validation + 2.7.0 + + + + + + com.nimbusds + nimbus-jose-jwt + 9.22 + + + org.springframework.boot diff --git a/src/main/java/com/api/produc/sup/security/controllers/AuthController.java b/src/main/java/com/api/produc/sup/security/controllers/AuthController.java index 77d9f77..e04654a 100644 --- a/src/main/java/com/api/produc/sup/security/controllers/AuthController.java +++ b/src/main/java/com/api/produc/sup/security/controllers/AuthController.java @@ -1,5 +1,6 @@ package com.api.produc.sup.security.controllers; +import java.text.ParseException; import java.util.HashSet; import java.util.Set; @@ -118,5 +119,21 @@ public ResponseEntity login(@Valid @RequestBody LoginUsuarioDTO loginUsuario, return new ResponseEntity(jwtDto, HttpStatus.OK); } + + + + + @PostMapping("/refresh-token") + public ResponseEntity refreshToken(@RequestBody JwtDTO jwtDto) throws ParseException{ + + String token = jwtProvider.refreshToken(jwtDto); + + JwtDTO jwtRefresh = new JwtDTO(token); + + return new ResponseEntity (jwtRefresh, HttpStatus.OK); + + + } + } diff --git a/src/main/java/com/api/produc/sup/security/jwt/JwtProvider.java b/src/main/java/com/api/produc/sup/security/jwt/JwtProvider.java index 46e9950..e71f8de 100644 --- a/src/main/java/com/api/produc/sup/security/jwt/JwtProvider.java +++ b/src/main/java/com/api/produc/sup/security/jwt/JwtProvider.java @@ -1,5 +1,6 @@ package com.api.produc.sup.security.jwt; +import java.text.ParseException; import java.util.Date; import java.util.List; import java.util.stream.Collectors; @@ -11,7 +12,11 @@ import org.springframework.security.core.GrantedAuthority; import org.springframework.stereotype.Component; +import com.api.produc.sup.security.dto.JwtDTO; import com.api.produc.sup.security.entities.UsuarioDetails; +import com.nimbusds.jwt.JWT; +import com.nimbusds.jwt.JWTClaimsSet; +import com.nimbusds.jwt.JWTParser; import io.jsonwebtoken.ExpiredJwtException; import io.jsonwebtoken.Jwts; @@ -40,7 +45,8 @@ public String generateToken(Authentication authentication){ .setSubject(usuarioPrincipal.getUsername()) .claim("roles", roles) .setIssuedAt(new Date()) - .setExpiration(new Date(new Date().getTime() + expiration * 1000)) + //.setExpiration(new Date(new Date().getTime() + expiration * 1000)) + .setExpiration(new Date(new Date().getTime() + expiration)) .signWith(SignatureAlgorithm.HS512, secret.getBytes()) .compact(); } @@ -66,4 +72,29 @@ public boolean validateToken(String token){ } return false; } + + + public String refreshToken(JwtDTO jwtDto) throws ParseException { + + JWT jwt = JWTParser.parse(jwtDto.getToken()); + + JWTClaimsSet claims = jwt.getJWTClaimsSet(); + + String username = claims.getSubject(); + + //roles pasado desde el .claim de Jwts.builder + List roles = (List)claims.getClaim("roles"); + + //actualizamos un nuevo token + return Jwts.builder() + .setSubject(username) + .claim("roles", roles) + .setIssuedAt(new Date()) + //.setExpiration(new Date(new Date().getTime() + expiration * 1000)) + .setExpiration(new Date(new Date().getTime() + expiration)) + .signWith(SignatureAlgorithm.HS512, secret.getBytes()) + .compact(); + } + + } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 5df91f6..73763a3 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -34,4 +34,4 @@ logging.level.=errors # --- SPRING SECURITY jwt.secret = secret -jwt.expiration = 36000 \ No newline at end of file +jwt.expiration = 20000 \ No newline at end of file