feat:新增2个自定义异常
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package com.xiang.xservice.basic.common.resp;
|
package com.xiang.xservice.basic.common.resp;
|
||||||
|
|
||||||
|
import com.xiang.xservice.basic.exception.code.BaseErrorCode;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
@@ -55,4 +56,8 @@ public class Result<T> {
|
|||||||
public static Result<Void> error(String code, String message) {
|
public static Result<Void> error(String code, String message) {
|
||||||
return new Result<Void>(code, message, null);
|
return new Result<Void>(code, message, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Result<Void> error(BaseErrorCode code) {
|
||||||
|
return new Result<>(code.getCode(), code.getMessage(), null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package com.xiang.xservice.basic.exception;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.xiang.xservice.basic.common.resp.Result;
|
||||||
|
import com.xiang.xservice.basic.exception.code.ErrorCode;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.security.access.AccessDeniedException;
|
||||||
|
import org.springframework.security.web.access.AccessDeniedHandler;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户403鉴权失败异常捕获
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
public class CustomAccessDeniedHandler implements AccessDeniedHandler {
|
||||||
|
@Override
|
||||||
|
public void handle(HttpServletRequest request,
|
||||||
|
HttpServletResponse response,
|
||||||
|
AccessDeniedException accessDeniedException) throws IOException, ServletException {
|
||||||
|
response.setContentType("application/json;charset=UTF-8");
|
||||||
|
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
|
||||||
|
log.error("======用户鉴权失败======");
|
||||||
|
Result<Void> result = Result.error(ErrorCode.USER_DENIED_ACCESS);
|
||||||
|
response.getWriter().write(new ObjectMapper().writeValueAsString(result));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package com.xiang.xservice.basic.exception;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.xiang.xservice.basic.common.resp.Result;
|
||||||
|
import com.xiang.xservice.basic.exception.code.ErrorCode;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.security.core.AuthenticationException;
|
||||||
|
import org.springframework.security.web.AuthenticationEntryPoint;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户未鉴权异常处理器
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint {
|
||||||
|
@Override
|
||||||
|
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
|
||||||
|
response.setContentType("application/json;charset=UTF-8");
|
||||||
|
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
|
||||||
|
log.error("========用户未进行验证鉴权==========");
|
||||||
|
Result<Void> result = Result.error(ErrorCode.NOT_AUTHORIZATION);
|
||||||
|
response.getWriter().write(new ObjectMapper().writeValueAsString(result));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,6 +10,8 @@ public enum ErrorCode implements BaseErrorCode {
|
|||||||
ERROR("-1", "系统繁忙,请稍后再试!"),
|
ERROR("-1", "系统繁忙,请稍后再试!"),
|
||||||
PARAM_ERROR("-2", "请求参数异常!"),
|
PARAM_ERROR("-2", "请求参数异常!"),
|
||||||
JSON_ERROR("-3", "JSON异常!"),
|
JSON_ERROR("-3", "JSON异常!"),
|
||||||
|
USER_DENIED_ACCESS("-4", "用户权限不足!"),
|
||||||
|
NOT_AUTHORIZATION("-5", "用户未进行系统认证!")
|
||||||
;
|
;
|
||||||
|
|
||||||
private final String code;
|
private final String code;
|
||||||
|
|||||||
Reference in New Issue
Block a user