如何进一步停止处理请求?

普里什姆

如果请求中不存在某个标头,我需要阻止所有请求处理。所以,我有以下 SecurityConfig 代码,我在其中配置了一个在其他所有事情之前执行的过滤器:

protected void configure(HttpSecurity httpSecurity) throws Exception {
    httpSecurity.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    
    httpSecurity.addFilterBefore(
        new OncePerRequestFilter() {
            @Override
            protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
                    throws ServletException, IOException {    
                if(testmode) {
                    String testModeHeader = request.getHeader("TestMode");
                    System.out.println("In testmode :"+request.getRequestURI()+" "+testModeHeader);

                    if(!testmodeHeaderValue.equals(testModeHeader)) {
                        response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
                        response.flushBuffer();
                        return;
                    }
                }
                chain.doFilter(request, response);
            }
        }
        , SecurityContextPersistenceFilter.class);
    
    httpSecurity.csrf().disable()
        .authorizeRequests().antMatchers("/oauth/**", "/oauth2/**").permitAll()
        .anyRequest().authenticated()
        .and()
        .oauth2Login()
        .userInfoEndpoint()
            .userService(oauthUserService)
        .and()
        .successHandler(new AuthenticationSuccessHandler() {
            
            @Override
            public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, 
                    Authentication authentication) throws IOException, ServletException {
                ...code not shown...
            }
        })
        .and()
        .sessionManagement()
        .sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}

尽管发送错误代码并使用flushBuffer提交响应,但似乎spring boot仍在将用户重定向到登录页面,如下输出所示:

In testmode :/token null
In testmode :/oauth2/authorization/google null
In testmode :/oauth2/authorization/google null
In testmode :/oauth2/authorization/google null
In testmode :/oauth2/authorization/google null
In testmode :/oauth2/authorization/google null
In testmode :/oauth2/authorization/google null
In testmode :/oauth2/authorization/google null
In testmode :/oauth2/authorization/google null
In testmode :/oauth2/authorization/google null
In testmode :/oauth2/authorization/google null
In testmode :/oauth2/authorization/google null
In testmode :/oauth2/authorization/google null
In testmode :/oauth2/authorization/google null
In testmode :/oauth2/authorization/google null
In testmode :/oauth2/authorization/google null
In testmode :/oauth2/authorization/google null
In testmode :/oauth2/authorization/google null

我需要做什么才能在不通过任何其余过滤器的情况下提交响应?

而且我不确定当我只向 /token 发出一个请求时,向 /oauth2/authorization/google 收到这么多请求是什么?

史蒂夫·里森伯格

您看到的行为是尝试呈现默认错误视图 ( /error) 并结合Spring Security 采用的默认安全原则(参见示例 86)。

由于您在响应中返回 401 状态代码,因此/error正在内部调用视图,从而导致第二次通过过滤器链。由于您的过滤器 extends OncePerRequestFilter,它不会被第二次调用,使其看起来被跳过。

随后,Spring Security 过滤器链的其余部分启动,检测未经授权的/error视图访问,并重定向到/login,或者在您的情况下,因为您使用的是 oauth2 依赖项,/oauth2/authorization/google页面。然后循环无限地继续。

在这种情况下,一个简单的解决方法是通过.mvcMatchers("/error").permitAll()或类似方式公开错误页面值得考虑这是否有意义,或者您是否通过这样做来暴露敏感信息。您可能希望采用另一种技术来处理某些错误,例如Spring Boot 中ErrorViewResolver接口

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章