SpringBoot+SpringSecurity集成Swagger3与Knife4j

博客介绍了Swagger3和knife4j的配置过程,包括导入相关依赖、编写Swagger3配置文件、修改SecurityConfig配置文件以放行静态资源,还提及了非必需的yml配置,最后给出了文档访问路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.导入配置添加Swagger3依赖与knife4j依赖

  <!--添加Swagger3依赖-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>
        <!--knife4j(接口文档)-->
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-spring-boot-starter</artifactId>
            <version>3.0.3</version>
        </dependency>

2.Swagger3配置文件


import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
/**
 * @author :jerry
 * @date :Created in 2022/5/28 10:30
 * @description:
 * @version: V1.1
 *  *Swagger3API文档的配置
 *  knife4j文档地址(端口号根据自己项目配置): http://localhost:8081/doc.html#
 *  swagger文档地址(端口号根据自己项目配置):http://localhost:8081/swagger-ui/index.html#/
 */
@Configuration
@EnableOpenApi
@EnableKnife4j
public class Swagger3Config {

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.OAS_30)
//                .groupName("webApi")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.yl.controller"))
                .paths(PathSelectors.any())
                .build();
    }


    @Bean
    public ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("SwaggerUI接口文档")
                .description("接口文档Swagger-Bootstrap版")
                .termsOfServiceUrl("http://localhost:8081/swagger-ui/index.html#/")
                .contact(new Contact("jerry","http://localhost:8081/doc.html#", "13258239832@163.com"))
                .version("1.0")
                .license("jerry")
                .build();
    }
}

3.修改SecurityConfig配置文件(放行静态资源)

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter implements WebMvcConfigurer {

private static final String[] URL_WHITELIST = {

			
			"/favicon.ico",
			"/webjars/**",
			"/img.icons/**",
			"/swagger-resources/**",
			"/v3/api-docs/**",
			"/swagger-ui/**",
			"/doc.html",


	};



	protected void configure(HttpSecurity http) throws Exception {
            *
            *

        	// 配置拦截规则
				.and()
				.authorizeRequests()
				.antMatchers(URL_WHITELIST).permitAll()
				.anyRequest().authenticated()
    }


    @Bean
	@Override
	public AuthenticationManager authenticationManagerBean() throws Exception {
		return super.authenticationManagerBean();
	}

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

		registry.addResourceHandler("/webjars/**")
				.addResourceLocations("classpath:/META-INF/resources/webjars/");
		registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
	}
     
}

4.yml配置(非必需配置)

#swagger文档开启/关闭
springfox:
  documentation:
    auto-startup: true
#knife4j
knife4j:
  production: false # 开启/屏蔽文档资源

文档访问路径:

knife4j文档地址(端口号根据自己项目配置): http://localhost:8081/doc.html#
swagger文档地址(端口号根据自己项目配置):http://localhost:8081/swagger-ui/index.html#/

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值