Skip to content

Commit

Permalink
Updated RefreshToken
Browse files Browse the repository at this point in the history
  • Loading branch information
andresWeitzel committed Aug 2, 2022
1 parent 96cd964 commit a543673
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 11 deletions.
27 changes: 18 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
</dependency>



<!-- CONVERTIR OBJETOS JAVA EN OBJETOS JSON -->
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
Expand All @@ -54,15 +54,24 @@
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
</dependency>

<!-- API PARA LAS VALIDACIONES -->
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-validation -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<version>2.7.0</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<version>2.7.0</version>
</dependency>

<!-- REFRESH TOKEN -->
<!-- https://mvnrepository.com/artifact/com.nimbusds/nimbus-jose-jwt -->
<dependency>
<groupId>com.nimbusds</groupId>
<artifactId>nimbus-jose-jwt</artifactId>
<version>9.22</version>
</dependency>



<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.api.produc.sup.security.controllers;

import java.text.ParseException;
import java.util.HashSet;
import java.util.Set;

Expand Down Expand Up @@ -118,5 +119,21 @@ public ResponseEntity<?> login(@Valid @RequestBody LoginUsuarioDTO loginUsuario,

return new ResponseEntity<JwtDTO>(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<JwtDTO> (jwtRefresh, HttpStatus.OK);


}


}
33 changes: 32 additions & 1 deletion src/main/java/com/api/produc/sup/security/jwt/JwtProvider.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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();
}
Expand All @@ -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<String> roles = (List<String>)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();
}


}
2 changes: 1 addition & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ logging.level.=errors

# --- SPRING SECURITY
jwt.secret = secret
jwt.expiration = 36000
jwt.expiration = 20000

0 comments on commit a543673

Please sign in to comment.