mirror of https://gitee.com/maxjhandsome/pig
🐛 Fixing a bug. closed #I44U16 token失效后 没有json错误返回 而是报500
This commit is contained in:
parent
9cf7913447
commit
80e1b9f610
|
@ -65,10 +65,11 @@ public class PigWebResponseExceptionTranslator implements WebResponseExceptionTr
|
||||||
return handleOAuth2Exception(new InvalidException(ase.getMessage(), ase));
|
return handleOAuth2Exception(new InvalidException(ase.getMessage(), ase));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// token 过期 特殊处理 返回 424 不是 401
|
||||||
ase = (InvalidTokenException) throwableAnalyzer.getFirstThrowableOfType(InvalidTokenException.class,
|
ase = (InvalidTokenException) throwableAnalyzer.getFirstThrowableOfType(InvalidTokenException.class,
|
||||||
causeChain);
|
causeChain);
|
||||||
if (ase != null) {
|
if (ase != null) {
|
||||||
return handleOAuth2Exception(new UnauthorizedException(ase.getMessage(), ase));
|
return handleOAuth2Exception(new TokenInvalidException(ase.getMessage(), ase));
|
||||||
}
|
}
|
||||||
|
|
||||||
ase = (HttpRequestMethodNotSupportedException) throwableAnalyzer
|
ase = (HttpRequestMethodNotSupportedException) throwableAnalyzer
|
||||||
|
|
|
@ -22,6 +22,7 @@ import com.pig4cloud.pig.common.core.constant.CommonConstants;
|
||||||
import com.pig4cloud.pig.common.core.util.R;
|
import com.pig4cloud.pig.common.core.util.R;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.SneakyThrows;
|
import lombok.SneakyThrows;
|
||||||
|
import org.springframework.security.authentication.InsufficientAuthenticationException;
|
||||||
import org.springframework.security.core.AuthenticationException;
|
import org.springframework.security.core.AuthenticationException;
|
||||||
import org.springframework.security.web.AuthenticationEntryPoint;
|
import org.springframework.security.web.AuthenticationEntryPoint;
|
||||||
|
|
||||||
|
@ -45,12 +46,18 @@ public class ResourceAuthExceptionEntryPoint implements AuthenticationEntryPoint
|
||||||
response.setCharacterEncoding(CommonConstants.UTF8);
|
response.setCharacterEncoding(CommonConstants.UTF8);
|
||||||
response.setContentType(CommonConstants.CONTENT_TYPE);
|
response.setContentType(CommonConstants.CONTENT_TYPE);
|
||||||
R<String> result = new R<>();
|
R<String> result = new R<>();
|
||||||
result.setCode(HttpStatus.HTTP_UNAUTHORIZED);
|
result.setCode(CommonConstants.FAIL);
|
||||||
|
response.setStatus(HttpStatus.HTTP_UNAUTHORIZED);
|
||||||
if (authException != null) {
|
if (authException != null) {
|
||||||
result.setMsg("error");
|
result.setMsg("error");
|
||||||
result.setData(authException.getMessage());
|
result.setData(authException.getMessage());
|
||||||
}
|
}
|
||||||
response.setStatus(HttpStatus.HTTP_UNAUTHORIZED);
|
|
||||||
|
// 针对令牌过期返回特殊的 424
|
||||||
|
if (authException instanceof InsufficientAuthenticationException) {
|
||||||
|
response.setStatus(org.springframework.http.HttpStatus.FAILED_DEPENDENCY.value());
|
||||||
|
result.setMsg("token expire");
|
||||||
|
}
|
||||||
PrintWriter printWriter = response.getWriter();
|
PrintWriter printWriter = response.getWriter();
|
||||||
printWriter.append(objectMapper.writeValueAsString(result));
|
printWriter.append(objectMapper.writeValueAsString(result));
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,47 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2018-2025, lengleng All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
*
|
||||||
|
* Redistributions of source code must retain the above copyright notice,
|
||||||
|
* this list of conditions and the following disclaimer.
|
||||||
|
* Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* Neither the name of the pig4cloud.com developer nor the names of its
|
||||||
|
* contributors may be used to endorse or promote products derived from
|
||||||
|
* this software without specific prior written permission.
|
||||||
|
* Author: lengleng (wangiegie@gmail.com)
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.pig4cloud.pig.common.security.exception;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||||
|
import com.pig4cloud.pig.common.security.component.PigAuth2ExceptionSerializer;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author lengleng
|
||||||
|
* @date 2021-08-05
|
||||||
|
* <p>
|
||||||
|
* 令牌不合法
|
||||||
|
*/
|
||||||
|
@JsonSerialize(using = PigAuth2ExceptionSerializer.class)
|
||||||
|
public class TokenInvalidException extends PigAuth2Exception {
|
||||||
|
|
||||||
|
public TokenInvalidException(String msg, Throwable t) {
|
||||||
|
super(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getOAuth2ErrorCode() {
|
||||||
|
return "invalid_token";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getHttpErrorCode() {
|
||||||
|
return HttpStatus.FAILED_DEPENDENCY.value();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue