编写基础框架 60%
This commit is contained in:
parent
a28a3663ea
commit
de54e6789a
|
@ -13,5 +13,21 @@
|
|||
<artifactId>common</artifactId>
|
||||
<version>1.0</version>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.fastjson2</groupId>
|
||||
<artifactId>fastjson2</artifactId>
|
||||
<version>${fastjson2.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
package com.common.info;
|
||||
|
||||
import lombok.ToString;
|
||||
|
||||
@ToString
|
||||
public enum CodeMsg {
|
||||
SUCCESS(200, "成功");
|
||||
|
||||
private Integer code;
|
||||
private String message;
|
||||
|
||||
CodeMsg(Integer code, String message){
|
||||
this.code = code;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public Integer getCode() {
|
||||
return code;
|
||||
}
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
}
|
30
pom.xml
30
pom.xml
|
@ -38,32 +38,26 @@
|
|||
<spring.plugin.version>2.7.17</spring.plugin.version>
|
||||
<!-- 权限包 -->
|
||||
<jwt.version>4.2.2</jwt.version>
|
||||
<!-- mybatis -->
|
||||
<!-- jdbc -->
|
||||
<mybatis.plus.version>3.5.5</mybatis.plus.version>
|
||||
<mybatis.plugin.version>1.4.2</mybatis.plugin.version>
|
||||
<c3p0.version>0.9.5.5</c3p0.version>
|
||||
<mysql.version>8.0.28</mysql.version>
|
||||
<postgresql.version>42.3.8</postgresql.version>
|
||||
<!--project -->
|
||||
<project.version>1.0</project.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!--log -->
|
||||
|
||||
<!-- 热部署 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-devtools</artifactId>
|
||||
<optional>true</optional>
|
||||
<scope>true</scope>
|
||||
</dependency>
|
||||
<!--测试依赖-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<!--基础 包 -->
|
||||
<dependency>
|
||||
<groupId>com.szr</groupId>
|
||||
<artifactId>common</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!--data 包 -->
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
|
|
88
zbsz/pom.xml
88
zbsz/pom.xml
|
@ -9,20 +9,84 @@
|
|||
<version>1.0</version>
|
||||
</parent>
|
||||
|
||||
<groupId>com.szr</groupId>
|
||||
<artifactId>zbsz</artifactId>
|
||||
<version>1.0</version>
|
||||
|
||||
<dependencies>
|
||||
<!--基础 包 -->
|
||||
<dependency>
|
||||
<groupId>com.szr</groupId>
|
||||
<artifactId>common</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- spring web 依赖 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-validation</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.xiaoymin</groupId>
|
||||
<artifactId>knife4j-openapi2-spring-boot-starter</artifactId>
|
||||
<version>${knife4j.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-swagger-ui</artifactId>
|
||||
<version>${swagger.ui.version}</version>
|
||||
</dependency>
|
||||
<!-- jdbc 依赖 -->
|
||||
<dependency>
|
||||
<groupId>com.mchange</groupId>
|
||||
<artifactId>c3p0</artifactId>
|
||||
<version>${c3p0.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-boot-starter</artifactId>
|
||||
<version>${mybatis.plus.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
<version>${postgresql.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>${mysql.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-aop</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- security -->
|
||||
<dependency>
|
||||
<groupId>com.auth0</groupId>
|
||||
<artifactId>java-jwt</artifactId>
|
||||
<version>${jwt.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 热部署 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-devtools</artifactId>
|
||||
<optional>true</optional>
|
||||
<scope>true</scope>
|
||||
</dependency>
|
||||
<!--测试依赖-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.mybatis.generator</groupId>
|
||||
<artifactId>mybatis-generator-maven-plugin</artifactId>
|
||||
<version>${mybatis.plugin.version}</version>
|
||||
<configuration>
|
||||
<overwrite>true</overwrite> <!-- 重复生成时覆盖掉上次生成的文件 -->
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
package com.zbsz;
|
||||
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
@MapperScan("com.zbsz.mapper")
|
||||
public class Run {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Run.class,args);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package com.zbsz.config;
|
||||
|
||||
import com.mchange.v2.c3p0.ComboPooledDataSource;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.jdbc.DataSourceBuilder;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
|
||||
@Configuration
|
||||
public class C3P0Config {
|
||||
@Bean("datasource")
|
||||
@Primary
|
||||
@ConfigurationProperties(prefix = "c3p0")
|
||||
public DataSource dataSource(){
|
||||
return DataSourceBuilder.create().type(ComboPooledDataSource.class).build();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package com.zbsz.config;
|
||||
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
|
||||
|
||||
@Configuration
|
||||
public class JsonConfig {
|
||||
@Bean
|
||||
Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer(){
|
||||
return new Jackson2ObjectMapperBuilderCustomizer() {
|
||||
@Override
|
||||
public void customize(Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder) {
|
||||
jacksonObjectMapperBuilder.featuresToDisable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
package com.zbsz.config;
|
||||
|
||||
import com.zbsz.filter.JwtAuthenticationInterceptor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
|
||||
import springfox.documentation.builders.ApiInfoBuilder;
|
||||
import springfox.documentation.builders.PathSelectors;
|
||||
import springfox.documentation.builders.RequestHandlerSelectors;
|
||||
import springfox.documentation.spi.DocumentationType;
|
||||
import springfox.documentation.spring.web.plugins.Docket;
|
||||
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;
|
||||
|
||||
|
||||
/**
|
||||
* http://localhost:8601/doc.html
|
||||
*/
|
||||
@Configuration
|
||||
@Import(BeanValidatorPluginsConfiguration.class)
|
||||
@EnableSwagger2WebMvc
|
||||
public class Swagger2Config implements WebMvcConfigurer {
|
||||
|
||||
@Bean
|
||||
public Docket createRestApi() {
|
||||
return new Docket(DocumentationType.SWAGGER_2)
|
||||
.apiInfo(new ApiInfoBuilder()
|
||||
.title("Event Bridge API")
|
||||
.description("Event Bridge API 说明")
|
||||
.version("V-1.0")
|
||||
.build())
|
||||
.select()
|
||||
.apis(RequestHandlerSelectors.basePackage("com.rest.controller"))
|
||||
.paths(PathSelectors.any())
|
||||
.build();
|
||||
}
|
||||
|
||||
//过滤器
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
//添加拦截器
|
||||
registry.addInterceptor(new JwtAuthenticationInterceptor()).addPathPatterns("/api/*")
|
||||
.excludePathPatterns(
|
||||
"/api/login");//放掉某些特定不需要校验token的路由
|
||||
}
|
||||
|
||||
@Bean
|
||||
public JwtAuthenticationInterceptor authenticationInterceptor() {
|
||||
return new JwtAuthenticationInterceptor();
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.zbsz.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@TableName(value = "user")
|
||||
public class User {
|
||||
private Long id;
|
||||
private String name;
|
||||
private Integer age;
|
||||
private String email;
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
package com.zbsz.filter;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.zbsz.info.Result;
|
||||
import com.zbsz.tool.JwtTool;
|
||||
import org.springframework.web.method.HandlerMethod;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.PrintWriter;
|
||||
|
||||
public class JwtAuthenticationInterceptor implements HandlerInterceptor {
|
||||
|
||||
//判断然后进行用户拦截
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
|
||||
// 如果不是映射到方法直接通过
|
||||
if (!(handler instanceof HandlerMethod)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// 从请求头中取出 token 这里需要和前端约定好把jwt放到请求头一个叫token的地方
|
||||
String token = request.getHeader("token");
|
||||
|
||||
//验证token
|
||||
if(JwtTool.verify(token)){
|
||||
String userName = JwtTool.getUsername(token);
|
||||
request.setAttribute("token.user",userName);
|
||||
return true;
|
||||
}else{
|
||||
response.setCharacterEncoding("UTF-8");
|
||||
response.setContentType("application/json; charset=utf-8");
|
||||
Result result = new Result();
|
||||
|
||||
// if (token == null) {
|
||||
// result.setCode(CodeMsg.TOKEN_NULL.getCode());
|
||||
// result.setMsg(CodeMsg.TOKEN_NULL.getMessage());
|
||||
// LogTool.Error.error("token == null ------------------- : 用户未登录");
|
||||
// }else{
|
||||
// result.setCode(CodeMsg.TOKEN_FAILED.getCode());
|
||||
// result.setMsg(CodeMsg.TOKEN_FAILED.getMessage());
|
||||
// LogTool.Error.error("JwtTool.verify(token) ------------------- : 认证token已过期");
|
||||
// }
|
||||
|
||||
PrintWriter out = null;
|
||||
try {
|
||||
out = response.getWriter();
|
||||
out.write(JSONObject.toJSONString(result));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}finally {
|
||||
if(out != null){
|
||||
out.flush();
|
||||
out.close();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.zbsz.info;
|
||||
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class Result {
|
||||
@ApiModelProperty(value = "返回码")
|
||||
private Integer code;
|
||||
@ApiModelProperty(value = "返回成功、失败信息")
|
||||
private String msg;
|
||||
@ApiModelProperty(value = "返回json对象信息")
|
||||
private Object data;
|
||||
@ApiModelProperty(value = "返回总条数")
|
||||
private Long total;
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package com.zbsz.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.zbsz.entity.User;
|
||||
|
||||
public interface UserMapper extends BaseMapper<User> {
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
package com.zbsz.tool;
|
||||
|
||||
import com.auth0.jwt.JWT;
|
||||
import com.auth0.jwt.JWTVerifier;
|
||||
import com.auth0.jwt.algorithms.Algorithm;
|
||||
import com.auth0.jwt.exceptions.JWTDecodeException;
|
||||
import com.auth0.jwt.interfaces.DecodedJWT;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class JwtTool {
|
||||
private static final long EXPIRE_TIME = 15 * 60 * 60 * 1000;
|
||||
private static final String TOKEN_SECRET = "hp-xx040506070809-xxx-ppp";
|
||||
|
||||
/**
|
||||
* 校验token是否正确
|
||||
*
|
||||
* @param token 密钥
|
||||
* @return 是否正确
|
||||
*/
|
||||
public static boolean verify(String token) {
|
||||
try {
|
||||
Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
|
||||
JWTVerifier verifier = JWT.require(algorithm).build();
|
||||
verifier.verify(token);
|
||||
return true;
|
||||
} catch (Exception exception) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得token中的信息无需secret解密也能获得
|
||||
* @return token中包含的用户名
|
||||
*/
|
||||
public static String getUsername(String token) {
|
||||
try {
|
||||
DecodedJWT jwt = JWT.decode(token);
|
||||
return jwt.getClaim("loginName").asString();
|
||||
} catch (JWTDecodeException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取token
|
||||
* @return jwt token
|
||||
*/
|
||||
public static String getToken(String username) {
|
||||
try {
|
||||
Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME);//过期时间
|
||||
Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);//私钥及加密算法
|
||||
// 附带username,userId信息,生成签名
|
||||
return JWT.create().withAudience(username)
|
||||
.withIssuedAt(new Date()) //发行时间
|
||||
.withExpiresAt(date) //有效时间
|
||||
.withClaim("loginName", username)
|
||||
.sign(algorithm);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
server:
|
||||
port: 8601
|
||||
# ssl:
|
||||
# key-store: classpath:Serverkeystore.p12
|
||||
# key-store-type: PKCS12
|
||||
# key-store-password: 123456
|
||||
# key-alias: serverKey
|
||||
|
||||
spring:
|
||||
application:
|
||||
name: auth-server
|
||||
mvc:
|
||||
pathmatch:
|
||||
matching-strategy: ant_path_matcher
|
||||
|
||||
|
||||
c3p0:
|
||||
# jdbcUrl: jdbc:mysql://82.157.153.250:3306/hp_db?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
|
||||
# driverClass: com.mysql.cj.jdbc.Driver
|
||||
# user: hp
|
||||
# password: Hp1949..
|
||||
jdbcUrl: jdbc:postgresql://localhost:5432/postgres
|
||||
driverClass: org.postgresql.Driver
|
||||
user: postgres
|
||||
password: postgres
|
||||
minPoolSize: 2
|
||||
maxPoolSize: 10
|
||||
maxIdleTime: 6000
|
||||
breakAfterAcquireFailure: true
|
||||
testConnectionOnCheckout: false
|
||||
|
||||
# https://localhost:8601/doc.html
|
||||
knife4j:
|
||||
enable: true
|
||||
basic:
|
||||
enable: true
|
||||
username: admin
|
||||
password: admin@123
|
|
@ -0,0 +1,51 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<configuration scan="true" scanPeriod="60 seconds" debug="false">
|
||||
<property name="log_dir" value="/szr/logs/zbsz" />
|
||||
<property name="maxHistory" value="30"/>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger -%msg%n</pattern>
|
||||
<charset>UTF-8</charset>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<appender name="ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<filter class="ch.qos.logback.classic.filter.LevelFilter">
|
||||
<level>ERROR</level>
|
||||
<onMatch>ACCEPT</onMatch>
|
||||
<onMismatch>DENY</onMismatch>
|
||||
</filter>
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||
<fileNamePattern>${log_dir}/%d{yyyy-MM-dd}/error-log.log</fileNamePattern>
|
||||
<maxHistory>${maxHistory}</maxHistory>
|
||||
</rollingPolicy>
|
||||
<encoder>
|
||||
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
|
||||
<charset>UTF-8</charset>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<appender name="INFO" class="ch.qos.logback.core.rolling.RollingFileAppender" >
|
||||
<filter class="ch.qos.logback.classic.filter.LevelFilter">
|
||||
<level>INFO</level>
|
||||
<onMatch>ACCEPT</onMatch>
|
||||
<onMismatch>DENY</onMismatch>
|
||||
</filter>
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||
<fileNamePattern>${log_dir}/%d{yyyy-MM-dd}/info-log.log
|
||||
</fileNamePattern>
|
||||
<maxHistory>${maxHistory}</maxHistory>
|
||||
</rollingPolicy>
|
||||
<encoder>
|
||||
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
|
||||
<charset>UTF-8</charset>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO" >
|
||||
<appender-ref ref="STDOUT" />
|
||||
<appender-ref ref="ERROR" />
|
||||
<appender-ref ref="INFO" />
|
||||
</root>
|
||||
</configuration>
|
Loading…
Reference in New Issue