jwt 更新 2023年2月20日15:47:10

master
zhangmeng 2023-02-20 15:47:31 +08:00
parent c1bb104189
commit 20e8f7e24b
6 changed files with 196 additions and 28 deletions

View File

@ -162,7 +162,7 @@ public class HomeController implements Serializable {
@Override
public ListCell<ResourcesUtils.Menu> call(ListView<ResourcesUtils.Menu> playerListView) {
Label label = new Label();
label.setPrefWidth(100);
label.setPrefWidth(200);
ListCell<ResourcesUtils.Menu> listCell = new ListCell<>() {
@Override
protected void updateItem(ResourcesUtils.Menu player, boolean b) {

View File

@ -1,47 +1,180 @@
package com.zhangmeng.tools.controller;
import com.zhangmeng.tools.utils.AlertUtils;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.impl.Base64Codec;
import io.jsonwebtoken.impl.crypto.EllipticCurveSigner;
import io.jsonwebtoken.impl.crypto.MacSigner;
import io.jsonwebtoken.impl.crypto.RsaSigner;
import io.jsonwebtoken.impl.crypto.Signer;
import io.jsonwebtoken.lang.Assert;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.geometry.Pos;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.util.Callback;
import lombok.extern.slf4j.Slf4j;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
import java.nio.charset.StandardCharsets;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import static io.jsonwebtoken.SignatureAlgorithm.*;
/**
* @author :
* @version : 1.0
* @date : 2023-02-18 16:21
*/
@Slf4j
public class JwtController {
public static ObservableList<Data> list = FXCollections.observableArrayList();
@FXML
private TextField secret_key;
@FXML
private TextField key;
@FXML
private TextField value;
@FXML
private TextField expiration_time;
@FXML
private ComboBox<SignatureAlgorithm> comboBox;
@FXML
private ListView<Data> listView;
@FXML
private TextArea token;
private volatile Key KEY = null;
@FXML
public void initialize() {
ObservableList<SignatureAlgorithm> list = FXCollections.observableArrayList(SignatureAlgorithm.values());
comboBox.setItems(list);
comboBox.getSelectionModel().select(SignatureAlgorithm.ES256);
ObservableList<SignatureAlgorithm> list_1 = FXCollections.observableArrayList();
list_1.add(HS256);
list_1.add(HS384);
list_1.add(HS512);
comboBox.setItems(list_1);
comboBox.getSelectionModel().select(HS256);
listView.setPlaceholder(new Label("没有数据"));
listView.setItems(list);
listView.getSelectionModel().select(0);
listView.setFixedCellSize(40);
listView.setCellFactory(new Callback<>() {
@Override
public ListCell<Data> call(ListView<Data> dataListView) {
return new ListCell<>() {
@Override
protected void updateItem(Data data, boolean b) {
super.updateItem(data, b);
if (!b) {
HBox hBox = new HBox(15);
hBox.setAlignment(Pos.CENTER_RIGHT);
Label label = new Label(data.key + " -- " + data.value);
Button button = new Button("删除");
hBox.getChildren().addAll(label, button);
this.setGraphic(hBox);
button.setOnAction(event -> {
log.info("删除....");
list.remove(data);
});
}
}
};
}
});
token.setWrapText(true);
}
private String generateToken(Map<String, Object> claims,String secret,long expiration,SignatureAlgorithm signatureAlgorithm) {
@FXML
public void add() {
log.info("添加...");
if (key.getText().length() == 0) {
AlertUtils.alert_warning("key不能为空");
return;
}
if (value.getText().length() == 0) {
AlertUtils.alert_warning("value不能为空");
return;
}
Data data = new Data(key.getText(), value.getText());
list.add(data);
//清空
clear_input(key);
clear_input(value);
}
public void clear_input(TextField textField){
textField.setText(null);
}
@FXML
public void gen_jwt_token() {
String secret;
if (secret_key.getText().length() == 0) {
if (key.getText().length() == 0) {
AlertUtils.alert_warning("key不能为空");
return;
}
}
secret = secret_key.getText();
Map<String, Object> map = new HashMap<>();
list.forEach(i -> {
map.put(i.key, i.value);
});
log.info("map:{}", map);
long expiration = 0;
if (expiration_time.getText().length() != 0) {
expiration = System.currentTimeMillis() + Long.parseLong(expiration_time.getText());
}
SignatureAlgorithm signatureAlgorithm = comboBox.getSelectionModel().getSelectedItem();
String res = generateToken(map, secret, expiration, signatureAlgorithm);
log.info("token:{}", res);
token.setText(res);
}
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();
return switch (signatureAlgorithm){
case HS256, HS384, HS512 -> Jwts.builder().setClaims(claims).setExpiration(expirationDate).signWith(signatureAlgorithm, getKeyInstance(secret,signatureAlgorithm)).compact();
case RS256, RS384, RS512, PS256, PS384, PS512 -> "暂不支持,敬请期待!";
case ES256, ES384, ES512 -> "暂不支持,敬请期待!";
default -> throw new IllegalStateException("Unexpected value: " + signatureAlgorithm);
};
}
private Claims getClaimsFromToken(String token,String secret) {
public Key getKeyInstance(String secret,SignatureAlgorithm signatureAlgorithm) {
if (KEY == null) {
synchronized (JwtController.class) {
// 双重锁
if (KEY == null) {
byte[] apiKeySecretBytes = DatatypeConverter.parseBase64Binary(secret);
KEY = new SecretKeySpec(apiKeySecretBytes, signatureAlgorithm.getJcaName());
}
}
}
return KEY;
}
private Claims getClaimsFromToken(String token, String secret) {
Claims claims;
try {
claims = Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody();
@ -51,13 +184,43 @@ public class JwtController {
return claims;
}
public Boolean isTokenExpired(String token,String secret ) {
public Boolean isTokenExpired(String token, String secret) {
try {
Claims claims = getClaimsFromToken(token,secret);
Claims claims = getClaimsFromToken(token, secret);
Date expiration = claims.getExpiration();
return expiration.before(new Date());
} catch (Exception e) {
return false;
}
}
static class Data {
private String key;
private Object value;
public Data(String key, Object value) {
this.key = key;
this.value = value;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
public Data() {
}
}
}

View File

@ -160,7 +160,7 @@ public class PlayerController {
public ListCell<ResourcesUtils.Player> call(ListView<ResourcesUtils.Player> playerListView) {
Label label = new Label();
label.setPrefWidth(100);
label.setPrefWidth(200);
ListCell<ResourcesUtils.Player> listCell = new ListCell<>() {
@Override
protected void updateItem(ResourcesUtils.Player player, boolean b) {

View File

@ -162,7 +162,7 @@ public class SmallToolsController {
public ListCell<ResourcesUtils.SmallTools> call(ListView<ResourcesUtils.SmallTools> SmallToolsListView) {
Label label = new Label();
label.setPrefWidth(100);
label.setPrefWidth(200);
ListCell<ResourcesUtils.SmallTools> listCell = new ListCell<>() {
@Override
protected void updateItem(ResourcesUtils.SmallTools SmallTools, boolean b) {

View File

@ -3,25 +3,30 @@
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.TextArea?>
<?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="(毫秒)" />
<Label layoutX="437.0" layoutY="86.0" text="秘钥:" />
<TextField fx:id="secret_key" layoutX="578.0" layoutY="82.0" prefHeight="25.0" prefWidth="520.0" AnchorPane.bottomAnchor="542.0" AnchorPane.leftAnchor="578.0" AnchorPane.rightAnchor="102.0" AnchorPane.topAnchor="82.0" />
<ComboBox fx:id="comboBox" layoutX="578.0" layoutY="132.0" prefWidth="150.0" AnchorPane.bottomAnchor="492.0" AnchorPane.leftAnchor="578.0" AnchorPane.rightAnchor="472.0" AnchorPane.topAnchor="132.0" />
<Label layoutX="439.0" layoutY="136.0" text="加密方式:" />
<Button layoutX="578.0" layoutY="501.0" mnemonicParsing="false" onAction="#gen_jwt_token" prefHeight="25.0" prefWidth="188.0" text="生成" AnchorPane.bottomAnchor="123.0" AnchorPane.leftAnchor="578.0" AnchorPane.rightAnchor="434.0" AnchorPane.topAnchor="501.0" />
<Label layoutX="439.0" layoutY="195.0" text="加密的数据:" />
<Label layoutX="750.0" layoutY="187.0" text="(key)" />
<Label layoutX="961.0" layoutY="187.0" text="(value)" />
<TextField fx:id="key" layoutX="578.0" layoutY="183.0" AnchorPane.bottomAnchor="441.0" AnchorPane.leftAnchor="578.0" AnchorPane.rightAnchor="461.0" AnchorPane.topAnchor="183.0" />
<TextField fx:id="value" layoutX="794.0" layoutY="183.0" AnchorPane.bottomAnchor="441.0" AnchorPane.leftAnchor="794.0" AnchorPane.rightAnchor="245.0" AnchorPane.topAnchor="183.0" />
<Button layoutX="1058.0" layoutY="183.0" mnemonicParsing="false" onAction="#add" text="添加" AnchorPane.bottomAnchor="441.0" AnchorPane.rightAnchor="102.0" />
<Label layoutX="439.0" layoutY="235.0" text="过期时间:" />
<TextField fx:id="expiration_time" layoutX="578.0" layoutY="231.0" prefHeight="25.0" prefWidth="175.0" AnchorPane.bottomAnchor="393.0" AnchorPane.leftAnchor="578.0" AnchorPane.rightAnchor="447.0" AnchorPane.topAnchor="231.0" />
<Text layoutX="762.0" layoutY="248.302734375" strokeType="OUTSIDE" strokeWidth="0.0" text="(毫秒)" />
<ListView fx:id="listView" layoutX="167.0" layoutY="83.0" prefHeight="443.0" prefWidth="236.0" AnchorPane.bottomAnchor="123.0" AnchorPane.leftAnchor="167.0" AnchorPane.rightAnchor="797.0" AnchorPane.topAnchor="83.0" />
<TextArea fx:id="token" layoutX="578.0" layoutY="278.0" prefHeight="200.0" prefWidth="520.0" AnchorPane.bottomAnchor="171.0" AnchorPane.leftAnchor="578.0" AnchorPane.rightAnchor="102.0" AnchorPane.topAnchor="278.0" />
<Label layoutX="439.0" layoutY="278.0" text="token" />
</children>
</AnchorPane>

View File

@ -14,7 +14,7 @@
<Button fx:id="button" layoutX="930.0" layoutY="98.0" mnemonicParsing="false" text="转换" AnchorPane.rightAnchor="230.0" />
<Label layoutX="169.0" layoutY="160.0" text="unicode字符:" />
<TextArea fx:id="hex_16" layoutX="300.0" layoutY="160.0" prefHeight="121.0" prefWidth="619.0" AnchorPane.leftAnchor="300.0" AnchorPane.rightAnchor="281.0" AnchorPane.topAnchor="160.0" />
<Label layoutX="169.0" layoutY="369.0" text="将要转换的unicode字符:" AnchorPane.leftAnchor="169.0" AnchorPane.rightAnchor="918.0" />
<Label layoutX="169.0" layoutY="369.0" text="unicode字符:" AnchorPane.leftAnchor="169.0" AnchorPane.rightAnchor="918.0" />
<Separator layoutX="56.0" layoutY="317.0" prefHeight="5.0" prefWidth="1200.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" />
<TextField fx:id="text_filed1" layoutX="300.0" layoutY="365.0" prefHeight="25.0" prefWidth="619.0" AnchorPane.leftAnchor="300.0" AnchorPane.rightAnchor="281.0" />
<Button fx:id="button1" layoutX="930.0" layoutY="365.0" mnemonicParsing="false" text="转换" AnchorPane.rightAnchor="230.0" />