diff --git a/common/pom.xml b/common/pom.xml index 3ea4bd8..5471e24 100644 --- a/common/pom.xml +++ b/common/pom.xml @@ -13,5 +13,21 @@ common 1.0 + + + org.projectlombok + lombok + + + com.alibaba.fastjson2 + fastjson2 + ${fastjson2.version} + + + junit + junit + + + diff --git a/common/src/main/java/com/common/info/CodeMsg.java b/common/src/main/java/com/common/info/CodeMsg.java new file mode 100644 index 0000000..beb8601 --- /dev/null +++ b/common/src/main/java/com/common/info/CodeMsg.java @@ -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; + } +} diff --git a/pom.xml b/pom.xml index 0aa016f..3a01123 100644 --- a/pom.xml +++ b/pom.xml @@ -38,32 +38,26 @@ 2.7.17 4.2.2 - + + 3.5.5 1.4.2 + 0.9.5.5 + 8.0.28 + 42.3.8 1.0 - - - - - - org.springframework.boot - spring-boot-devtools - true - true - - - - org.springframework.boot - spring-boot-starter-test - test - - + + + com.szr + common + ${project.version} + + org.projectlombok diff --git a/zbsz/pom.xml b/zbsz/pom.xml index fd5d976..a2db7e4 100644 --- a/zbsz/pom.xml +++ b/zbsz/pom.xml @@ -9,20 +9,84 @@ 1.0 + com.szr zbsz + 1.0 + + + + + com.szr + common + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-validation + + + com.github.xiaoymin + knife4j-openapi2-spring-boot-starter + ${knife4j.version} + + + io.springfox + springfox-swagger-ui + ${swagger.ui.version} + + + + com.mchange + c3p0 + ${c3p0.version} + + + com.baomidou + mybatis-plus-boot-starter + ${mybatis.plus.version} + + + org.postgresql + postgresql + ${postgresql.version} + + + mysql + mysql-connector-java + ${mysql.version} + + + org.springframework.boot + spring-boot-starter-aop + + + + + com.auth0 + java-jwt + ${jwt.version} + + + + + org.springframework.boot + spring-boot-devtools + true + true + + + + org.springframework.boot + spring-boot-starter-test + test + + - - - - org.mybatis.generator - mybatis-generator-maven-plugin - ${mybatis.plugin.version} - - true - - - - diff --git a/zbsz/src/main/java/com/zbsz/Run.java b/zbsz/src/main/java/com/zbsz/Run.java new file mode 100644 index 0000000..61f250a --- /dev/null +++ b/zbsz/src/main/java/com/zbsz/Run.java @@ -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); + } +} diff --git a/zbsz/src/main/java/com/zbsz/config/C3P0Config.java b/zbsz/src/main/java/com/zbsz/config/C3P0Config.java new file mode 100644 index 0000000..ceca9fd --- /dev/null +++ b/zbsz/src/main/java/com/zbsz/config/C3P0Config.java @@ -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(); + } +} diff --git a/zbsz/src/main/java/com/zbsz/config/JsonConfig.java b/zbsz/src/main/java/com/zbsz/config/JsonConfig.java new file mode 100644 index 0000000..63863e2 --- /dev/null +++ b/zbsz/src/main/java/com/zbsz/config/JsonConfig.java @@ -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); + } + }; + } +} diff --git a/zbsz/src/main/java/com/zbsz/config/Swagger2Config.java b/zbsz/src/main/java/com/zbsz/config/Swagger2Config.java new file mode 100644 index 0000000..3f1a1bd --- /dev/null +++ b/zbsz/src/main/java/com/zbsz/config/Swagger2Config.java @@ -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(); + } + + +} diff --git a/zbsz/src/main/java/com/zbsz/entity/User.java b/zbsz/src/main/java/com/zbsz/entity/User.java new file mode 100644 index 0000000..eaa2438 --- /dev/null +++ b/zbsz/src/main/java/com/zbsz/entity/User.java @@ -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; +} diff --git a/zbsz/src/main/java/com/zbsz/filter/JwtAuthenticationInterceptor.java b/zbsz/src/main/java/com/zbsz/filter/JwtAuthenticationInterceptor.java new file mode 100644 index 0000000..6880b36 --- /dev/null +++ b/zbsz/src/main/java/com/zbsz/filter/JwtAuthenticationInterceptor.java @@ -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; + } + } + +} diff --git a/zbsz/src/main/java/com/zbsz/info/Result.java b/zbsz/src/main/java/com/zbsz/info/Result.java new file mode 100644 index 0000000..c3cf871 --- /dev/null +++ b/zbsz/src/main/java/com/zbsz/info/Result.java @@ -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; +} diff --git a/zbsz/src/main/java/com/zbsz/mapper/UserMapper.java b/zbsz/src/main/java/com/zbsz/mapper/UserMapper.java new file mode 100644 index 0000000..7f82249 --- /dev/null +++ b/zbsz/src/main/java/com/zbsz/mapper/UserMapper.java @@ -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 { +} diff --git a/zbsz/src/main/java/com/zbsz/tool/JwtTool.java b/zbsz/src/main/java/com/zbsz/tool/JwtTool.java new file mode 100644 index 0000000..7ac73a9 --- /dev/null +++ b/zbsz/src/main/java/com/zbsz/tool/JwtTool.java @@ -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; + } + } +} diff --git a/zbsz/src/main/resources/application.yml b/zbsz/src/main/resources/application.yml new file mode 100644 index 0000000..a054f8f --- /dev/null +++ b/zbsz/src/main/resources/application.yml @@ -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 diff --git a/zbsz/src/main/resources/logback.xml b/zbsz/src/main/resources/logback.xml new file mode 100644 index 0000000..c4d5bed --- /dev/null +++ b/zbsz/src/main/resources/logback.xml @@ -0,0 +1,51 @@ + + + + + + + + %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger -%msg%n + UTF-8 + + + + + + ERROR + ACCEPT + DENY + + + ${log_dir}/%d{yyyy-MM-dd}/error-log.log + ${maxHistory} + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n + UTF-8 + + + + + + INFO + ACCEPT + DENY + + + ${log_dir}/%d{yyyy-MM-dd}/info-log.log + + ${maxHistory} + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n + UTF-8 + + + + + + + + +