【问题标题】:.antMatchers("/swagger-ui.html").permitAll() Doesn't work.antMatchers("/swagger-ui.html").permitAll() 不起作用
【发布时间】:2021-07-17 05:20:19
【问题描述】:

我想在我的浏览器上打开 swagger ui。这是我的代码

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/swagger-ui.html").permitAll()                .anyRequest().authenticated().and().httpBasic().and().csrf().disable();
    }

但它不起作用。我还需要输入httpBasic()提供的基本认证

所以我添加以下别人找到的代码

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/v2/api-docs",
                "/configuration/ui",
                "/swagger-resources/**",
                "/configuration/security",
                "/swagger-ui.html",
                "/webjars/**");
    }

现在,我可以访问 localhost:8080/swagger-ui.html 但仍然会弹出 httpBasic 窗口。我可以点击取消关闭窗口并继续使用 swagger ui。但我不知道是什么导致了这个问题

【问题讨论】:

  • 不是真的,请阅读我的问题的第二部分。
  • 基本上我可以打开 swagger UI 但添加代码后 httpBasic() 仍然会弹出
  • 该问题是由主要swagger-ui.html 文件直接或间接引用的辅助文件引起的。 .js.css.png 等文件。您可以在 Web 浏览器的“网络”面板上查看完整列表。例如。在 Firefox 中,按 F12 并选择“网络”选项卡,然后重新加载页面。 Chrome 也有类似的功能。返回状态为 401 Unauthorized 的请求是导致 httpBasic 弹出窗口发生的原因。
  • @Andreas 可能是对的

标签: java spring spring-boot swagger


【解决方案1】:

从哪个控制器调用这个 swagger-ui.html 将该控制器 URL 放入 .antMatchers()。

【讨论】:

    【解决方案2】:

    试试这个-

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .antMatchers("/v2/api-docs", "/configuration/ui", 
                         "/swagger-resources", "/configuration/security", 
                         "/swagger-ui.html", "/webjars/**", "/swagge‌​r-ui.html",
                         "/swagger-resources/configuration/ui", 
                         "/swagger-resources/configuration/security").permitAll() 
            .anyRequest().authenticated()
            .and().httpBasic()
            .and().csrf().disable();
    }
    

    【讨论】:

      猜你喜欢
      • 2018-01-31
      • 1970-01-01
      • 1970-01-01
      • 2019-09-27
      • 2018-03-27
      • 1970-01-01
      • 1970-01-01
      • 2018-06-08
      • 2016-02-16
      相关资源
      最近更新 更多