谷歌验证码

1
2
3
4
5
<dependency>
<groupId>com.warrenstrange</groupId>
<artifactId>googleauth</artifactId>
<version>1.5.0</version>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package utils;

import com.warrenstrange.googleauth.GoogleAuthenticator;
import com.warrenstrange.googleauth.GoogleAuthenticatorKey;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class GoogleAuthUtil {


/**
* 生成key
*/
public static String generaKey(){
GoogleAuthenticator gAuth = new GoogleAuthenticator();
GoogleAuthenticatorKey key = gAuth.createCredentials();
return key.getKey();
}

/**
* 生成二维码
* @param key 生成的key
* @param issuer app上title名字 服务名称 如: Google Github 印象笔记
* @param account app上title名字(括号里的) 用户账户 如: [email protected] 138XXXXXXXX
*/
public static String createGoogleAuthQRCodeData(String key, String account,String issuer) throws UnsupportedEncodingException {
return String.format("otpauth://totp/%s?secret=%s&issuer=%s",
URLEncoder.encode(issuer + ":" + account, "UTF-8").replace("+", "%20"),
URLEncoder.encode(key, "UTF-8").replace("+", "%20"),
URLEncoder.encode(issuer, "UTF-8").replace("+", "%20"));
}

public static void main(String[] args) throws UnsupportedEncodingException {
String s = generaKey();
System.out.println(s);
String googleAuthQRCodeData = createGoogleAuthQRCodeData(s, "anthony", "V16");
System.out.println(googleAuthQRCodeData);
}

/**
* 检查
* @param key 生成的key
* @param code 用户输入的面膜
*/
public static boolean check(String key,Integer code){
GoogleAuthenticator gAuth = new GoogleAuthenticator();
// 根据用户密钥和用户输入的密码,验证是否一致。
boolean isCodeValid = gAuth.authorize(key, code);
return isCodeValid;
}
}

图片验证码

1
2
3
4
5
<dependency>
<groupId>com.github.whvcse</groupId>
<artifactId>easy-captcha</artifactId>
<version>1.6.2</version>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@ApiOperation("获取验证码")
@PostMapping("/getVerify")
public Response<Map<String, String>> getVerify() {
try {
SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 6);
specCaptcha.setFont(new Font("Default", Font.PLAIN, 32));
String verCode = specCaptcha.text().toLowerCase();
String key = UUID.randomUUID().toString();
Map<String, String> stringStringHash = new HashMap<>();
stringStringHash.put("uuid", key);
stringStringHash.put("img", specCaptcha.toBase64());

key = "login_code:" + key;
redisDao.setString(key, verCode);
redisDao.expire(key, 2, TimeUnit.MINUTES);
return Response.successData(stringStringHash);
} catch (Exception e) {
throw new RuntimeException("验证码生成失败", e);
}
}

华为云上传图片

1
2
3
4
5
<dependency>
<groupId>com.huaweicloud</groupId>
<artifactId>esdk-obs-java-bundle</artifactId>
<version>3.23.9.1</version>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import com.obs.services.ObsClient;
import com.obs.services.ObsConfiguration;
import com.obs.services.exception.ObsException;
import com.obs.services.model.*;

import java.io.IOException;
import java.io.InputStream;

public class SimpleMultipartUploadSample {
// 通常一个链接是 https://桶的名字.obs.ap-southeast-1.myhuaweicloud.com/资源路径
private static final String endPoint = "https://obs.ap-southeast-1.myhuaweicloud.com";

private static final String ak = "xxxx";

private static final String sk = "xxxx";

private static ObsClient obsClient;

private static String bucketName = "桶的名字";


public static void upload(String objectKey, InputStream file) throws IOException {
ObsConfiguration config = new ObsConfiguration();
config.setSocketTimeout(30000);
config.setConnectionTimeout(10000);
config.setEndPoint(endPoint);
try {

obsClient = new ObsClient(ak, sk, config);

System.out.println("Step 1: initiate multipart upload \n");
InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest();
request.setBucketName(bucketName);
request.setObjectKey(objectKey);
InitiateMultipartUploadResult result = obsClient.initiateMultipartUpload(request);

System.out.println("Step 2: upload part \n");
UploadPartResult uploadPartResult = obsClient.uploadPart(bucketName, objectKey, result.getUploadId(), 1, file);

System.out.println("Step 3: complete multipart upload \n");
CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest();
completeMultipartUploadRequest.setBucketName(bucketName);
completeMultipartUploadRequest.setObjectKey(objectKey);
completeMultipartUploadRequest.setUploadId(result.getUploadId());
PartEtag partEtag = new PartEtag();
partEtag.setPartNumber(uploadPartResult.getPartNumber());
partEtag.setEtag(uploadPartResult.getEtag());
completeMultipartUploadRequest.getPartEtag().add(partEtag);
obsClient.completeMultipartUpload(completeMultipartUploadRequest);
} catch (ObsException e) {
System.out.println("Response Code: " + e.getResponseCode());
System.out.println("Error Message: " + e.getErrorMessage());
System.out.println("Error Code: " + e.getErrorCode());
System.out.println("Request ID: " + e.getErrorRequestId());
System.out.println("Host ID: " + e.getErrorHostId());
} finally {
if (obsClient != null) {
try {
obsClient.close();
} catch (IOException e) {
}
}
}

}

}