【问题标题】:antMatchers that matches any beginning of path匹配任何路径开头的 antMatchers
【发布时间】:2017-09-27 23:59:52
【问题描述】:

我有用于身份验证的 REST 服务。身份验证端点看起来像/api/v.1/authentication。 API 版本是一个可以更改以反映更新版本的变量。一个例子是/api/v.2/authentication。我喜欢有一个antMatcher 可以处理这两种情况,所以我尝试使用.antMatchers(HttpMethod.POST,"**/authenticate").permitAll() 使用** 来匹配端点的任何开头,但这不起作用。下面的完整设置。

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
        .sessionManagement()
            .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
            .and()
        .csrf().disable()
        .authorizeRequests()
             .antMatchers(HttpMethod.POST, "**/authenticate").permitAll()
             .antMatchers(HttpMethod.GET, "**/get-public-key").permitAll()
             .and()
        .authorizeRequests()
             .antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
             .anyRequest().authenticated();
}

有什么建议可以解决这个问题吗?

【问题讨论】:

    标签: spring spring-boot spring-security


    【解决方案1】:

    你必须使用绝对模式,见AntPathMatcher:

    注意:模式和路径必须都是绝对的或必须都是相对的,才能使两者匹配。因此,建议此实现的用户清理模式,以便在它们前面加上“/”,因为这在使用它们的上下文中是有意义的。

    您修改和简化的配置:

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
            .csrf().disable()
            .authorizeRequests()
                .antMatchers(HttpMethod.POST, "/**/authenticate").permitAll()
                .antMatchers(HttpMethod.GET, "/**/get-public-key").permitAll()
                .antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
                .anyRequest().authenticated();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-12
      • 1970-01-01
      • 2019-05-11
      • 1970-01-01
      • 1970-01-01
      • 2011-03-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多