• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Springboot整合knife4j与shiro的操作

springboot 搞代码 4年前 (2022-01-05) 65次浏览 已收录 0个评论
文章目录[隐藏]

这篇文章主要介绍了Springboot整合knife4j与shiro的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

一、介绍knife4j

增强版本的Swagger 前端UI,取名knife4j是希望她能像一把匕首一样小巧,轻量,并且功能强悍,更名也是希望把她做成一个为Swagger接口文档服务的通用性解决方案,不仅仅只是专注于前端Ui前端。

二、Spring Boot 整合knife4j

第一步

在Maven中的pom.xml文件引入:

  com.github.xiaoyminknife4j-spring-boot-starter<!--在引用时请在maven中央仓库搜索最新版本号-->2.0.4

第二步

增加配置类,主要添加@Configuration、EnableSwagger2、@EnableKnife4j以及@Import(BeanValidatorPluginsConfiguration.class)注解:

 @Configuration @EnableSwagger2 @EnableKnife4j @Imp<strong style="color:transparent">来源gaodaima#com搞(代@码网</strong>ort(BeanValidatorPluginsConfiguration.class) public class Swagger2Config { @Bean public Docket createRestApi(){ return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .enable(true) .select() //为当前包下controller生成API文档 .apis(RequestHandlerSelectors.basePackage("com.dream")) .paths(PathSelectors.any()) .build() .securitySchemes(securitySchemes()) .securityContexts(securityContexts()); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("SwaggerUI") .description("mall-tiny") .contact("macro") .version("1.0") .build(); } private List securitySchemes() { //设置请求头信息 List result = new ArrayList(); ApiKey apiKey = new ApiKey("Authorization", "Authorization", "header"); result.add(apiKey); return result; } private List securityContexts() { //设置需要登录认证的路径 List result = new ArrayList(); result.add(getContextByPath("/misty/.*")); return result; } private SecurityContext getContextByPath(String pathRegex){ return SecurityContext.builder() .securityReferences(defaultAuth()) .forPaths(PathSelectors.regex(pathRegex)) .build(); } private List defaultAuth() { List result = new ArrayList(); AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything"); AuthorizationScope[] authorizationScopes = new AuthorizationScope[1]; authorizationScopes[0] = authorizationScope; result.add(new SecurityReference("Authorization", authorizationScopes)); return result; } } 

第三步

如果项目中没有使用shiro、SpringSecurity 等权限框架,可以访问,如下地址:

http://localhost:8080/doc.html

第四步

如果使用了权限框架,如shiro、SpringSecurity,需要添加配置:

1、实现WebMvcConfigurer

 @SpringBootApplication public class SwaggerBootstrapUiDemoApplication  implements WebMvcConfigurer{ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("doc.html").addResourceLocations("classpath*:/META-INF/resources/"); registry.addResourceHandler("/webjars/**").addResourceLocations("classpath*:/META-INF/resources/webjars/"); } }

注意: 楼主在这里遇到一个很大的坑,就是如果我使用classpath*:,会一直报错;修改为classpath后,恢复正常。

2、楼主用的shiro,需要配置,放开相应的路径:

 @Bean protected ShiroFilterChainDefinition shiroFilterChainDefinition() { DefaultShiroFilterChainDefinition chainDefinition = new DefaultShiroFilterChainDefinition(); chainDefinition.addPathDefinition("/doc.html", "anon"); chainDefinition.addPathDefinition("/webjars/**/**", "anon"); return chainDefinition; }

第五步,展示结果:

首页

实体页

knife4j 的官网地址

补充一点知识:

classpath和classpath*区别:

  • classpath:默认只会在你项目的class路径中查找文件。
  • classpath*:默认不仅包含class路径,还包括jar文件中(class路径)进行查找。
  • 注意:
  • 使用classpath*:Spring需要遍历所有的classpath,所以加载速度是很慢的;故在设计中,应该尽可能划分好资源文件所在的路径,尽量避免使用classpath*。

classpath*的使用:

  • 当项目中有多个classpath路径,并同时加载多个classpath路径下(此种情况多数不会遇到)的文件,就发挥了作用,如果不加,则表示仅仅加载第一个classpath路径。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持gaodaima搞代码网

以上就是Springboot整合knife4j与shiro的操作的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Springboot整合knife4j与shiro的操作

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址