TokenController.java
2.2 KB
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
package com.aukey.example.web;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;
import java.util.concurrent.TimeUnit;
/**
* @author: wgf
* @create: 2020-05-13 10:11
* @description: 获取token回调controller
* 当向数据仓库平台发起 http://dw.aukeyit.com/api/authorize?appSecret=应用秘钥 请求时,
* 数据仓库验证应用秘钥后会生成token,并通过调用APP回调地址将token作为参数返回到客户端
**/
@RestController
@RequestMapping("/token")
@ApiIgnore
public class TokenController {
private Logger log = LoggerFactory.getLogger(TokenController.class);
/**
* 当前token
*/
private static String currentToken = null;
/*********************************************************************
* 这里的controller path 需要和数据仓库中APP设置的回调地址保持一致。 *
* 例如APP里回调地址为 http://localhost:8099/token/receive *
* TokenJob定时任务定时请求数据仓库获取授权API后 *
* 数据仓库回调 http://localhost:8099/token/receive,并且传递token参数 *
* 获取token后,可以放在 mysql,redis等。DEMO就直接放在内存中。 *
********************************************************************/
@PostMapping("/receive")
public void receive(String token) {
log.info("获取到远程DW服务回调请求,token:{}", token);
currentToken = token;
}
public static String getCurrentToken() {
for (int i = 0; i < 3; i++) {
if (StringUtils.isEmpty(currentToken)) {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
return currentToken;
}
}
return null;
}
}