【问题标题】:SpringBoot websecurity doesn't ignore a url if it has params如果有参数,SpringBoot websecurity 不会忽略 url
【发布时间】:2019-08-02 03:45:39
【问题描述】:

我试图忽略 SpringBoot 中 WebSecurity 的 url。能够为精确的网址匹配做到这一点。但是如果 url 本身有一个参数,它就不能忽略它。有没有更好的方法来忽略 url,比如忽略特定的控制器。如果没有,如何使用参数?

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/api/v1/something");
    // was able to ignore [url]/api/v1/something but not [url]/api/v1/something?xyz=wjsbjbjbsjbw
}

【问题讨论】:

  • 添加“*”发布你的网址,它会为你工作

标签: spring spring-boot websecurity


【解决方案1】:

尝试使用通配符。

web.ignoring().antMatchers("/api/v1/something/**");

【讨论】:

  • 没有成功,但谢谢。仍然 /api/v1/something 被忽略,但不带参数
【解决方案2】:

HttpSecurity试试吧

@Override
public void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
            .antMatchers("/api/v1/something/**").permitAll();
}

【讨论】:

    【解决方案3】:

    这应该适合你。

    [url]/api/v1/something?xyz=wjsbjbjbsjbw
    

    替换如下 "wjsbjbjbsjbw" = *

    所以新的 URL 将是

    [url]/api/v1/something?xyz=*
    

    / 之后的每个值都可以视为*

    我不确定something 是什么,否则你也可以使用*

    或者其他方式肯定会起作用

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("[url]/api/v1");
    }
    

    详情请见Spring Security Samples

    【讨论】:

      【解决方案4】:

      您也可以使用regexmatchers

      public void configure(WebSecurity web) throws Exception {
            web.ignoring().regexMatchers("/api/v1/something.*");
      }
      

      【讨论】:

        【解决方案5】:

        如果您在当前的 antMatcher 规范之后附加一个星号 (*),您应该可以实现您的目标。

        Per definition:

        映射使用以下规则匹配 URL:

        • ?匹配一个字符
        • * 匹配零个或多个字符
        • ** 匹配路径中的零个或多个目录
        • {spring:[a-z]+} 匹配正则表达式 [a-z]+ 作为名为“spring”的路径变量

        您的代码中重要的 sn-p 可能如下所示:

        @Override
        public void configure(WebSecurity web) throws Exception {
            web.ignoring().antMatchers("/api/v1/something*");
        }
        

        您也可以使用其他重载的 configure 方法和HttpSecurity 类型的参数来实现与路径相关的安全配置,如下所示:

        @Override
        public void configure(HttpSecurity http) throws Exception {
            http.authorizeRequests().antMatchers("/api/v1/something*").permitAll();
        }
        

        如定义中所述,您还可以更进一步,在使用 /** 结束 AntMatcher 时允许该路径下方的任何内容。但这实际上取决于您的用例。 实现细节请看Spring AntPathRequestMatcher


        您的相应请求映射应如下所示(以@GetMapping 为例)。重要的是,您的路径没有尾随 /

        @GetMapping("/api/v1/something")
        public ResponseEntity<String> getSomething(@RequestParam(value = "xyz", required = false) String xyz){
            return ResponseEntity.ok("Called with xyz param: " + xyz);
        }
        

        【讨论】:

          【解决方案6】:

          web.ignoring().antMatchers("/api/v1/something/**"); 应该可以正常工作,但请检查您是否包含web.ignoring().antMatchers("/error/**");,因为我之前遇到了类似的问题,错误端点也正在获得身份验证。希望这会有所帮助。

          【讨论】:

          • 这是问题所在,我尝试调试代码。由于其他代码,错误被抛出。
          猜你喜欢
          • 2016-06-15
          • 2019-09-03
          • 2017-06-11
          • 1970-01-01
          • 2020-10-21
          • 2011-11-06
          • 2019-10-17
          • 1970-01-01
          • 2022-08-13
          相关资源
          最近更新 更多