Some minor refactoring of auth-related classes.
This commit is contained in:
parent
e4bf81d14f
commit
026e3a7ab2
@ -28,10 +28,7 @@ public final class AuthController {
|
|||||||
this.authService = authService;
|
this.authService = authService;
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/login")
|
private ResponseEntity<LoginView> getLoginViewResponseEntity(LoginDetails loginDetails) {
|
||||||
public ResponseEntity<LoginView> login(@RequestBody LoginBody loginBody) {
|
|
||||||
try {
|
|
||||||
final LoginDetails loginDetails = this.authService.login(loginBody.getUsername(), loginBody.getPassword());
|
|
||||||
final AuthToken refreshToken = loginDetails.getRefreshToken();
|
final AuthToken refreshToken = loginDetails.getRefreshToken();
|
||||||
final ResponseCookie refreshCookie = getRefreshTokenCookie(
|
final ResponseCookie refreshCookie = getRefreshTokenCookie(
|
||||||
refreshToken.getToken(),
|
refreshToken.getToken(),
|
||||||
@ -43,6 +40,13 @@ public final class AuthController {
|
|||||||
return ResponseEntity.ok()
|
return ResponseEntity.ok()
|
||||||
.header(HttpHeaders.SET_COOKIE, refreshCookie.toString())
|
.header(HttpHeaders.SET_COOKIE, refreshCookie.toString())
|
||||||
.body(loginView);
|
.body(loginView);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/login")
|
||||||
|
public ResponseEntity<LoginView> login(@RequestBody LoginBody loginBody) {
|
||||||
|
try {
|
||||||
|
final LoginDetails loginDetails = this.authService.login(loginBody.getUsername(), loginBody.getPassword());
|
||||||
|
return this.getLoginViewResponseEntity(loginDetails);
|
||||||
} catch (LoginException loginException) {
|
} catch (LoginException loginException) {
|
||||||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
|
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
|
||||||
}
|
}
|
||||||
@ -54,15 +58,7 @@ public final class AuthController {
|
|||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
final LoginDetails loginDetails = this.authService.refresh(oldRefreshToken);
|
final LoginDetails loginDetails = this.authService.refresh(oldRefreshToken);
|
||||||
final AuthToken newRefreshToken = loginDetails.getRefreshToken();
|
return this.getLoginViewResponseEntity(loginDetails);
|
||||||
final ResponseCookie refreshCookie = getRefreshTokenCookie(
|
|
||||||
newRefreshToken.getToken(),
|
|
||||||
newRefreshToken.getLifetime()
|
|
||||||
);
|
|
||||||
final var loginView = new LoginView(loginDetails.getUsername(), loginDetails.getAccessToken().getToken());
|
|
||||||
return ResponseEntity.ok()
|
|
||||||
.header(HttpHeaders.SET_COOKIE, refreshCookie.toString())
|
|
||||||
.body(loginView);
|
|
||||||
} catch (LoginException loginException) {
|
} catch (LoginException loginException) {
|
||||||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
|
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,6 @@ package app.mealsmadeeasy.api.jwt;
|
|||||||
|
|
||||||
import app.mealsmadeeasy.api.security.AuthToken;
|
import app.mealsmadeeasy.api.security.AuthToken;
|
||||||
import app.mealsmadeeasy.api.security.SimpleAuthToken;
|
import app.mealsmadeeasy.api.security.SimpleAuthToken;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
||||||
import io.jsonwebtoken.JwtException;
|
import io.jsonwebtoken.JwtException;
|
||||||
import io.jsonwebtoken.Jwts;
|
import io.jsonwebtoken.Jwts;
|
||||||
import io.jsonwebtoken.io.Serializer;
|
import io.jsonwebtoken.io.Serializer;
|
||||||
@ -23,7 +22,6 @@ public final class JwtServiceImpl implements JwtService {
|
|||||||
private final SecretKey secretKey;
|
private final SecretKey secretKey;
|
||||||
|
|
||||||
public JwtServiceImpl(
|
public JwtServiceImpl(
|
||||||
ObjectMapper objectMapper,
|
|
||||||
@Value("${app.mealsmadeeasy.api.security.access-token-lifetime}") Long accessTokenLifetime,
|
@Value("${app.mealsmadeeasy.api.security.access-token-lifetime}") Long accessTokenLifetime,
|
||||||
SecretKey secretKey
|
SecretKey secretKey
|
||||||
) {
|
) {
|
||||||
|
@ -2,7 +2,8 @@ package app.mealsmadeeasy.api.security;
|
|||||||
|
|
||||||
import app.mealsmadeeasy.api.jwt.JwtService;
|
import app.mealsmadeeasy.api.jwt.JwtService;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import io.jsonwebtoken.security.SecurityException;
|
import io.jsonwebtoken.ExpiredJwtException;
|
||||||
|
import io.jsonwebtoken.JwtException;
|
||||||
import jakarta.servlet.FilterChain;
|
import jakarta.servlet.FilterChain;
|
||||||
import jakarta.servlet.ServletException;
|
import jakarta.servlet.ServletException;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
@ -37,6 +38,12 @@ public final class JwtFilter extends OncePerRequestFilter {
|
|||||||
this.objectMapper = objectMapper;
|
this.objectMapper = objectMapper;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void handleSecurityException(HttpServletResponse response, int status, String message) throws IOException {
|
||||||
|
final SecurityExceptionView view = new SecurityExceptionView(status, message);
|
||||||
|
response.setStatus(status);
|
||||||
|
response.getWriter().write(this.objectMapper.writeValueAsString(view));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
|
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
|
||||||
throws ServletException, IOException {
|
throws ServletException, IOException {
|
||||||
@ -51,11 +58,16 @@ public final class JwtFilter extends OncePerRequestFilter {
|
|||||||
final String username;
|
final String username;
|
||||||
try {
|
try {
|
||||||
username = this.jwtService.getSubject(token);
|
username = this.jwtService.getSubject(token);
|
||||||
} catch (SecurityException e) {
|
} catch (ExpiredJwtException expiredJwtException) {
|
||||||
logger.error("Error while getting username from token.", e);
|
this.handleSecurityException(
|
||||||
final SecurityExceptionView view = new SecurityExceptionView(401, e.getMessage());
|
response,
|
||||||
response.setStatus(401);
|
HttpServletResponse.SC_UNAUTHORIZED,
|
||||||
response.getWriter().write(this.objectMapper.writeValueAsString(view));
|
expiredJwtException.getMessage()
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
} catch (JwtException jwtException) {
|
||||||
|
logger.error("Error while getting username from token.", jwtException);
|
||||||
|
this.handleSecurityException(response, HttpServletResponse.SC_UNAUTHORIZED, jwtException.getMessage());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
final UserDetails userDetails = this.userDetailsService.loadUserByUsername(username);
|
final UserDetails userDetails = this.userDetailsService.loadUserByUsername(username);
|
||||||
|
Loading…
Reference in New Issue
Block a user