当前位置: 首页 > 知识库问答 >
问题:

如何允许Swagger绕过WebSecurityConfig?

吴开宇
2023-03-14

我有一个基本配置,如下所示

public class WebSecurityConfig extends WebSecurityConfigurerAdapter

 protected void configure(HttpSecurity httpSecurity) throws Exception {
        // We don't need CSRF for this example
        httpSecurity.csrf().disable()
                // dont authenticate this particular request
                .authorizeRequests().antMatchers("/authenticate").permitAll().
                // all other requests need to be authenticated
                        anyRequest().authenticated().and().
                        // make sure we use stateless session; session won't be used to
                        // store user's state.
                        exceptionHandling().authenticationEntryPoint(jwtAuthenticationEntryPoint).and().sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS);
        // Add a filter to validate the tokens with every request
        httpSecurity.addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class);
    }

所以基本上它允许/通过身份验证,但其他一切都需要token。我如何让它也绕过Swagger?这使得开发变得困难,因为团队依赖于Swagger来查看API文档。

共有1个答案

蒙麒
2023-03-14

使用permitall().在antMatchers中添加swaggerendpoint,或者您可以使用configure(WebSecurity WebSecurity)并使用.ligoring()请参见此处

protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity.csrf().disable()
                .authorizeRequests()
                .antMatchers("/authenticate").permitAll()
                .antMatchers("/v2/api-docs", "/configuration/**", "/swagger*/**", "/webjars/**").permitAll()
                .anyRequest().authenticated().and()
                .exceptionHandling().authenticationEntryPoint(jwtAuthenticationEntryPoint).and().sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS);

        httpSecurity.addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class);
    }
 类似资料:
  • 我使用的是与node.js快递4.12.3和mysql db招摇过市2.0。 我创建了以下模式- 此处删除的_at字段将为空,并且在删除记录之前不会出现在数据库中。我的基于express的nodejs服务器返回的日期如下- [{id:4,“国家”:“g”,“创建时间”:“2018-01-29T04:51:46.000Z”,“删除时间”:null},{id:5,“国家”:“gaaaf”,“创建时间”

  • 注意:这个问题与这里的问题不同,因为我需要它来处理Swagger。 给定一个 FastAPI endpoint,我想允许任意一组 URL 参数,同时保持 Swagger 支持。 我的用例是,我想支持一组类似JSON API的查询参数,例如: 方括号的使用阻止了我使用传统的类来建模查询参数,所以我直接使用对象来代替。但是,我想使用Swagger来测试endpoint。我找不到提供任意URL参数的方法

  • 我正试图通过使用< code>allOf来解决这个swagger API继承的问题。这是我的swagger yaml文件。 当我将其粘贴到在线编辑器中时,我收到以下错误,我很难弄清楚。

  • 问题内容: 我正在创建一个使用webpack-dev-server和react-router进行开发的应用程序。 似乎webpack-dev-server是建立在以下假设的基础上的:您将在一个位置拥有一个公共入口点(即“ /”),而react- router允许无限数量的入口点。 我想要webpack-dev-server的好处,特别是对生产力非常有用的热重载功能,但是我仍然希望能够加载react

  • 问题内容: 我正在对他们设置的平台上的我自己的服务器执行ajax调用,以防止这些ajax调用(但是我需要它从服务器上获取数据以显示从服务器数据库中检索到的数据)。我的ajax脚本正在运行,它可以将数据发送到服务器的php脚本以进行处理。但是由于它被 我无权访问该平台的源/核心。因此我无法删除不允许这样做的脚本。(P / SI使用了Google Chrome浏览器的控制台,并发现了此错误) Ajax

  • 我试图从JS/Ajax向我的WebAPI发出请求时遇到了问题。在我的解决方案中,我有一个发布在srv02:2400上的Web API,我的网站发布在srv02:2300上 当我导航到页面时http://srv02:2300/all-请求。aspx,页面加载正常,除了应该来自我的API的数据 我得到了一个错误: 但是,如果我把url超文本传输协议://srv02:2400/api/请求/查找/1粘贴