GitBucket
4.23.0
Toggle navigation
Sign in
Files
Branches
1
Releases
Issues
Pull requests
Labels
Priorities
Milestones
Wiki
Forks
yn-bftl-byx
/
thirdparty
Browse code
加入日志
master
1 parent
27f47f8
commit
617db79b0a5fca92a718fe9fb9338ce6b4eb6e13
steven
authored
on 31 Dec
Patch
Showing
2 changed files
src/main/java/com/yn/bftl/thirdparty/modules/secret/controller/RandomKeyController.java
src/main/java/com/yn/bftl/thirdparty/modules/secret/service/impl/RandomKeyServiceImpl.java
Ignore Space
Show notes
View
src/main/java/com/yn/bftl/thirdparty/modules/secret/controller/RandomKeyController.java
package com.yn.bftl.thirdparty.modules.secret.controller; import com.yn.bftl.thirdparty.modules.secret.service.RandomKeyService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @Slf4j @Api(value = "AuthMemberController", tags = "加密随机因子") @RestController @RequestMapping("/random/secret") public class RandomKeyController { @Resource private RandomKeyService randomKeyService; @ApiOperation(value = "获取一个随机因子") @RequestMapping(value = "/get", method = RequestMethod.GET) public String get() { log.info("================================获取随机因子开始"); String randomKey = this.randomKeyService.getOneKey(); log.info("================================获取随机因子:" + randomKey); return randomKey; } }
package com.yn.bftl.thirdparty.modules.secret.controller; import com.yn.bftl.thirdparty.modules.secret.service.RandomKeyService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @Slf4j @Api(value = "AuthMemberController", tags = "加密随机因子") @RestController @RequestMapping("/random/secret") public class RandomKeyController { @Resource private RandomKeyService randomKeyService; @ApiOperation(value = "获取一个随机因子") @RequestMapping(value = "/get", method = RequestMethod.GET) public String get() { String randomKey = this.randomKeyService.getOneKey(); log.info("================================获取随机因子:" + randomKey); return randomKey; } }
Ignore Space
Show notes
View
src/main/java/com/yn/bftl/thirdparty/modules/secret/service/impl/RandomKeyServiceImpl.java
package com.yn.bftl.thirdparty.modules.secret.service.impl; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.yn.bftl.common.common.util.ByxRouteUtil; import com.yn.bftl.thirdparty.modules.chinaums.util.ByxChinaumsUtil; import com.yn.bftl.thirdparty.modules.secret.service.RandomKeyService; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.time.Duration; import java.time.LocalDateTime; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; @Slf4j @Service public class RandomKeyServiceImpl implements RandomKeyService { //申请随机因子数量 private final Integer applyCount = 100; // 创建一个容量为100的阻塞队列 private final BlockingQueue<String> queue = new LinkedBlockingQueue<>(applyCount); private LocalDateTime markedDate = LocalDateTime.now(); @Resource private ByxChinaumsUtil byxChinaumsUtil; public String getOneKey() { try { if (queue.size() < 1 || this.checkExpireTime()) { synchronized (this) { if (queue.size() > 0 && !this.checkExpireTime()) { return queue.take(); } // 发送请求去获取随机因子 // TODO: send JSONObject params = new JSONObject(); params.put("applyCount", applyCount); JSONObject result = byxChinaumsUtil.execute(ByxRouteUtil.ROUTE_GET_RANDOM_KEY, params, JSONObject.class); if (result == null) { log.error("随机因子创建失败,请联系管理员"); throw new RuntimeException("随机因子创建失败,请联系管理员"); } JSONArray jsonArray = result.getJSONArray("list"); this.addDataToQueue(jsonArray); //更新markedDate markedDate = LocalDateTime.now(); return queue.take(); } } return queue.take(); } catch (Exception e) { e.printStackTrace(); log.error(e.getMessage()); throw new RuntimeException("随机因子创建失败,请联系管理员"); } } private void addDataToQueue(JSONArray jsonArray) throws InterruptedException{ if (jsonArray == null) { log.error("获取随机因子失败,请联系管理员"); throw new RuntimeException("获取随机因子失败,请联系管理员"); } for (int i = 0; i < jsonArray.size(); i++) { String randomKey = jsonArray.getJSONObject(i).get("plugRandomKey").toString(); queue.put(randomKey); } } private boolean checkExpireTime() { try { // 获取当前时间 LocalDateTime now = LocalDateTime.now(); // 计算两个LocalDateTime之间的Duration Duration duration = Duration.between(markedDate, now); // 24 ---- 表示24小时,文档中限制的是24小时内未验证就会失效, long daysBetween = duration.toHours() / 24; if (daysBetween >= 1) { return true; } } catch (Exception e) { log.error(e.getMessage()); throw new RuntimeException(e); } return false; } }
package com.yn.bftl.thirdparty.modules.secret.service.impl; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.yn.bftl.common.common.util.ByxRouteUtil; import com.yn.bftl.thirdparty.modules.chinaums.util.ByxChinaumsUtil; import com.yn.bftl.thirdparty.modules.secret.service.RandomKeyService; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.time.Duration; import java.time.LocalDateTime; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; @Slf4j @Service public class RandomKeyServiceImpl implements RandomKeyService { //申请随机因子数量 private final Integer applyCount = 100; // 创建一个容量为10的阻塞队列 private final BlockingQueue<String> queue = new LinkedBlockingQueue<>(applyCount); private LocalDateTime markedDate = LocalDateTime.now(); @Resource private ByxChinaumsUtil byxChinaumsUtil; public String getOneKey() { try { if (queue.size() < 1 || this.checkExpireTime()) { synchronized (this) { if (queue.size() > 0 && !this.checkExpireTime()) { return queue.take(); } // 发送请求去获取随机因子 // TODO: send JSONObject params = new JSONObject(); params.put("applyCount", applyCount); JSONObject result = byxChinaumsUtil.execute(ByxRouteUtil.ROUTE_GET_RANDOM_KEY, params, JSONObject.class); if (result == null) { log.error("随机因子创建失败,请联系管理员"); throw new RuntimeException("随机因子创建失败,请联系管理员"); } JSONArray jsonArray = result.getJSONArray("list"); this.addDataToQueue(jsonArray); //更新markedDate markedDate = LocalDateTime.now(); return queue.take(); } } return queue.take(); } catch (Exception e) { e.printStackTrace(); log.error(e.getMessage()); throw new RuntimeException("随机因子创建失败,请联系管理员"); } } private void addDataToQueue(JSONArray jsonArray) throws InterruptedException{ if (jsonArray == null) { log.error("获取随机因子失败,请联系管理员"); throw new RuntimeException("获取随机因子失败,请联系管理员"); } for (int i = 0; i < jsonArray.size(); i++) { String randomKey = jsonArray.getJSONObject(i).get("plugRandomKey").toString(); queue.put(randomKey); } } private boolean checkExpireTime() { // 获取当前时间 LocalDateTime now = LocalDateTime.now(); // 计算两个LocalDateTime之间的Duration Duration duration = Duration.between(markedDate, now); // 24 ---- 表示24小时,文档中限制的是24小时内未验证就会失效, long daysBetween = duration.toHours() / 24; if (daysBetween >= 1) { return true; } return false; } }
Show line notes below