springboot mybatisplus shiro jwt redis JAVA从零搭框架 home 编辑时间 2019/04/24 ![](/api/file/getImage?fileId=5d527c6016199b2a52000b0c) ## 前言 需求是自己搭一套空的后台项目API框架,并完成基本的配置,和基本的数据库表以及自动生成系统,为方便以后开发项目可以快速启动。 ### 基本 springboot 2.1.4 mybatisplus 3.4.0 ### 权限控制 shiro 1.4.0 jwt 3.8.0 ### 调试 swagger2 2.7.0 ### 数据库 mysql 5.7 redis 4.0.9 ## 正文 1 新建一个springboot项目 截图省略 <br> <br> 2 maven依赖 pom.xml ```xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> <relativePath/> </parent> <groupId>com.service</groupId> <artifactId>test</artifactId> <version>1.0.0</version> <name>test</name> <description>test service</description> <!-- version control --> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <mysql.version>5.1.38</mysql.version> <commons.lang.version>2.6</commons.lang.version> <fastjson.version>1.2.45</fastjson.version> <jedis.version>2.9.0</jedis.version> <mybatisplus.spring.boot.version>3.1.0</mybatisplus.spring.boot.version> <mybatisplus.generator.version>3.1.0</mybatisplus.generator.version> <freemarker.version>2.3.28</freemarker.version> <swagger.version>2.7.0</swagger.version> <commons.lang.version>2.6</commons.lang.version> <jwt.version>3.8.0</jwt.version> <apache.shiro.version>1.4.0</apache.shiro.version> </properties> <dependencies> <!-- springboot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- mysql mybatisplus generator --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>${mybatisplus.spring.boot.version}</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>${mybatisplus.generator.version}</version> </dependency> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>${freemarker.version}</version> </dependency> <!-- swagger2 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>${swagger.version}</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>${swagger.version}</version> </dependency> <!-- reids --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>${jedis.version}</version> </dependency> <!-- fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>${fastjson.version}</version> </dependency> <!-- StringUtils --> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>${commons.lang.version}</version> </dependency> <!-- shiro jwt --> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>${apache.shiro.version}</version> </dependency> <dependency> <groupId>com.auth0</groupId> <artifactId>java-jwt</artifactId> <version>${jwt.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <!-- 跳过单元测试 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <skipTests>true</skipTests> </configuration> </plugin> </plugins> </build> <!-- aliyun maven --> <repositories> <repository> <id>public</id> <name>aliyun nexus</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>public</id> <name>aliyun nexus</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> </project> ``` <br> 3 接下来是新建一个`config` 文件夹开始配置 Swagger2只需要一个`SwaggerConfig.java` 就可以配置完成 项目启动后swagger的访问路径是 http://localhost:port/context-path/swagger-ui.html (端口号、项目路径根据实际情况替换) `SwaggerConfig.java` ```java package com.kpiservice.kpi.config; import io.swagger.annotations.ApiOperation; 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.service.ApiInfo; import springfox.documentation.service.ApiKey; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; import java.util.List; import static com.google.common.collect.Lists.newArrayList; @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() //加了ApiOperation注解的类,才生成接口文档 .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) //包下的类,才生成接口文档 //.apis(RequestHandlerSelectors.basePackage("com.gemini.controller")) .paths(PathSelectors.any()) .build() .securitySchemes(security()); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("API接口调试页面") .description("api接口") // .termsOfServiceUrl("http://www.xxx.com") .version("1.0.0") .build(); } private List<ApiKey> security() { return newArrayList( new ApiKey("token", "token", "header") ); } } ``` # 未完待续 ## END 送人玫瑰,手留余香 赞赏 Wechat Pay Alipay java 实现 AES/CBC/PKCS7Padding 加密 解密 Chrome 73 安装crx扩展插件方法汇总,解决CRX HEADER INVALID