【发布时间】:2021-11-15 13:46:43
【问题描述】:
如果请求中不存在某个标头,我需要阻止所有请求处理。所以,我有以下 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 发出这么多请求是什么?
【问题讨论】:
标签: spring-boot spring-security spring-security-oauth2 spring-filter