【问题标题】:Spring Security filter chain does not work for forwarded requestSpring Security 过滤器链不适用于转发的请求
【发布时间】:2021-08-18 08:19:21
【问题描述】:

我的应用程序只需要支持一个 URL。像 http://.../service/api。要执行的操作取决于“ACTION”请求参数

为了解决这个问题,我创建了以下控制器

@RestController
public class Controller {

@PostMapping(path = "/api", params = "ACTION=INIT")
 public String init() {
   return "Inside Initialize";
 }

 @PostMapping(path = "/api", params = "ACTION=FETCH")
 public String fetch() {
   return "Inside Fetch";
 }
 
 @PostMapping(path="/view", param = "!ACTION")
 public String view() {
   return "Inside View";
 }

当缺少 ACTION 参数时将调用 /view。对于前两个请求,我配置了 OAuth 身份验证,后者即 /view 将使用 formlogin。

我创建了一个过滤器,在其中检查 ACTION 参数,如果缺少,我将请求转发到 /view 处理程序。

@Component
public class RouteFilter extends OncePerRequestFilter {

  @Override
  protected void doFilterInternal(HttpServletRequest httpServletRequest,
      HttpServletResponse httpServletResponse, FilterChain filterChain)
      throws ServletException, IOException {
    if(!StringUtils.hasText(httpServletRequest.getParameter("ACTION"))){
      httpServletRequest.getRequestDispatcher("/view").forward(httpServletRequest,httpServletResponse);
    }else {
      filterChain.doFilter(httpServletRequest,httpServletResponse);
    }
  }

下面是过滤器注册。我确保我的过滤器在 FilterChainProxy 之前被调用

@Autowired
private RouteFilter routeFilter;

@Bean
public FilterRegistrationBean<RouteFilter> filter() {
  FilterRegistrationBean<RouteFilter> bean = new FilterRegistrationBean<>();
  bean.setFilter(routeFilter);
  bean.addUrlPatterns("/api");
  bean.setOrder(-100);
  return bean;
}

下面是安全配置

@Configuration
@Order(1)
public class Config extends WebSecurityConfigurerAdapter {

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http
        .authorizeRequests()
        .antMatchers("/api")
        .hasAuthority("SCOPE_API")
        .anyRequest()
        .authenticated()
        .and()
        .oauth2ResourceServer()
        .opaqueToken();
  }
}

@Configuration
@Order(2)
public class Config2 extends WebSecurityConfigurerAdapter {

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http
        .authorizeRequests()
        .antMatchers("/view")
        .permitAll()
        .and()
        .formLogin();
  }
}

当我调用 /service/api 时,RouteFilter 将请求转发到 /view 处理程序,但与 /view 关联的 Spring Security 没有得到尊重。 Spring Security Filter 链是否不适用于转发的请求,或者我在这里遗漏了什么。我使用的是spring-boot 2.4.0版

【问题讨论】:

标签: java spring spring-boot spring-security


【解决方案1】:

here 所述,我们需要将 DispatcherType.FORWARD 添加到 springFilterChain 以拦截转发的请求。上面链接中描述的步骤不起作用,因为 springFilterChain 是由 SecurityAutoConfiguration 创建的。要在其中添加转发调度程序,我们需要将 application.yml 中的属性设置为

security:
    filter:
      dispatcher-types:
        - request
        - async
        - error
        - forward 

设置此属性后,请求被安全过滤器链拦截。

【讨论】:

猜你喜欢
  • 2018-03-14
  • 2020-07-10
  • 2020-12-27
  • 2021-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-30
  • 2011-03-25
相关资源
最近更新 更多