This commit is contained in:
王永亮
2022-11-04 19:00:49 +08:00
parent b1322e347f
commit 7845481a18
16 changed files with 724 additions and 18 deletions
+16 -2
View File
@@ -3,9 +3,10 @@
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">
<parent>
<artifactId>aos-core</artifactId>
<artifactId>aos-dependencies</artifactId>
<groupId>com.aos</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../aos-dependencies/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
@@ -15,5 +16,18 @@
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>transmittable-thread-local</artifactId>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,29 @@
package com.aos.common.model;
/**
* 错误码接口
*
* @author wangyl
* @version V1.0
* @ClassName: ResultCodeInterface
* @Date: 2022/1/6 16:26
*/
public interface ResultCodeInterface {
/**
* @return java.lang.Integer
* @Description 获取错误码
* @Date 2020/11/8 15:37
* @Author wangyl
* @Version V1.0
*/
String getCode();
/**
* @return java.lang.String
* @Description 获取错误信息
* @Date 2020/11/8 15:38
* @Author wangyl
* @Version V1.0
*/
String getMsg();
}
@@ -0,0 +1,56 @@
/*
* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.aos.common.model.context;
import com.alibaba.ttl.TransmittableThreadLocal;
import java.util.Objects;
/**
* @author: wangyl
* @date: 2022/11/3
* @description: 用户信息上下文
*/
public class CurrentUserContextHolder {
private static ThreadLocal<SecurityUserInfo> userHolder = new TransmittableThreadLocal<>();
/**
* 清空
*/
public static void clearContext() {
userHolder.remove();
}
/**
* 删除
*/
public static SecurityUserInfo getContext() {
SecurityUserInfo securityUserInfo = userHolder.get();
if (Objects.isNull(securityUserInfo)) {
securityUserInfo = new SecurityUserInfo();
}
return securityUserInfo;
}
/**
* 设置
*/
public static void setContext(SecurityUserInfo context) {
userHolder.set(context);
}
}
@@ -0,0 +1,30 @@
package com.aos.common.model.context;
import lombok.Data;
/**
* @author wangyl
* @version V1.0
* @ClassName: SecurityUserInfo
* @Function: 认证的用户信息
* @Date: 2019/12/17 21:24
*/
@Data
public class SecurityUserInfo {
/**
* 用户名
*/
private String username = "未知";
/**
* 登录id
*/
private Long loginId = -1L;
/**
* 邮箱
*/
private String email = "";
/**
* 手机号
*/
private String phone = "";
}
@@ -0,0 +1,71 @@
package com.aos.common.model.exception;
import com.aos.common.model.ResultCodeInterface;
import lombok.Data;
/**
* 统一异常
*
* @author wyl
* @version V1.0
* @ClassName: BizException
* @Date: 2020/4/12 20:13
*/
@Data
public class AosBaseBizException extends RuntimeException {
private static final long serialVersionUID = 1L;
/**
* 错误码
*/
protected String errorCode;
/**
* 错误信息
*/
protected String errorMsg;
public AosBaseBizException() {
super();
}
public AosBaseBizException(ResultCodeInterface resultCode) {
super(resultCode.getCode());
this.errorCode = resultCode.getCode();
this.errorMsg = resultCode.getMsg();
}
public AosBaseBizException(ResultCodeInterface resultCode, Throwable cause) {
super(resultCode.getCode(), cause);
this.errorCode = resultCode.getCode();
this.errorMsg = resultCode.getMsg();
}
public AosBaseBizException(String errorMsg) {
super(errorMsg);
this.errorMsg = errorMsg;
}
public AosBaseBizException(String errorCode, String errorMsg) {
super(errorCode.toString());
this.errorCode = errorCode;
this.errorMsg = errorMsg;
}
public AosBaseBizException(String errorCode, String errorMsg, Throwable cause) {
super(errorCode.toString(), cause);
this.errorCode = errorCode;
this.errorMsg = errorMsg;
}
@Override
public String getMessage() {
return errorMsg;
}
@Override
public Throwable fillInStackTrace() {
return this;
}
}
@@ -0,0 +1,28 @@
package com.aos.common.model.result;
import com.aos.common.model.ResultCodeInterface;
public enum ResultCode implements ResultCodeInterface {
/**
*
*/
SUCCEED("200", "成功"),
FAILED("-1", "系统异常");
private String code;
private String msg;
ResultCode(String code, String msg) {
this.code = code;
this.msg = msg;
}
@Override
public String getCode() {
return code;
}
@Override
public String getMsg() {
return msg;
}
}
@@ -0,0 +1,51 @@
package com.aos.common.model.result;
import com.aos.common.model.ResultCodeInterface;
import lombok.Getter;
import lombok.Setter;
/**
* @author wangyl
* @version V1.0
* @ClassName: WebResult
* @Function: 返回对象
* @Date: 2020/3/27 10:49
*/
@Getter
@Setter
public class WebResult<T> {
/**
* 返回码
*/
private String code;
/**
* 描述信息
*/
private String msg;
/**
* 返回数据
*/
private T data;
public WebResult() {
}
public WebResult(String code, String msg, T data) {
this.code = code;
this.msg = msg;
this.data = data;
}
public WebResult(ResultCodeInterface resultCode) {
this(resultCode, null);
}
public WebResult(ResultCodeInterface resultCode, T data) {
this(resultCode.getCode(), resultCode.getMsg(), data);
}
public WebResult(String code, String msg) {
this(code, msg, null);
}
}
@@ -0,0 +1,20 @@
package com.aos.common.thread;
import com.alibaba.ttl.TtlRunnable;
import lombok.Builder;
import java.util.concurrent.ThreadPoolExecutor;
/**
* @author: wangyl
* @date: 2022/11/4
* @description: ttl的线程池
*/
@Builder
public class TtlThreadPool extends ThreadPoolExecutor {
@Override
public void execute(Runnable command) {
TtlRunnable.get(command);
}
}
@@ -0,0 +1,189 @@
/**
* @Author wangyl
* @E-mail wangyl@dsgdata.com
**/
package com.aos.common.token;
import com.google.common.base.Strings;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
import java.util.Date;
import java.util.Map;
import java.util.Objects;
/**
* @author wyl
* @version V1.0
* @ClassName: TokenUtilBase
* @Function: 提供部分jwt 基础接口
* @Date: 2019/12/17 0:36
*/
public class TokenUtilBase {
/**
* 生成一个token
*
* @param claims 需要存储的信息
* @param expireTime 设置token的超时时间
* @return java.lang.String
* @Date 2019/12/17 1:02
* @Author wangyl
* @Version V1.0
*/
public static String generateToken(Map<String, Object> claims, Long expireTime, String jwtSecret) {
Date expirationDate = generalExpirationDate(expireTime);
return Jwts.builder()
.setClaims(claims)
.setExpiration(expirationDate)
.signWith(SignatureAlgorithm.HS512, generalKey(jwtSecret))
.compact();
}
/**
* 使用默认的密钥,默认超时时间 生成token
*
* @param claims
* @return java.lang.String
* @Date 2022/11/3
* @Author wangyl
*/
public static String generateToken(Map<String, Object> claims) {
return generateToken(claims, null, null);
}
/**
* 使用默认的密钥,指定超时时间 生成token
*
* @param claims
* @param expireTime
* @return java.lang.String
* @Date 2022/11/3
* @Author wangyl
*/
public static String generateToken(Map<String, Object> claims, Long expireTime) {
return generateToken(claims, expireTime, null);
}
/**
* 使用默认的超时时间,指定密钥 生成token
*
* @param claims 需要存储的信息
* @return java.lang.String
* @Date 2019/12/17 20:38
* @Author wangyl
* @Version V1.0
*/
public static String generateToken(Map<String, Object> claims, String jwtSecret) {
return generateToken(claims, null, jwtSecret);
}
/**
* 从Token中获取Claims信息
*
* @param token
* @return io.jsonwebtoken.Claims
* @Date 2019/12/17 13:24
* @Author wangyl
* @Version V1.0
*/
public static Claims getClaimsFromToken(String token, String jwtSecret) {
Claims claims = Jwts.parser()
.setSigningKey(generalKey(jwtSecret))
.parseClaimsJws(token)
.getBody();
return claims;
}
/**
* 从Token中获取Claims信息
*
* @param token
* @return io.jsonwebtoken.Claims
* @Date 2019/12/17 13:24
* @Author wangyl
* @Version V1.0
*/
public static Claims getClaimsFromToken(String token) {
return getClaimsFromToken(token, null);
}
/**
* @param token
* @return java.lang.Boolean
* @Description 判断Token是否时效
* @Date 2019/12/17 13:41
* @Author wangyl
* @Version V1.0
*/
public Boolean tokenExpired(String token, String jwtSecret) {
Claims claims = getClaimsFromToken(token, jwtSecret);
Date expiration = claims.getExpiration();
return expiration.after(new Date());
}
/**
* 判断Token是否时效
*
* @param token
* @return java.lang.Boolean
* @Date 2019/12/17 13:41
* @Author wangyl
* @Version V1.0
*/
public Boolean tokenExpired(String token) {
return tokenExpired(token, null);
}
/**
* @param
* @return javax.crypto.SecretKey
* @Description 通过Base64加密生成秘钥
* @Date 2019/12/17 0:56
* @Author wangyl
* @Version V1.0
*/
private static SecretKey generalKey(String jwtSecret) {
if (Strings.isNullOrEmpty(jwtSecret)) {
jwtSecret = TokenConstant.jwtSecret;
}
byte[] encodedKey = Base64.getDecoder().decode(jwtSecret);
SecretKey key = new SecretKeySpec(encodedKey, 0, encodedKey.length, "AES");
return key;
}
/**
* 生成超时时间
*
* @param expireTime
* @return java.util.Date
* @Date 2022/11/3
* @Author wangyl
*/
private static Date generalExpirationDate(Long expireTime) {
if (Objects.isNull(expireTime) || Objects.equals(expireTime, 0L)) {
expireTime = TokenConstant.expireTime;
}
Date expirationDate = new Date(System.currentTimeMillis() + expireTime);
return expirationDate;
}
}
class TokenConstant {
/**
* 密钥
*/
final static String jwtSecret = "wylaigyx";
/**
* 默认有效期1小时
*/
final static long expireTime = 60 * 60 * 1000;
}
+42 -14
View File
@@ -6,6 +6,7 @@
<artifactId>aos-core</artifactId>
<groupId>com.aos</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>aos-dependencies</artifactId>
@@ -30,6 +31,8 @@
<spring-cloud-alibaba.version>2.2.7.RELEASE</spring-cloud-alibaba.version>
<spring.cloud.version>Hoxton.SR12</spring.cloud.version>
<spring.boot.vsersion>2.3.12.RELEASE</spring.boot.vsersion>
<jjwt.version>0.9.1</jjwt.version>
<ttl.version>2.11.4</ttl.version>
</properties>
<dependencyManagement>
<dependencies>
@@ -75,7 +78,12 @@
<artifactId>mybatis-plus-extension</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<!--tools-->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<!-- swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
@@ -98,6 +106,19 @@
<artifactId>knife4j-spring-ui</artifactId>
<version>${swagger-bootstrap-ui.knife4j.version}</version>
</dependency>
<!--JWT-->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>${jjwt.version}</version>
</dependency>
<!--thread-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>transmittable-thread-local</artifactId>
<version>${ttl.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
@@ -105,7 +126,6 @@
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
@@ -113,18 +133,26 @@
<version>1.7.30</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<attach>true</attach>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<distributionManagement>
<repository>
<id>nexus</id>
<name>releases</name>
<url>https://maven.wylgyx.top/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>nexus</id>
<name>snapshots</name>
<url>https://maven.wylgyx.top/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
</project>
+2 -1
View File
@@ -3,9 +3,10 @@
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">
<parent>
<artifactId>aos-core</artifactId>
<artifactId>aos-dependencies</artifactId>
<groupId>com.aos</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../aos-dependencies/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
+39
View File
@@ -0,0 +1,39 @@
<?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">
<parent>
<artifactId>aos-dependencies</artifactId>
<groupId>com.aos</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../aos-dependencies/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>aos-swagger</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!-- swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
</dependency>
<!-- swagger-bootstrap-ui-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-ui</artifactId>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,71 @@
package com.aos.swagger.conf;
import com.aos.swagger.enable.EnableSwagger;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
/**
* @author: zeng li liang
* @date: 2022/4/21
* @description:
*/
public class SwaggerConfig implements ImportBeanDefinitionRegistrar {
@Override
public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry beanDefinitionRegistry) {
//获取EnableEcho注解的所有属性的value
Map<String, Object> attributes = annotationMetadata.getAnnotationAttributes(EnableSwagger.class.getName());
//获取package属性的value
List<String> applicationNames = Arrays.asList((String[]) attributes.get("applicationName"));
List<String> scanPackagesPaths = Arrays.asList((String[]) attributes.get("scanPackagesPath"));
int size = applicationNames.size();
for (int i = 0; i < size; i++) {
//使用beanDefinitionRegistry对象将EchoBeanPostProcessor注入至Spring容器中
String applicationName = applicationNames.get(i);
String scanPackagesPath = scanPackagesPaths.get(i);
beanDefinitionRegistry.registerBeanDefinition(applicationName, beanDefinitionBuilder.getBeanDefinition());
AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext();
ConfigurableListableBeanFactory beanFactory = annotationConfigApplicationContext.getBeanFactory();
beanFactory.registerSingleton();
}
}
// @Value("${spring.application.name}")
// private String applicationName;
//
public Docket createRestApi(String applicationName) {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo(applicationName))
.groupName(applicationName)
.select()
.apis(RequestHandlerSelectors.basePackage("com.asura"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo(String applicationName) {
return new ApiInfoBuilder()
.title(applicationName + " swagger apis")
.description("swagger-bootstrap-ui")
.termsOfServiceUrl("")
.version("1.0")
.build();
}
}
@@ -0,0 +1,27 @@
package com.aos.swagger.enable;
import com.aos.swagger.conf.SwaggerConfig;
import org.springframework.context.annotation.Import;
import java.lang.annotation.*;
/**
* @author: wangyl
* @date: 2022/11/4
* @description: 启动swagger的注解
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import({SwaggerConfig.class})
public @interface EnableSwagger {
/**
* swagger显示的名称
*
* @param
* @return java.lang.String
* @Date 2022/11/4
* @Author wangyl
*/
String applicationName();
}
+39
View File
@@ -0,0 +1,39 @@
<?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">
<parent>
<artifactId>aos-dependencies</artifactId>
<groupId>com.aos</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../aos-dependencies/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>aos-web</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!-- swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
</dependency>
<!-- swagger-bootstrap-ui-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-ui</artifactId>
</dependency>
</dependencies>
</project>
+14 -1
View File
@@ -14,7 +14,20 @@
<module>aos-common</module>
<module>aos-feign</module>
<module>aos-log</module>
<module>aos-common</module>
<module>aos-swagger</module>
<module>aos-web</module>
</modules>
<packaging>pom</packaging>
<distributionManagement>
<repository>
<id>nexus</id>
<name>releases</name>
<url>https://maven.wylgyx.top/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>nexus</id>
<name>snapshots</name>
<url>https://maven.wylgyx.top/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
</project>