无法使用自定义身份验证入口点更改响应Spring Boot

whitzsjs  于 2021-09-30  发布在  Java
关注(0)|答案(0)|浏览(572)

我试图在spring boot 1.5.22上的应用程序中返回自定义身份验证错误消息。
问题在于,在重写的“开始”方法中,不可能更改响应。response.iscommitted()始终返回true。因此,不可能更改响应的正文或标题。。。
连接自定义入口点:

  1. @Configuration
  2. @EnableResourceServer
  3. public class APIConfigurationAdapter extends ResourceServerConfigurerAdapter {
  4. @Autowired
  5. private TestEntryPoint entryPoint ;
  6. @Override
  7. public void configure(HttpSecurity http) throws Exception {
  8. http.authorizeRequests()
  9. .antMatchers("/api/oauth2/**").anonymous().and().authorizeRequests()
  10. .antMatchers("/oauth/**").anonymous().and().authorizeRequests().and()
  11. .exceptionHandling().authenticationEntryPoint(entryPoint);
  12. }
  13. ...

入口点:

  1. @Component
  2. public class TestEntryPoint implements AuthenticationEntryPoint {
  3. @Override
  4. public void commence(HttpServletRequest request, HttpServletResponse response,
  5. AuthenticationException authException)
  6. throws IOException,
  7. ServletException {
  8. //response.isCommitted() == true
  9. //response.getStatus() == 500
  10. response.setStatus(400);
  11. //response.getStatus() == 500 :(
  12. //throws IllegalStateException (Cannot call sendError() after the response has been committed)
  13. response.sendError(400);
  14. }
  15. }

我不明白原因是什么。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题