main
黄海 10 months ago
parent 3d07166195
commit 0b9693f7f3

@ -71,5 +71,33 @@
<artifactId>snakeyaml</artifactId>
<version>${snakeyaml.version}</version>
</dependency>
<!--https://blog.csdn.net/AdminGuan/article/details/100147488-->
<!--JWT,用于鉴权-->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.7.0</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
</project>

@ -38,6 +38,15 @@ public class GwApplication extends JFinalConfig {
public static Map<String, String> routeList = new HashMap<>();
//白名单
public static Set<String> whiteSet = new HashSet<>();
public static Prop PropKit;
static {
//加载配置文件
String configFile = "application_{?}.yaml".replace("{?}", getEnvPrefix());
PropKit = new YamlProp(configFile);
}
/**
*
*/
@ -45,16 +54,14 @@ public class GwApplication extends JFinalConfig {
public void configConstant(Constants me) {
//使用LogBack
me.setLogFactory(new LogBackLogFactory());
//加载配置文件
String configFile = "application_{?}.yaml".replace("{?}", getEnvPrefix());
Prop PropKit = new YamlProp(configFile);
// 设置静态根目录为上传根目录
me.setBaseUploadPath(PropKit.get("uploadTempPath"));
// 获取所有配置项,得到路由表
for (Map.Entry<Object, Object> entry : PropKit.getProperties().entrySet()) {
if (entry.getKey().toString().startsWith("route")) {
routeList.put(entry.getKey().toString().substring(6).replace(".url",""), entry.getValue().toString());
routeList.put(entry.getKey().toString().substring(6).replace(".url", ""), entry.getValue().toString());
}
}
// 获取白名单链接列表

@ -0,0 +1,67 @@
package com.dsideal.gw.Util;
import com.dsideal.gw.GwApplication;
import com.dsideal.gw.Plugin.YamlProp;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Date;
public class JwtUtil {
public static final String AUTHORIZATION_STARTER = "Bearer ";
public static final String SECRET = GwApplication.PropKit.get("SECRET");
/**
* JWT
*
* @param identity_id ID
* @param person_id ID
* @return JWT
*/
public static String generateToken(int identity_id, String person_id) {
// 获取当前日期和时间
Date now = new Date();
// 格式化日期
Map<String, Object> claims = new HashMap<>();
claims.put("create_time", now.toString());
claims.put("identity_id", identity_id);
claims.put("person_id", person_id);
return AUTHORIZATION_STARTER + Jwts.builder().setClaims(claims).signWith(SignatureAlgorithm.HS256, SECRET).compact();
}
public static Claims getClaims(String token) {
if (token.contains(AUTHORIZATION_STARTER)) {
token = token.replace(AUTHORIZATION_STARTER, "");
}
Claims claims;
try {
claims = Jwts.parser()
.setSigningKey(SECRET)
.parseClaimsJws(token)
.getBody();
} catch (Exception e) {
try {
claims = Jwts.parser()
.setSigningKey(SECRET.getBytes(StandardCharsets.UTF_8))
.parseClaimsJws(token)
.getBody();
} catch (Exception err) {
claims = null;
}
}
return claims;
}
public static void main(String[] args) {
GwApplication gw = new GwApplication();
String token = generateToken(4, "0b64e31e-a85e-43eb-ba5f-3088d986a8da");
Claims claims = getClaims(token);
System.out.println(claims.get("identity_id"));
System.out.println(claims.get("person_id"));
}
}

@ -1,6 +1,9 @@
# 上传文件的临时路径
uploadTempPath: c:/Windows/Temp
# JWT
SECRET: ZXZnZWVr5b+r5LmQ5L2g55qE5Ye66KGM
# 路由
route:
# 基础数据

@ -1,5 +1,8 @@
# 上传文件的临时路径
uploadTempPath: /tmp
# JWT
SECRET: ZXZnZWVr5b+r5LmQ5L2g55qE5Ye66KGM
# 路由
route:
# 基础数据

@ -1,6 +1,9 @@
# 上传文件的临时路径
uploadTempPath: c:/Windows/Temp
# JWT
SECRET: ZXZnZWVr5b+r5LmQ5L2g55qE5Ye66KGM
# 路由
route:
# 基础数据

@ -1,5 +1,8 @@
# 上传文件的临时路径
uploadTempPath: /tmp
# JWT
SECRET: ZXZnZWVr5b+r5LmQ5L2g55qE5Ye66KGM
# 路由
route:
# 基础数据

Loading…
Cancel
Save