Я хочу настроить ответ json для исключения BadCredential (ошибка 401 не авторизована) в безопасности Spring.
Текущий 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());
});
}