安全过滤器链中的API注解被Swagger阻止,只能通过Postman接受访问。
创始人
2024-11-04 20:00:21
0

要解决Swagger阻止安全过滤器链中的API注解的问题,并允许通过Postman进行访问,可以采取以下步骤:

  1. 确保在Spring Boot项目中已经正确配置了Swagger。

  2. 创建一个自定义注解,用于标记需要在Swagger中显示的API。例如,可以创建一个@SwaggerAnnotation注解。

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface SwaggerAnnotation {
}
  1. 在需要在Swagger中显示的API方法上使用@SwaggerAnnotation注解。
@RestController
public class MyController {

    @SwaggerAnnotation
    @GetMapping("/api/mypath")
    public String myMethod() {
        // API逻辑
    }
}
  1. 创建一个自定义的Swagger配置类,用于配置Swagger的行为,并在该类中将带有@SwaggerAnnotation注解的API方法添加到Swagger的文档中。
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(SwaggerAnnotation.class))
                .build()
                .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        // Swagger文档信息
    }
}
  1. 配置Spring Security以允许访问Swagger的相关路径。可以通过创建一个WebSecurityConfigurerAdapter的子类来实现。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private Environment environment;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/swagger-ui.html", "/swagger-resources/**", "/v2/api-docs", "/webjars/**")
                .permitAll()
                .and()
            .authorizeRequests()
                .anyRequest()
                .authenticated()
                .and()
            .csrf()
                .disable();
        
        // 如果还有其他安全配置,请在此处添加
    }
}
  1. 确保在Pom.xml文件中添加了Swagger和Spring Security的相关依赖。

  2. 启动应用程序,并使用Postman进行访问。现在,Swagger应该不会阻止进入安全过滤器链中带有@SwaggerAnnotation注解的API,并且只有需要身份验证的API才会受到Spring Security的限制。

相关内容

热门资讯

避免在粘贴双引号时向VS 20... 在粘贴双引号时向VS 2022添加反斜杠的问题通常是由于编辑器的自动转义功能引起的。为了避免这个问题...
安装apache-beam==... 出现此错误可能是因为用户的Python版本太低,而apache-beam==2.34.0需要更高的P...
Android Recycle... 要在Android RecyclerView中实现滑动卡片效果,可以按照以下步骤进行操作:首先,在项...
安装了Anaconda之后找不... 在安装Anaconda后,如果找不到Jupyter Notebook,可以尝试以下解决方法:检查环境...
omi系统和安卓系统哪个好,揭... OMI系统和安卓系统哪个好?这个问题就像是在问“苹果和橘子哪个更甜”,每个人都有自己的答案。今天,我...
原生ios和安卓系统,原生对比... 亲爱的读者们,你是否曾好奇过,为什么你的iPhone和安卓手机在操作体验上有着天壤之别?今天,就让我...
Android - 无法确定任... 这个错误通常发生在Android项目中,表示编译Debug版本的Java代码时出现了依赖关系问题。下...
Android - NDK 预... 在Android NDK的构建过程中,LOCAL_SRC_FILES只能包含一个项目。如果需要在ND...
Akka生成Actor问题 在Akka框架中,可以使用ActorSystem对象生成Actor。但是,当我们在Actor类中尝试...
Agora-RTC-React... 出现这个错误原因是因为在 React 组件中使用,import AgoraRTC from “ago...