springboot+spring security配置swagger2
這里springboot整合springsecurity就不說了,上篇文章就有:https://www.cnblogs.com/qiantao/p/14605154.html
springboot版本:2.3.7、swagger2版本:2.9.2
1、pom.xml檔案中添加相關依賴
<!-- SpringSecurity 安 全 框 架 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <!-- swagger2 接 口 文 檔 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <!-- swagger2 UI --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency>
2、目錄

3、SwaggerConfig配置類
/** * @Author qt * @Date 2021/4/8 * @Description Swagger2介面配置 */ @Configuration @EnableSwagger2// 該注解開啟Swagger2的自動配置 public class SwaggerConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .pathMapping("/") .select() //api掃描的Controller包名 .apis(RequestHandlerSelectors.basePackage("com.qt.springfashionsys.controller")) .paths(PathSelectors.any()) .build() .apiInfo(apiInfo()); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("API介面檔案")//標題 .description("服裝管理系統")//描述 .termsOfServiceUrl("qt個人demo")//服務的團隊 .contact(new Contact("qt", "blog.csdn.net", "[email protected]"))//作者的資訊 .license("The Apache License")//授權資訊 .licenseUrl("http://www.baidu.com")//授權的url .version("1.0.0")//版本號 .build(); } }
4、swagger在Controller介面中添加檔案說明
/** * @Author qt * @Date 2021/3/25 * @Description */ @Api(value = "服裝管理業務介面", tags = { "服裝管理業務介面" }, hidden = true) @Controller @RequestMapping("/user") public class UserInfoController { private Logger logger = LoggerFactory.getLogger(getClass()); @Resource private UserInfoService userInfoService; /** * 根據用戶名獲取用戶資訊 * @param username 用戶名 * @return 用戶資訊 */ @ApiOperation(value = "XXX介面描述") @ApiImplicitParams({ @ApiImplicitParam(name = "username", defaultValue = "https://www.cnblogs.com/qiantao/p/user", value = "https://www.cnblogs.com/qiantao/p/用戶名", required = true, dataType = "String", paramType = "query") }) @GetMapping("/getUserInfo") @ResponseBody public User getUserInfo(@RequestParam String username){ return userInfoService.getUserInfoByUsername(username); } }
5、啟動專案,訪問 localhost:8080/springfashionsys/swagger-ui.html 頁面直接跳轉到如下頁面

介面檔案頁面:

6、配置不攔截swagger
如果專案加了過濾器或攔截了swagger就無法訪問到,可能會報404,也可能報下圖錯誤,所以需要配置不攔截swagger,這里專案中整合了security可能有些不同,相關檔案位置可以看我上篇文章springboot整合springsecurity,

6.1 在WebSecurityConfg配置類中添加:
@Override public void configure(WebSecurity web) throws Exception { web.ignoring() //配置靜態檔案不需要認證 .antMatchers("/static/**") //配置swagger2不需要認證 .antMatchers("/v2/api-docs", "/configuration/security", "swagger/**", "/swagger-resources", "/swagger-resources/**", "/swagger-ui.html", "/swagger-ui.html/*", "/webjars/**"); }
6.2 WebMvcConfig配置類:
/** * @Author qt * @Date 2021/3/19 * @Description */ @Configuration public class WebMvcConfig extends WebMvcConfigurationSupport { /** * 配置靜態資源 * @param registry */ @Override protected void addResourceHandlers(ResourceHandlerRegistry registry) { //配置靜態檔案不需要認證,解決靜態資源被攔截的問題 registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/"); //配置swagger2不需要認證 registry.addResourceHandler("/swagger-ui.html") .addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/"); super.addResourceHandlers(registry); } }
demo地址:http://www.qnto.top/springfashionsys/swagger-ui.html
總結:實踐是檢驗真理的唯一標準,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/275660.html
標籤:Java
上一篇:想少踩坑?不可忽視的JAVA注釋
下一篇:010_Nginx入門
