token获取,以及测试用例
This commit is contained in:
parent
a2fc4819ce
commit
e190bafc69
@ -0,0 +1,10 @@
|
|||||||
|
package cd.casic.ci.process.constant;
|
||||||
|
|
||||||
|
public class SastUrlConstant {
|
||||||
|
// 本地使用vpn调用内网ip
|
||||||
|
public static final String baseUrl="http://192.168.31.93";
|
||||||
|
// 远程服务调用 外网ip
|
||||||
|
// public static final String baseUrl="http://39.155.212.109:22880";
|
||||||
|
// 获取token
|
||||||
|
public static final String getToken="/api/login/noCaptcha";
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
package cd.casic.ci.process.dto.resp.sast;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class TokenResp {
|
||||||
|
private String accessToken;
|
||||||
|
private Integer expiresIn;
|
||||||
|
private Integer refreshExpiresIn;
|
||||||
|
private String refreshToken;
|
||||||
|
private String tokenType;
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package cd.casic.ci.process.process.service.sast;
|
||||||
|
|
||||||
|
import cd.casic.ci.process.dto.resp.sast.TokenResp;
|
||||||
|
|
||||||
|
public interface SastService {
|
||||||
|
String getToken();
|
||||||
|
}
|
@ -0,0 +1,69 @@
|
|||||||
|
package cd.casic.ci.process.process.service.sast.impl;
|
||||||
|
|
||||||
|
import cd.casic.ci.process.constant.SastUrlConstant;
|
||||||
|
import cd.casic.ci.process.dto.resp.sast.TokenResp;
|
||||||
|
import cd.casic.ci.process.process.service.sast.SastService;
|
||||||
|
import cd.casic.ci.process.properties.SastProperties;
|
||||||
|
import cd.casic.framework.redis.core.RedisTemplateUtils;
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.springframework.http.HttpEntity;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.HttpMethod;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import static cd.casic.ci.process.constant.SastUrlConstant.*;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class SastServiceImpl implements SastService {
|
||||||
|
@Resource
|
||||||
|
private RedisTemplateUtils redisTemplateUtils;
|
||||||
|
@Resource
|
||||||
|
private SastProperties sastProperties;
|
||||||
|
@Resource
|
||||||
|
private RestTemplate restTemplate;
|
||||||
|
public static final String tokenPrefix = "Bearer";
|
||||||
|
public static final String tokenHeaderKey = "authorization";
|
||||||
|
public static final String REDIS_SAST_TOKEN_KEY = "REDIS_SAST_TOKEN_KEY";
|
||||||
|
|
||||||
|
private TokenResp getTokenRemote(){
|
||||||
|
HttpHeaders httpHeaders = new HttpHeaders();
|
||||||
|
HttpEntity<SastProperties> httpEntity = new HttpEntity<SastProperties>(sastProperties,httpHeaders);
|
||||||
|
ResponseEntity<String> exchange = restTemplate.exchange(baseUrl+getToken, HttpMethod.POST, httpEntity, String.class, new HashMap<>());
|
||||||
|
String body = exchange.getBody();
|
||||||
|
JSONObject bodyObject = JSON.parseObject(body);
|
||||||
|
TokenResp tokenResp = new TokenResp();
|
||||||
|
tokenResp.setAccessToken(bodyObject.getString("access_token"));
|
||||||
|
tokenResp.setExpiresIn(bodyObject.getInteger("expires_in"));
|
||||||
|
tokenResp.setRefreshExpiresIn(bodyObject.getInteger("refresh_expires_in"));
|
||||||
|
tokenResp.setRefreshToken(bodyObject.getString("refresh_token"));
|
||||||
|
tokenResp.setTokenType(bodyObject.getString("token_type"));
|
||||||
|
return tokenResp;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getToken() {
|
||||||
|
Object o = redisTemplateUtils.get(REDIS_SAST_TOKEN_KEY);
|
||||||
|
String token = o instanceof String ? ((String) o) : "";
|
||||||
|
if (StringUtils.isEmpty(token)) {
|
||||||
|
synchronized(this){
|
||||||
|
o = redisTemplateUtils.get(REDIS_SAST_TOKEN_KEY);
|
||||||
|
token = o instanceof String ? ((String) o) : "";
|
||||||
|
if (StringUtils.isEmpty(token)) {
|
||||||
|
TokenResp tokenRemote = getTokenRemote();
|
||||||
|
String accessToken = tokenRemote.getAccessToken();
|
||||||
|
redisTemplateUtils.set(REDIS_SAST_TOKEN_KEY,accessToken,tokenRemote.getExpiresIn()*1000);
|
||||||
|
token = accessToken;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return token;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
package cd.casic.ci.process.properties;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@ConfigurationProperties(prefix = "sast")
|
||||||
|
@Component
|
||||||
|
@Data
|
||||||
|
public class SastProperties {
|
||||||
|
@Value("username")
|
||||||
|
private String username;
|
||||||
|
@Value("password")
|
||||||
|
private String password;
|
||||||
|
@Value("id")
|
||||||
|
private String id;
|
||||||
|
@Value("captcha")
|
||||||
|
private String captcha;
|
||||||
|
}
|
@ -70,7 +70,9 @@ spring:
|
|||||||
# Redis 配置。Redisson 默认的配置足够使用,一般不需要进行调优
|
# Redis 配置。Redisson 默认的配置足够使用,一般不需要进行调优
|
||||||
data:
|
data:
|
||||||
redis:
|
redis:
|
||||||
host: 127.0.0.1 # 地址
|
# host: 127.0.0.1 # 地址
|
||||||
|
# port: 16379 # 端口
|
||||||
|
host: 192.168.1.120 # 地址
|
||||||
port: 6379 # 端口
|
port: 6379 # 端口
|
||||||
database: 0 # 数据库索引
|
database: 0 # 数据库索引
|
||||||
# password: dev # 密码,建议生产环境开启
|
# password: dev # 密码,建议生产环境开启
|
||||||
@ -156,3 +158,8 @@ logging:
|
|||||||
org.springframework.context.support.PostProcessorRegistrationDelegate: ERROR # 禁用,Spring Boot 3.X 存在部分错误的 WARN 提示
|
org.springframework.context.support.PostProcessorRegistrationDelegate: ERROR # 禁用,Spring Boot 3.X 存在部分错误的 WARN 提示
|
||||||
|
|
||||||
debug: false
|
debug: false
|
||||||
|
sast:
|
||||||
|
username: admin@clouditera.com
|
||||||
|
password: Aa123456
|
||||||
|
id: clouditera
|
||||||
|
captcha: clouditera
|
||||||
|
70
ops-server/src/test/java/cd/casic/server/SastTest.java
Normal file
70
ops-server/src/test/java/cd/casic/server/SastTest.java
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
package cd.casic.server;
|
||||||
|
|
||||||
|
import cd.casic.ci.process.dto.resp.sast.TokenResp;
|
||||||
|
import cd.casic.ci.process.process.service.sast.SastService;
|
||||||
|
import cd.casic.ci.process.properties.SastProperties;
|
||||||
|
import cd.casic.framework.redis.core.RedisTemplateUtils;
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.core.task.TaskExecutor;
|
||||||
|
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||||
|
import org.springframework.test.context.ActiveProfiles;
|
||||||
|
|
||||||
|
import java.util.concurrent.BrokenBarrierException;
|
||||||
|
import java.util.concurrent.CyclicBarrier;
|
||||||
|
|
||||||
|
@SpringBootTest(classes = {OpsServerApplication.class})
|
||||||
|
@ActiveProfiles("local")
|
||||||
|
@Slf4j
|
||||||
|
public class SastTest {
|
||||||
|
@Resource
|
||||||
|
SastProperties sastProperties;
|
||||||
|
@Resource
|
||||||
|
SastService sastService;
|
||||||
|
@Resource
|
||||||
|
ThreadPoolTaskExecutor workerExecutor;
|
||||||
|
@Resource
|
||||||
|
RedisTemplateUtils redisTemplateUtils;
|
||||||
|
@Test
|
||||||
|
public void properties(){
|
||||||
|
System.out.println(JSON.toJSONString(sastProperties));
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
public void getToken(){
|
||||||
|
String token = sastService.getToken();
|
||||||
|
System.out.println(token);
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
public void getTokenThread(){
|
||||||
|
|
||||||
|
CyclicBarrier cyclicBarrier = new CyclicBarrier(2);
|
||||||
|
String token1 = "";
|
||||||
|
String token2 = "";
|
||||||
|
redisTemplateUtils.del("REDIS_SAST_TOKEN_KEY");
|
||||||
|
workerExecutor.submit(()->{
|
||||||
|
try {
|
||||||
|
cyclicBarrier.await();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
} catch (BrokenBarrierException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
log.info(sastService.getToken());
|
||||||
|
});
|
||||||
|
workerExecutor.submit(()->{
|
||||||
|
try {
|
||||||
|
cyclicBarrier.await();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
} catch (BrokenBarrierException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
log.info(sastService.getToken());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user