【发布时间】:2019-05-17 13:10:14
【问题描述】:
我想在 Spring security 中自定义 BadCredential Exception (Error 401 Unauthorized) 的 json 响应。
当前json:
{
"timestamp": 1558062843375,
"status": 401,
"error": "Unauthorized",
"message": "Invalid credentials!",
"path": "/Test/api/v1/consultas/ddjj"
}
新格式:
{
"codigo": "Invalid",
"mensaje": "Invalid credentials!"
}
我尝试向我的安全配置类添加一个身份验证入口点,但它不起作用。我只能捕获 403 错误,但不能捕获 401。
@Configuration
@Order(1)
public class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.antMatcher("/api/**")
.authorizeRequests().anyRequest().authenticated()
.and()
.httpBasic();
http.exceptionHandling().authenticationEntryPoint((request, response, e)
-> {
response.setContentType("application/json;charset=UTF-8");
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
JSONObject respuesta = new JSONObject();
respuesta.put("codigo", "Invalid");
respuesta.put("mensaje", "Invalid credentials!");
response.getWriter().write(respuesta.toJSONString());
});
}
【问题讨论】:
标签: spring-boot spring-security