Added refresh token endpoint

This commit is contained in:
Filip Izydorczyk
2020-12-23 11:51:17 +01:00
parent 7f630ccdac
commit 61c5a43192
7 changed files with 45 additions and 11 deletions

View File

@ -1,5 +1,7 @@
package com.plannaplan.controllers;
import java.util.UUID;
import com.plannaplan.entities.User;
import com.plannaplan.exceptions.UserNotFoundException;
import com.plannaplan.responses.models.TokenResponse;
@ -61,4 +63,16 @@ public class TokenController {
}
}
@GetMapping("/token/refresh")
@ApiOperation(value = "Endpoint to access new token based on refresh token. It's needed when request with provided token fail with code 403")
public ResponseEntity<TokenResponse> getRefreshToken(
@RequestParam("refreshToken") @ApiParam(value = "Refresh token obtained in /token request") final String refreshToken) {
User user = this.userService.getUserByRefreshToken(refreshToken)
.orElseThrow(() -> new NullPointerException("User not found"));
user.setToken(UUID.randomUUID().toString());
user = this.userService.save(user);
return new ResponseEntity<>(new TokenResponse(user), HttpStatus.OK);
}
}

View File

@ -9,6 +9,8 @@ import io.swagger.annotations.ApiModelProperty;
public class TokenResponse {
@ApiModelProperty(value = "user token used to verify requests")
private String token;
@ApiModelProperty(value = "user token needed to refresh")
private String refreshToken;
@ApiModelProperty(value = "user id in database")
private Long id;
@ApiModelProperty(value = "user app role")
@ -21,6 +23,7 @@ public class TokenResponse {
this.authorityRole = user.getRole().toString();
this.email = user.getEmail();
this.token = user.getToken();
this.refreshToken = user.getRefreshToken();
}
public String getEmail() {
@ -39,4 +42,8 @@ public class TokenResponse {
return token;
}
public String getRefreshToken() {
return this.refreshToken;
}
}

View File

@ -37,9 +37,10 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
public void configure(final WebSecurity webSecurity) {
webSecurity.ignoring().antMatchers("/token**").antMatchers("/api/v1/courses/all")
.antMatchers("/api/v1/groups/course/{id}").antMatchers("/v2/api-docs", "/configuration/ui",
"/swagger-resources/**", "/configuration/security", "/swagger-ui.html", "/webjars/**");
webSecurity.ignoring().antMatchers("/token**").antMatchers("/token/refresh**")
.antMatchers("/api/v1/courses/all").antMatchers("/api/v1/groups/course/{id}")
.antMatchers("/v2/api-docs", "/configuration/ui", "/swagger-resources/**", "/configuration/security",
"/swagger-ui.html", "/webjars/**");
}
@Override