3.4.0 with Angular
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
package org.maxkey.crypto.jwt;
|
||||
|
||||
import java.text.ParseException;
|
||||
|
||||
import com.nimbusds.jose.JOSEException;
|
||||
import com.nimbusds.jose.JWSAlgorithm;
|
||||
import com.nimbusds.jose.JWSHeader;
|
||||
import com.nimbusds.jose.JWSObject;
|
||||
import com.nimbusds.jose.JWSSigner;
|
||||
import com.nimbusds.jose.Payload;
|
||||
import com.nimbusds.jose.crypto.MACSigner;
|
||||
import com.nimbusds.jose.crypto.MACVerifier;
|
||||
import com.nimbusds.jose.jwk.KeyUse;
|
||||
import com.nimbusds.jose.jwk.OctetSequenceKey;
|
||||
import com.nimbusds.jose.util.Base64URL;
|
||||
|
||||
public class HMAC512Service {
|
||||
|
||||
public final static String MXK_AUTH_JWK = "mxk_auth_jwk";
|
||||
|
||||
JWSSigner signer;
|
||||
|
||||
MACVerifier verifier;
|
||||
|
||||
public HMAC512Service() {
|
||||
super();
|
||||
}
|
||||
|
||||
public HMAC512Service(String secretString) throws JOSEException {
|
||||
Base64URL secret=new Base64URL(secretString);
|
||||
OctetSequenceKey octKey= new OctetSequenceKey.Builder(secret)
|
||||
.keyID(MXK_AUTH_JWK)
|
||||
.keyUse(KeyUse.SIGNATURE)
|
||||
.algorithm(JWSAlgorithm.HS512)
|
||||
.build();
|
||||
signer = new MACSigner(octKey);
|
||||
verifier = new MACVerifier(octKey);
|
||||
}
|
||||
|
||||
public String sign(Payload payload) {
|
||||
try {
|
||||
// Prepare JWS object with payload
|
||||
JWSObject jwsObject = new JWSObject(new JWSHeader(JWSAlgorithm.HS256), payload);
|
||||
// Apply the HMAC
|
||||
jwsObject.sign(signer);
|
||||
String jwt = jwsObject.serialize();
|
||||
return jwt;
|
||||
} catch (JOSEException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public String sign(String payload) {
|
||||
return sign(new Payload(payload));
|
||||
}
|
||||
|
||||
|
||||
public boolean verify(String jwt) {
|
||||
try {
|
||||
JWSObject jwsObjected =JWSObject.parse(jwt);
|
||||
boolean isVerifier = verifier.verify(
|
||||
jwsObjected.getHeader(),
|
||||
jwsObjected.getSigningInput(),
|
||||
jwsObjected.getSignature());
|
||||
return isVerifier;
|
||||
}catch(JOSEException JOSEException) {
|
||||
|
||||
}catch(ParseException ParseException) {
|
||||
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
88
maxkey-common/src/main/java/org/maxkey/entity/Message.java
Normal file
88
maxkey-common/src/main/java/org/maxkey/entity/Message.java
Normal file
@@ -0,0 +1,88 @@
|
||||
package org.maxkey.entity;
|
||||
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
||||
public class Message<T> {
|
||||
|
||||
public final static int SUCCESS = 0; //成功
|
||||
public final static int ERROR = 1; //错误
|
||||
public final static int FAIL = 2; //失败
|
||||
public final static int INFO = 101; //信息
|
||||
public final static int PROMPT = 102; //提示
|
||||
public final static int WARNING = 103; //警告
|
||||
|
||||
int code;
|
||||
|
||||
String message;
|
||||
|
||||
T data;
|
||||
|
||||
public Message() {
|
||||
this.code = SUCCESS;
|
||||
}
|
||||
|
||||
public Message(int code) {
|
||||
this.code = code;
|
||||
}
|
||||
public Message(T data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public Message(int code, String message) {
|
||||
this.code = code;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public Message(int code, String message, T data) {
|
||||
this.code = code;
|
||||
this.message = message;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public Message(int code, T data) {
|
||||
this.code = code;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public void setCode(int code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public T getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public void setData(T data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public ResponseEntity<?> buildResponse() {
|
||||
return ResponseEntity.ok(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append("Message [code=");
|
||||
builder.append(code);
|
||||
builder.append(", message=");
|
||||
builder.append(message);
|
||||
builder.append(", data=");
|
||||
builder.append(data);
|
||||
builder.append("]");
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package org.maxkey.crypto.signature;
|
||||
|
||||
import org.maxkey.crypto.jwt.HMAC512Service;
|
||||
|
||||
import com.nimbusds.jose.JOSEException;
|
||||
|
||||
public class HMAC512ServiceTest {
|
||||
|
||||
public static void main(String[] args) throws JOSEException {
|
||||
// TODO Auto-generated method stub
|
||||
String key ="7heM-14BtxjyKPuH3ITIm7q2-ps5MuBirWCsrrdbzzSAOuSPrbQYiaJ54AeA0uH2XdkYy3hHAkTFIsieGkyqxOJZ_dQzrCbaYISH9rhUZAKYx8tUY0wkE4ArOC6LqHDJarR6UIcMsARakK9U4dhoOPO1cj74XytemI-w6ACYfzRUn_Rn4e-CQMcnD1C56oNEukwalf06xVgXl41h6K8IBEzLVod58y_VfvFn-NGWpNG0fy_Qxng6dg8Dgva2DobvzMN2eejHGLGB-x809MvC4zbG7CKNVlcrzMYDt2Gt2sOVDrt2l9YqJNfgaLFjrOEVw5cuXemGkX1MvHj6TAsbLg";
|
||||
HMAC512Service HMAC512Service = new HMAC512Service(key);
|
||||
String jwt = HMAC512Service.sign("hkkkk");
|
||||
boolean isverify = HMAC512Service.verify(jwt);
|
||||
System.out.println(isverify);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package org.maxkey.crypto.signature;
|
||||
|
||||
import java.text.ParseException;
|
||||
|
||||
import com.nimbusds.jose.JOSEException;
|
||||
import com.nimbusds.jose.JWSAlgorithm;
|
||||
import com.nimbusds.jose.JWSHeader;
|
||||
import com.nimbusds.jose.JWSObject;
|
||||
import com.nimbusds.jose.JWSSigner;
|
||||
import com.nimbusds.jose.Payload;
|
||||
import com.nimbusds.jose.crypto.MACSigner;
|
||||
import com.nimbusds.jose.crypto.MACVerifier;
|
||||
import com.nimbusds.jose.jwk.KeyUse;
|
||||
import com.nimbusds.jose.jwk.OctetSequenceKey;
|
||||
import com.nimbusds.jose.util.Base64URL;
|
||||
|
||||
public class HS512SignerTest {
|
||||
|
||||
public static void main(String[] args) throws JOSEException, ParseException {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
Base64URL secret=new Base64URL(
|
||||
"7heM-14BtxjyKPuH3ITIm7q2-ps5MuBirWCsrrdbzzSAOuSPrbQYiaJ54AeA0uH2XdkYy3hHAkTFIsieGkyqxOJZ_dQzrCbaYISH9rhUZAKYx8tUY0wkE4ArOC6LqHDJarR6UIcMsARakK9U4dhoOPO1cj74XytemI-w6ACYfzRUn_Rn4e-CQMcnD1C56oNEukwalf06xVgXl41h6K8IBEzLVod58y_VfvFn-NGWpNG0fy_Qxng6dg8Dgva2DobvzMN2eejHGLGB-x809MvC4zbG7CKNVlcrzMYDt2Gt2sOVDrt2l9YqJNfgaLFjrOEVw5cuXemGkX1MvHj6TAsbLg"
|
||||
);
|
||||
OctetSequenceKey octKey= new OctetSequenceKey.Builder(secret)
|
||||
.keyID("mxk_auth_jwk_secret")
|
||||
.keyUse(KeyUse.SIGNATURE)
|
||||
.algorithm(JWSAlgorithm.HS512)
|
||||
.build();
|
||||
System.out.println(octKey.toJSONString());
|
||||
// Create HMAC signer
|
||||
JWSSigner signer = new MACSigner(octKey);
|
||||
|
||||
// Prepare JWS object with "Hello, world!" payload
|
||||
JWSObject jwsObject = new JWSObject(new JWSHeader(JWSAlgorithm.HS256), new Payload("Hello, world!"));
|
||||
|
||||
// Apply the HMAC
|
||||
jwsObject.sign(signer);
|
||||
String s = jwsObject.serialize();
|
||||
System.out.println(s);
|
||||
|
||||
JWSObject jwsObjected =JWSObject.parse(s);
|
||||
MACVerifier verifier = new MACVerifier(octKey);
|
||||
boolean isVerifier = verifier.verify(jwsObjected.getHeader(), jwsObjected.getSigningInput(), jwsObjected.getSignature());
|
||||
System.out.println(isVerifier);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user