fix(OAuthExceptionHandler.class):认证异常添加http状态码

This commit is contained in:
有来技术 2021-10-10 01:54:01 +08:00
parent 6124eaeb04
commit 270babffbc

View File

@ -3,11 +3,13 @@ package com.youlai.auth.common.exception;
import com.youlai.common.result.Result; import com.youlai.common.result.Result;
import com.youlai.common.result.ResultCode; import com.youlai.common.result.ResultCode;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.authentication.InternalAuthenticationServiceException; import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.oauth2.common.exceptions.InvalidGrantException; import org.springframework.security.oauth2.common.exceptions.InvalidGrantException;
import org.springframework.security.oauth2.common.exceptions.InvalidTokenException; import org.springframework.security.oauth2.common.exceptions.InvalidTokenException;
import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice @RestControllerAdvice
@ -20,6 +22,7 @@ public class OAuthExceptionHandler {
* @param e * @param e
* @return * @return
*/ */
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(UsernameNotFoundException.class) @ExceptionHandler(UsernameNotFoundException.class)
public Result handleUsernameNotFoundException(UsernameNotFoundException e) { public Result handleUsernameNotFoundException(UsernameNotFoundException e) {
return Result.failed(ResultCode.USER_NOT_EXIST); return Result.failed(ResultCode.USER_NOT_EXIST);
@ -31,9 +34,10 @@ public class OAuthExceptionHandler {
* @param e * @param e
* @return * @return
*/ */
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(InvalidGrantException.class) @ExceptionHandler(InvalidGrantException.class)
public Result handleInvalidGrantException(InvalidGrantException e) { public Result handleInvalidGrantException(InvalidGrantException e) {
return Result.failed(ResultCode.USERNAME_OR_PASSWORD_ERROR); return Result.failed(e.getMessage());
} }
@ -43,6 +47,7 @@ public class OAuthExceptionHandler {
* @param e * @param e
* @return * @return
*/ */
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler({InternalAuthenticationServiceException.class}) @ExceptionHandler({InternalAuthenticationServiceException.class})
public Result handleInternalAuthenticationServiceException(InternalAuthenticationServiceException e) { public Result handleInternalAuthenticationServiceException(InternalAuthenticationServiceException e) {
return Result.failed(e.getMessage()); return Result.failed(e.getMessage());
@ -54,6 +59,7 @@ public class OAuthExceptionHandler {
* @param e * @param e
* @return * @return
*/ */
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler({InvalidTokenException.class}) @ExceptionHandler({InvalidTokenException.class})
public Result handleInvalidTokenExceptionException(InvalidTokenException e) { public Result handleInvalidTokenExceptionException(InvalidTokenException e) {
return Result.failed(e.getMessage()); return Result.failed(e.getMessage());