murraco/spring-boot-jwt

How to obtain refresh token

MRHarrison opened this issue · 3 comments

@murraco How do I obtain a refresh token? I see it mentioned briefly but not sure of how to implement in this auth flow.

I would like to know about that as well. Were you able to figure it out? Thanks

Just add this method to the UserController:

@GetMapping("/refresh")
@PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_CLIENT')")
public String refresh(HttpServletRequest req) {
  return userService.refresh(req.getRemoteUser());
}

And one for the UserService:

public String refresh(String username) {
  return jwtTokenProvider.createToken(username, 
  userRepository.findByUsername(username).getRoles());
}

In this way you will create an endpoint to refresh your current token with a new one. Remember than to access this endpoint you must be authenticated.

Just add this method to the UserController:

@GetMapping("/refresh")
@PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_CLIENT')")
public String refresh(HttpServletRequest req) {
  return userService.refresh(req.getRemoteUser());
}

And one for the UserService:

public String refresh(String username) {
  return jwtTokenProvider.createToken(username, 
  userRepository.findByUsername(username).getRoles());
}

In this way you will create an endpoint to refresh your current token with a new one. Remember than to access this endpoint you must be authenticated.

This solution is different with Refresh token.