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

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

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

@Configuration
@EnableResourceServer
public class APIConfigurationAdapter extends ResourceServerConfigurerAdapter {

    @Autowired
    private TestEntryPoint entryPoint ;

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/api/oauth2/**").anonymous().and().authorizeRequests()
                .antMatchers("/oauth/**").anonymous().and().authorizeRequests().and()
                .exceptionHandling().authenticationEntryPoint(entryPoint);
    }
...

入口点:

@Component
public class TestEntryPoint implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response,
                         AuthenticationException authException)
            throws IOException,
            ServletException {

        //response.isCommitted() == true

        //response.getStatus() == 500
        response.setStatus(400);
        //response.getStatus() == 500 :(

        //throws IllegalStateException (Cannot call sendError() after the response has been committed)
        response.sendError(400);
    }
}

我不明白原因是什么。

暂无答案!

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

相关问题