基于Spring Boot, Spring Security 整合Swagger UI实现在线API文档

一、添加依赖

<dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger2</artifactId>
   <version>2.7.0</version>
</dependency>
<dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger-ui</artifactId>
   <version>2.7.0</version>
</dependency>

二、创建SwaggerWebMvcConfig.java

@Configuration
public class SwaggerWebMvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}

三、创建SwaggerConfig

@Configuration
@EnableWebMvc
@EnableSwagger2
public class SwaggerConfig implements WebMvcConfigurer {

    /**
     * 指定接口基础信息
     *      apiInfo:定义项目描述信息
     *      apis:   指定接口层中的位置
     *
     * @return Docket
     */
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("业务接口")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.yc.practice.mall"))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 指定接口基础信息
     *      apiInfo:定义项目描述信息
     *      apis:   指定接口层中的位置
     *
     *  [将多个controller拼装到一个分组
     *    .apis(Predicates.or(selector1,selector2))   ]
     *  [只监控user相关接口
     *    .paths(PathSelectors.regex("/user.*"))  ]
     *  [为有@Api注解的Controller生成API文档
     *   .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))   ]
     *  [为有@ApiOperation注解的方法生成API文档
     *    .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))  ]
     *
     * @return Docket
     */
    @Bean
    public Docket createSystemRestApi() {
        //分组展示
        Predicate<RequestHandler> selector1 = RequestHandlerSelectors.basePackage("com.yc.practice.system.controller");
        // Predicate<RequestHandler> selector2 = RequestHandlerSelectors.basePackage("com.lh.modules");
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("系统接口")
                .apiInfo(apiInfo())
                .select()
                .apis(Predicates.or(selector1))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 定义项目描述信息
     * @return 定义项目描述信息
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("接口文档")
                .description("Restful风格接口文档")
                .version("1.0")
                .build();
    }

}

四、security配置类中过滤swagger相关信息

@Override
    public void configure(WebSecurity web) {
        web.ignoring().antMatchers( "/swagger-ui.html")
                .antMatchers("/webjars/**")
                .antMatchers("/v2/**")
                .antMatchers("/swagger-resources/**");
    }

五、访问http://ip:port/ swagger-ui.html即可

Spring Security整合Swagger的过程中,我们需要进行以下几个步骤: 1. 配置Spring Security:在Spring Security配置类中,我们可以使用`WebSecurityConfigurerAdapter`来配置权限过滤和访问控制。可以在`configure(HttpSecurity http)`方法中添加`.antMatchers("/swagger-ui.html").permitAll()`来允许Swagger UI页面的访问。这样,Swagger UI页面将不会被Spring Security拦截。 2. 配置静态资源:Swagger UI页面需要访问一些静态资源,例如Swagger API文档UI配置文件。我们可以在Spring Security配置类中使用`WebSecurity.configure(WebSecurity web)`方法来配置这些静态资源的访问权限。可以使用`web.ignoring().antMatchers("/v2/api-docs", "/swagger-resources/configuration/ui", "/swagger-resources", "/swagger-resources/configuration/security", "/swagger-ui.html")`来允许这些静态资源的访问。 3. 添加相关依赖:在项目的pom.xml文件中,我们需要添加Spring SecuritySwagger的相关依赖。可以添加以下依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> ``` 这些依赖将帮助我们实现Spring SecuritySwagger整合。 综上所述,以上是实现Spring Security整合Swagger的方法。通过配置Spring Security和静态资源,以及添加相关依赖,我们可以实现Spring Boot项目中使用Spring Security保护接口并允许Swagger UI的访问[1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值