2023年2月18日17:50:59 jwt
parent
e9ff569837
commit
c1bb104189
|
|
@ -0,0 +1,63 @@
|
|||
package com.zhangmeng.tools.controller;
|
||||
|
||||
import io.jsonwebtoken.Claims;
|
||||
import io.jsonwebtoken.Jwts;
|
||||
import io.jsonwebtoken.SignatureAlgorithm;
|
||||
import javafx.collections.FXCollections;
|
||||
import javafx.collections.ObservableList;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.scene.control.ComboBox;
|
||||
import javafx.scene.control.TextField;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author : 芊芊墨客
|
||||
* @version : 1.0
|
||||
* @date : 2023-02-18 16:21
|
||||
*/
|
||||
public class JwtController {
|
||||
|
||||
@FXML
|
||||
private TextField secret_key;
|
||||
|
||||
@FXML
|
||||
private TextField expiration_time;
|
||||
|
||||
@FXML
|
||||
private ComboBox<SignatureAlgorithm> comboBox;
|
||||
|
||||
@FXML
|
||||
public void initialize() {
|
||||
ObservableList<SignatureAlgorithm> list = FXCollections.observableArrayList(SignatureAlgorithm.values());
|
||||
comboBox.setItems(list);
|
||||
comboBox.getSelectionModel().select(SignatureAlgorithm.ES256);
|
||||
|
||||
}
|
||||
|
||||
private String generateToken(Map<String, Object> claims,String secret,long expiration,SignatureAlgorithm signatureAlgorithm) {
|
||||
Date expirationDate = new Date(System.currentTimeMillis() + expiration);
|
||||
return Jwts.builder().setClaims(claims).setExpiration(expirationDate).signWith(signatureAlgorithm, secret).compact();
|
||||
}
|
||||
|
||||
private Claims getClaimsFromToken(String token,String secret) {
|
||||
Claims claims;
|
||||
try {
|
||||
claims = Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody();
|
||||
} catch (Exception e) {
|
||||
claims = null;
|
||||
}
|
||||
return claims;
|
||||
}
|
||||
|
||||
public Boolean isTokenExpired(String token,String secret ) {
|
||||
try {
|
||||
Claims claims = getClaimsFromToken(token,secret);
|
||||
Date expiration = claims.getExpiration();
|
||||
return expiration.before(new Date());
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -42,6 +42,7 @@ public class SmallToolsController {
|
|||
|
||||
private AnchorPane hex_16;
|
||||
private AnchorPane unicode;
|
||||
private AnchorPane jwt_web;
|
||||
|
||||
@FXML
|
||||
private ListView<ResourcesUtils.SmallTools> listView;
|
||||
|
|
@ -128,6 +129,13 @@ public class SmallToolsController {
|
|||
}
|
||||
unicode(flag);
|
||||
}
|
||||
|
||||
if (newValue.getIndex() == 2) {
|
||||
if (jwt_web != null) {
|
||||
flag = true;
|
||||
}
|
||||
jwt_web(flag);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
@ -136,6 +144,7 @@ public class SmallToolsController {
|
|||
return switch (SmallTools) {
|
||||
case Hex_16 -> new Image(ImagePath.path(ImagePath.ImagePathType.Hex_16));
|
||||
case Unicode -> new Image(ImagePath.path(ImagePath.ImagePathType.Unicode));
|
||||
case JWT_WEB -> new Image(ImagePath.path(ImagePath.ImagePathType.JWT_WEB));
|
||||
};
|
||||
}
|
||||
|
||||
|
|
@ -233,6 +242,23 @@ public class SmallToolsController {
|
|||
common_method();
|
||||
}
|
||||
|
||||
public void jwt_web(boolean flag){
|
||||
//默认选择第一个
|
||||
listView.getSelectionModel().select(2);
|
||||
|
||||
if (!flag) {
|
||||
try {
|
||||
root = FXMLLoader.load(ResourcesUtils.getResource("jwt"));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
jwt_web = root;
|
||||
} else {
|
||||
root = jwt_web;
|
||||
}
|
||||
common_method();
|
||||
}
|
||||
|
||||
private void common_method() {
|
||||
splitPane.getItems().remove(1);
|
||||
splitPane.getItems().add(1, root);
|
||||
|
|
|
|||
|
|
@ -54,6 +54,7 @@ public class ImagePath {
|
|||
VIP_PLAYER("VIP解析播放"),
|
||||
MD5("md5 加密"),
|
||||
SPRING_SECURITY("SPRING_SECURITY 加密"),
|
||||
JWT_WEB("json-web-token"),
|
||||
|
||||
Hex_16("16 进制"),
|
||||
Unicode("Unicode"),
|
||||
|
|
@ -238,6 +239,7 @@ public class ImagePath {
|
|||
public static String SPRING_SECURITY = "svg/spring-security.png";
|
||||
public static String Hex_16 = "svg/hex_16.png";
|
||||
public static String Unicode = "svg/unicode.png";
|
||||
public static String JWT_WEB = "svg/jwt-web.png";
|
||||
|
||||
public static String path(ImagePathType type) {
|
||||
|
||||
|
|
@ -386,6 +388,9 @@ public class ImagePath {
|
|||
case Unicode:
|
||||
path = ImagePath.Unicode;
|
||||
break;
|
||||
case JWT_WEB:
|
||||
path = ImagePath.JWT_WEB;
|
||||
break;
|
||||
}
|
||||
return "static/" + path;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -82,7 +82,7 @@ public class ResourcesUtils {
|
|||
|
||||
Hex_16("16进制(Hex)",0),
|
||||
Unicode("Unicode和字符串转换",1),
|
||||
|
||||
JWT_WEB("json-web-token",2),
|
||||
;
|
||||
|
||||
SmallTools(String title, int index) {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,27 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<?import javafx.scene.control.Button?>
|
||||
<?import javafx.scene.control.ComboBox?>
|
||||
<?import javafx.scene.control.Label?>
|
||||
<?import javafx.scene.control.TextField?>
|
||||
<?import javafx.scene.layout.AnchorPane?>
|
||||
<?import javafx.scene.text.Text?>
|
||||
|
||||
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="649.0" prefWidth="1200.0" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.zhangmeng.tools.controller.JwtController">
|
||||
<children>
|
||||
<Label layoutX="304.0" layoutY="84.0" text="秘钥:" />
|
||||
<TextField fx:id="secret_key" layoutX="414.0" layoutY="80.0" prefHeight="25.0" prefWidth="520.0" AnchorPane.rightAnchor="266.0" />
|
||||
<ComboBox fx:id="comboBox" layoutX="414.0" layoutY="130.0" prefWidth="150.0" />
|
||||
<Label layoutX="292.0" layoutY="134.0" text="加密方式:" />
|
||||
<Button layoutX="414.0" layoutY="402.0" mnemonicParsing="false" text="生成" />
|
||||
<Label layoutX="286.0" layoutY="267.0" text="加密的数据:" />
|
||||
<Label layoutX="470.0" layoutY="228.0" text="key" />
|
||||
<Label layoutX="702.0" layoutY="228.0" text="value" />
|
||||
<TextField layoutX="414.0" layoutY="263.0" />
|
||||
<TextField layoutX="637.0" layoutY="263.0" />
|
||||
<Button layoutX="840.0" layoutY="263.0" mnemonicParsing="false" text="添加" />
|
||||
<Label layoutX="277.0" layoutY="345.0" text="过期时间:" />
|
||||
<TextField layoutX="420.0" layoutY="333.0" prefHeight="25.0" prefWidth="175.0" />
|
||||
<Text layoutX="612.0" layoutY="350.0" strokeType="OUTSIDE" strokeWidth="0.0" text="(毫秒)" />
|
||||
</children>
|
||||
</AnchorPane>
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 18 KiB |
Loading…
Reference in New Issue