DID登陆是未来最流行的登陆方式
One-click Login with Blockchain: A MetaMask Tutorial
public static void main(String[] args) {
Credentials credentials = Credentials.create(privatekey);
String message = "Hello";
byte[] hashMsg = Hash.sha3(message.getBytes());
Sign.SignatureData signature = Sign.signMessage(hashMsg, credentials.getEcKeyPair(), false);
byte[] sig_data = ByteBuffer.allocate(signature.getR().length + signature.getS().length + signature.getV().length)
.put(signature.getR())
.put(signature.getS())
.put(signature.getV())
.array();
final String hexSig = Numeric.toHexStringNoPrefix(sig_data);
System.out.printf("签名后的字符串:%s\n", hexSig);
}
接口实现
@PostMapping("/oauthLogin/metamask")
public ResultVo metaMaskLogin(@Valid @RequestBody MetamaskLoginReq metamaskLoginReq) {
String publicAddress = metamaskLoginReq.getPublicAddress();
String signature = metamaskLoginReq.getSignature();
String message = metamaskLoginReq.getMessage();
// 地址合法性校验
if (!WalletUtils.isValidAddress(publicAddress)) {
return ResultVo.fail("地址格式非法!");
}
// 校验签名信息
if (!CryptoUtils.validate(signature, message, publicAddress)) {
return ResultVo.fail("签名校验失败!");
}
//认证通过之后 开始业务处理
return ResultVo.success();
}
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!