SpringMvcSpringBoot中如何实现HTTP请求的加密与解密通信?
- 内容介绍
- 文章标签
- 相关推荐
本文共计691个文字,预计阅读时间需要3分钟。
前言:从去年10月份到现在,忙碌的没时间写博客了,今天就趁着给家里买干货的功夫来写一篇!
最近很多人问到下面的问题,我们不想在每个Controller方法收到字符串后再次调用解密,当然可以!
前言
从去年10月份到现在忙的没时间写博客了,今天就甩给大家一个干货吧!!!
近来很多人问到下面的问题
- 我们不想在每个Controller方法收到字符串报文后再调用一次解密,虽然可以完成,但是很low,且如果想不再使用加解密,修改起来很是麻烦。
- 我们想在使用Rest工具或swagger请求的时候不进行加解密,而在app调用的时候处理加解密,这可如何操作。
针对以上的问题,下面直接给出解决方案:
实现思路
- APP调用API的时候,如果需要加解密的接口,需要在")
.append("totp")
.append("/").append(formatLabel(issuer, accountName));
Map<String, String> parameter = new HashMap<String, String>();
/**
* github.com/google/google-authenticator/wiki/Key-Uri-Format
* The secret parameter is an arbitrary key value encoded in Base32 according to RFC 3548.
*/
parameter.put("secret", keyBase32);
if (issuer != null) {
if (issuer.contains(":")) {
throw new IllegalArgumentException("Issuer cannot contain the \':\' character.");
}
parameter.put("issuer", issuer);
}
parameter.put("algorithm", "SHA1");
parameter.put("digits", String.valueOf(config.getCodeDigits()));
parameter.put("period", String.valueOf(TimeUnit.MILLISECONDS.toSeconds(config.getTimeStepSizeInMillis())));
URLCodec urlCodec = new URLCodec();
if (!parameter.isEmpty()) {
url.append("?");
for(String key : parameter.keySet()) {
String value = parameter.get(key);
if (value == null){
continue;
}
value = urlCodec.encode(value);
url.append(key).append("=").append(value).append("&");
}
}
return url.toString();
}
private static final String DEFAULT_RANDOM_NUMBER_ALGORITHM = "SHA1PRNG";
private static final String DEFAULT_RANDOM_NUMBER_ALGORITHM_PROVIDER = "SUN";
private static final String HMAC_HASH_FUNCTION = "HmacSHA1";
private static final String HMAC_MD5_FUNCTION = "HmacMD5";
/**
* 基于时间 生成16位的 code
* @param key
* @param tm
* @return
*/
public String calculateCode16(byte[] key, long tm)
{
// Allocating an array of bytes to represent the specified instant
// of time.
byte[] data = new byte[8];
long value = tm;
// Converting the instant of time from the long representation to a
// big-endian array of bytes (RFC4226, 5.2. Description).
for (int i = 8; i-- > 0; value >>>= 8)
{
data[i] = (byte) value;
}
// Building the secret key specification for the HmacSHA1 algorithm.
SecretKeySpec signKey = new SecretKeySpec(key, HMAC_HASH_FUNCTION);
try
{
// Getting an HmacSHA1 algorithm implementation from the JCE.
Mac mac = Mac.getInstance(HMAC_HASH_FUNCTION);
// Initializing the MAC algorithm.
mac.init(signKey);
// Processing the instant of time and getting the encrypted data.
byte[] hash = mac.doFinal(data);
// Building the validation code performing dynamic truncation
// (RFC4226, 5.3. Generating an HOTP value)
int offset = hash[hash.length - 1] & 0xB;
// We are using a long because Java hasn't got an unsigned integer type
// and we need 32 unsigned bits).
long truncatedHash = 0;
for (int i = 0; i < 8; ++i)
{
truncatedHash <<= 8;
// Java bytes are signed but we need an unsigned integer:
// cleaning off all but the LSB.
truncatedHash |= (hash[offset + i] & 0xFF);
}
truncatedHash &= Long.MAX_VALUE;
truncatedHash %= 10000000000000000L;
// module with the maximum validation code value.
// Returning the validation code to the caller.
return String.format("%016d", truncatedHash);
} catch (InvalidKeyException e) {
throw new GoogleAuthenticatorException("The operation cannot be "
+ "performed now.");
} catch (NoSuchAlgorithmException ex) {
// We're not disclosing internal error details to our clients.
throw new GoogleAuthenticatorException("The operation cannot be "
+ "performed now.");
}
}
}
GoogleAuth其他代码 看这里
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。
本文共计691个文字,预计阅读时间需要3分钟。
前言:从去年10月份到现在,忙碌的没时间写博客了,今天就趁着给家里买干货的功夫来写一篇!
最近很多人问到下面的问题,我们不想在每个Controller方法收到字符串后再次调用解密,当然可以!
前言
从去年10月份到现在忙的没时间写博客了,今天就甩给大家一个干货吧!!!
近来很多人问到下面的问题
- 我们不想在每个Controller方法收到字符串报文后再调用一次解密,虽然可以完成,但是很low,且如果想不再使用加解密,修改起来很是麻烦。
- 我们想在使用Rest工具或swagger请求的时候不进行加解密,而在app调用的时候处理加解密,这可如何操作。
针对以上的问题,下面直接给出解决方案:
实现思路
- APP调用API的时候,如果需要加解密的接口,需要在")
.append("totp")
.append("/").append(formatLabel(issuer, accountName));
Map<String, String> parameter = new HashMap<String, String>();
/**
* github.com/google/google-authenticator/wiki/Key-Uri-Format
* The secret parameter is an arbitrary key value encoded in Base32 according to RFC 3548.
*/
parameter.put("secret", keyBase32);
if (issuer != null) {
if (issuer.contains(":")) {
throw new IllegalArgumentException("Issuer cannot contain the \':\' character.");
}
parameter.put("issuer", issuer);
}
parameter.put("algorithm", "SHA1");
parameter.put("digits", String.valueOf(config.getCodeDigits()));
parameter.put("period", String.valueOf(TimeUnit.MILLISECONDS.toSeconds(config.getTimeStepSizeInMillis())));
URLCodec urlCodec = new URLCodec();
if (!parameter.isEmpty()) {
url.append("?");
for(String key : parameter.keySet()) {
String value = parameter.get(key);
if (value == null){
continue;
}
value = urlCodec.encode(value);
url.append(key).append("=").append(value).append("&");
}
}
return url.toString();
}
private static final String DEFAULT_RANDOM_NUMBER_ALGORITHM = "SHA1PRNG";
private static final String DEFAULT_RANDOM_NUMBER_ALGORITHM_PROVIDER = "SUN";
private static final String HMAC_HASH_FUNCTION = "HmacSHA1";
private static final String HMAC_MD5_FUNCTION = "HmacMD5";
/**
* 基于时间 生成16位的 code
* @param key
* @param tm
* @return
*/
public String calculateCode16(byte[] key, long tm)
{
// Allocating an array of bytes to represent the specified instant
// of time.
byte[] data = new byte[8];
long value = tm;
// Converting the instant of time from the long representation to a
// big-endian array of bytes (RFC4226, 5.2. Description).
for (int i = 8; i-- > 0; value >>>= 8)
{
data[i] = (byte) value;
}
// Building the secret key specification for the HmacSHA1 algorithm.
SecretKeySpec signKey = new SecretKeySpec(key, HMAC_HASH_FUNCTION);
try
{
// Getting an HmacSHA1 algorithm implementation from the JCE.
Mac mac = Mac.getInstance(HMAC_HASH_FUNCTION);
// Initializing the MAC algorithm.
mac.init(signKey);
// Processing the instant of time and getting the encrypted data.
byte[] hash = mac.doFinal(data);
// Building the validation code performing dynamic truncation
// (RFC4226, 5.3. Generating an HOTP value)
int offset = hash[hash.length - 1] & 0xB;
// We are using a long because Java hasn't got an unsigned integer type
// and we need 32 unsigned bits).
long truncatedHash = 0;
for (int i = 0; i < 8; ++i)
{
truncatedHash <<= 8;
// Java bytes are signed but we need an unsigned integer:
// cleaning off all but the LSB.
truncatedHash |= (hash[offset + i] & 0xFF);
}
truncatedHash &= Long.MAX_VALUE;
truncatedHash %= 10000000000000000L;
// module with the maximum validation code value.
// Returning the validation code to the caller.
return String.format("%016d", truncatedHash);
} catch (InvalidKeyException e) {
throw new GoogleAuthenticatorException("The operation cannot be "
+ "performed now.");
} catch (NoSuchAlgorithmException ex) {
// We're not disclosing internal error details to our clients.
throw new GoogleAuthenticatorException("The operation cannot be "
+ "performed now.");
}
}
}
GoogleAuth其他代码 看这里
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

