Springboot整合knife4j与shiro的操作

作者:坎布里奇 时间:2023-08-25 08:31:42 

一、介绍knife4j

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

二、Spring Boot 整合knife4j

第一步

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


<dependency>
   <groupId>com.github.xiaoymin</groupId>
   <artifactId>knife4j-spring-boot-starter</artifactId>
   <!--在引用时请在maven中央仓库搜索最新版本号-->
   <version>2.0.4</version>
</dependency>

第二步

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


@Configuration
@EnableSwagger2
@EnableKnife4j
@Import(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<ApiKey> securitySchemes() {
       //设置请求头信息
       List<ApiKey> result = new ArrayList<>();
       ApiKey apiKey = new ApiKey("Authorization", "Authorization", "header");
       result.add(apiKey);
       return result;
   }
   private List<SecurityContext> securityContexts() {
       //设置需要登录认证的路径
       List<SecurityContext> 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<SecurityReference> defaultAuth() {
       List<SecurityReference> 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;
}

第五步,展示结果:

首页

Springboot整合knife4j与shiro的操作

实体页

Springboot整合knife4j与shiro的操作

knife4j 的官网地址

补充一点知识:

classpath和classpath*区别:

  • classpath:默认只会在你项目的class路径中查找文件。

  • classpath*:默认不仅包含class路径,还包括jar文件中(class路径)进行查找。

  • 注意:

  • 使用classpath*:Spring需要遍历所有的classpath,所以加载速度是很慢的;故在设计中,应该尽可能划分好资源文件所在的路径,尽量避免使用classpath*。

classpath*的使用:

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

来源:https://blog.csdn.net/wind1_rain/article/details/108590462

标签:Springboot,knife4j,shiro
0
投稿

猜你喜欢

  • Java反射之类的实例对象的三种表示方式总结

    2023-10-03 09:51:35
  • Android View类与SurfaceView类详解

    2022-07-17 14:49:24
  • Java面向对象基础知识之封装,继承,多态和抽象

    2022-11-18 07:35:59
  • c# 调用Surfer软件,添加引用的具体操作方法

    2023-12-06 17:05:37
  • spring boot使用拦截器修改请求URL域名 换 IP 访问的方法

    2022-08-21 20:15:53
  • 如何用Java Stream写出既高雅又装*的代码

    2022-04-13 23:23:58
  • 浅谈collection标签的oftype属性能否为java.util.Map

    2023-03-19 23:16:15
  • WPF调用ffmpeg实现屏幕录制

    2023-04-23 13:57:00
  • Android编程实现网络图片查看器和网页源码查看器实例

    2021-07-27 23:48:40
  • C#编程自学之数据类型和变量一

    2023-07-30 02:45:49
  • Android自定义View实现开关按钮

    2021-09-08 08:53:22
  • 谈谈Hashmap的容量为什么是2的幂次问题

    2022-12-27 18:42:35
  • 详解IntelliJ IDEA中TortoiseSVN修改服务器地址的方法

    2023-11-25 04:51:04
  • java去除字符串中的空格、回车、换行符、制表符的小例子

    2022-04-13 12:33:44
  • android自定义组件实现仪表计数盘

    2023-12-23 21:27:41
  • JAVA JNI函数的注册过程详细介绍

    2023-02-07 18:41:17
  • Android Studio多工程引用同一个library项目配置的解决方法

    2022-04-07 16:39:40
  • C#基于UDP进行异步通信的方法

    2022-03-20 18:23:55
  • Spring Cloud Config RSA简介及使用RSA加密配置文件的方法

    2023-11-28 22:39:26
  • 基于获取JAVA路径,包括CLASSPATH外的路径的方法详解

    2022-12-20 21:30:03
  • asp之家 软件编程 m.aspxhome.com