fix: 全局异常处理导致Spring Security自定义未认证、未授权异常失效问题修复

This commit is contained in:
haoxr
2023-12-01 18:41:46 +08:00
parent c4463cfcc1
commit e203870849
4 changed files with 14 additions and 6 deletions

View File

@@ -10,6 +10,8 @@ import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
@@ -183,7 +185,12 @@ public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public <T> Result<T> handleException(Exception e) {
public <T> Result<T> handleException(Exception e) throws Exception{
// 将 Spring Security 异常继续抛出,以便交给自定义处理器处理
if (e instanceof AccessDeniedException
|| e instanceof AuthenticationException) {
throw e;
}
log.error("unknown exception: {}", e.getMessage());
return Result.failed(e.getLocalizedMessage());
}
@@ -203,7 +210,7 @@ public class GlobalExceptionHandler {
if (matcher.find()) {
String matchString = matcher.group();
matchString = matchString.replace("[", "").replace("]", "");
matchString = "%s字段类型错误".formatted(matchString.replaceAll("\\\"", ""));
matchString = "%s字段类型错误".formatted(matchString.replaceAll("\"", ""));
group += matchString;
}
return group;

View File

@@ -20,6 +20,6 @@ import java.io.IOException;
public class MyAccessDeniedHandler implements AccessDeniedHandler {
@Override
public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException {
ResponseUtils.writeErrMsg(response, ResultCode.TOKEN_ACCESS_FORBIDDEN);
ResponseUtils.writeErrMsg(response, ResultCode.ACCESS_UNAUTHORIZED);
}
}

View File

@@ -78,7 +78,8 @@ public class JwtTokenProvider {
// claims 中添加角色信息
Set<String> roles = userDetails.getAuthorities().stream()
.map(GrantedAuthority::getAuthority).collect(Collectors.toSet());
.map(GrantedAuthority::getAuthority)
.collect(Collectors.toSet());
claims.put(JwtClaimConstants.AUTHORITIES, roles);
Date now = new Date();
@@ -107,7 +108,7 @@ public class JwtTokenProvider {
userDetails.setDataScope(Convert.toInt(claims.get(JwtClaimConstants.DATA_SCOPE))); // 数据权限范围
// 角色集合
Set<SimpleGrantedAuthority> authorities = ((Set<String>) claims.get(JwtClaimConstants.AUTHORITIES))
Set<SimpleGrantedAuthority> authorities = ((ArrayList<String>) claims.get(JwtClaimConstants.AUTHORITIES))
.stream()
.map(SimpleGrantedAuthority::new)
.collect(Collectors.toSet());

View File

@@ -72,7 +72,7 @@ public class PermissionService {
}
}
if (!hasPermission) {
log.error("用户无访问权限");
log.error("用户无操作权限");
}
return hasPermission;
}