java 异常 过滤器_java - 如何管理Spring中过滤器抛出的异常? - 堆栈内存溢出

在阅读上述答案中建议的不同方法后,我决定使用自定义过滤器处理身份验证异常。 我能够使用以下方法使用错误响应类来处理响应状态和代码。

我创建了一个自定义过滤器,并使用addFilterAfter方法修改了我的安全配置,并在CorsFilter类之后添加。

@Component

public class AuthFilter implements Filter {

@Override

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

//Cast the servlet request and response to HttpServletRequest and HttpServletResponse

HttpServletResponse httpServletResponse = (HttpServletResponse) response;

HttpServletRequest httpServletRequest = (HttpServletRequest) request;

// Grab the exception from the request attribute

Exception exception = (Exception) request.getAttribute("javax.servlet.error.exception");

//Set response content type to application/json

httpServletResponse.setContentType(MediaType.APPLICATION_JSON_VALUE);

//check if exception is not null and determine the instance of the exception to further manipulate the status codes and messages of your exception

if(exception!=null && exception instanceof AuthorizationParameterNotFoundException){

ErrorResponse errorResponse = new ErrorResponse(exception.getMessage(),"Authetication Failed!");

httpServletResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);

PrintWriter writer = httpServletResponse.getWriter();

writer.write(convertObjectToJson(errorResponse));

writer.flush();

return;

}

// If exception instance cannot be determined, then throw a nice exception and desired response code.

else if(exception!=null){

ErrorResponse errorResponse = new ErrorResponse(exception.getMessage(),"Authetication Failed!");

PrintWriter writer = httpServletResponse.getWriter();

writer.write(convertObjectToJson(errorResponse));

writer.flush();

return;

}

else {

// proceed with the initial request if no exception is thrown.

chain.doFilter(httpServletRequest,httpServletResponse);

}

}

public String convertObjectToJson(Object object) throws JsonProcessingException {

if (object == null) {

return null;

}

ObjectMapper mapper = new ObjectMapper();

return mapper.writeValueAsString(object);

}

}

SecurityConfig类

@Configuration

public class JwtSecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired

AuthFilter authenticationFilter;

@Override

protected void configure(HttpSecurity http) throws Exception {

http.addFilterAfter(authenticationFilter, CorsFilter.class).csrf().disable()

.cors(); //........

return http;

}

}

ErrorResponse类

public class ErrorResponse {

private final String message;

private final String description;

public ErrorResponse(String description, String message) {

this.message = message;

this.description = description;

}

public String getMessage() {

return message;

}

public String getDescription() {

return description;

}}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值