Добавил API get_request_token
This commit is contained in:
@ -273,6 +273,47 @@ public class MainController implements ServletContextAware {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
//------------------------------------------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------------------------------------------
|
||||||
|
//Одноразовый токен для каждого запроса ('X-Request-Token': token) с сохранением "User-Agent"
|
||||||
|
@Operation(summary = "Generate unique one-time request token", description = "Must be used in every data submission.")
|
||||||
|
@RequestMapping(value = "/get_request_token", method = {RequestMethod.GET}, produces = "application/json;charset=utf-8")
|
||||||
|
@ResponseBody
|
||||||
|
public ResponseEntity<Object> getRequestToken(HttpServletRequest request, @RequestParam(required = false, name = "lng", defaultValue = "1") String language_id) {
|
||||||
|
Translation trt = new Translation(language_id, jdbcTemplate);
|
||||||
|
try{
|
||||||
|
byte[] randomBytes = new byte[32];
|
||||||
|
new SecureRandom().nextBytes(randomBytes);
|
||||||
|
String token = Base64.getUrlEncoder().withoutPadding().encodeToString(randomBytes);
|
||||||
|
|
||||||
|
String userAgent = request.getHeader("User-Agent");
|
||||||
|
String userAgentHash = DigestUtils.sha256Hex(userAgent);
|
||||||
|
|
||||||
|
try (Cache cache = new Cache(redis_host, redis_port, redis_password)) {
|
||||||
|
cache.open();
|
||||||
|
cache.set(token, userAgentHash, access_time);
|
||||||
|
} catch (Exception e) {
|
||||||
|
String uuid = UUID.randomUUID().toString();
|
||||||
|
logger.error(uuid, e);
|
||||||
|
throw new CustomException(10000, trt.trt(false, "Internal_Server_Error"), null, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String, Object> response = new HashMap<>();
|
||||||
|
response.put("token", token);
|
||||||
|
response.put("ttl", access_time);
|
||||||
|
|
||||||
|
return ResponseEntity.ok(response);
|
||||||
|
} catch (CustomException e) {
|
||||||
|
if(e.isSaveToLog()) {
|
||||||
|
String uuid = UUID.randomUUID().toString();
|
||||||
|
logger.error(uuid, e);
|
||||||
|
}
|
||||||
|
return new ResponseEntity<>(e.getErrorResponseModel(), HttpStatus.INTERNAL_SERVER_ERROR);
|
||||||
|
} catch (Exception e) {
|
||||||
|
String uuid = UUID.randomUUID().toString();
|
||||||
|
logger.error(uuid, e);
|
||||||
|
return new ResponseEntity<>(new ErrorResponseModel(10000, trt.trt(false, "Internal_Server_Error"), null, uuid), HttpStatus.INTERNAL_SERVER_ERROR);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//------------------------------------------------------------------------------------------------------------------
|
||||||
@Operation(summary = "Get API version(date) of build", description = "Returns the date and API name")
|
@Operation(summary = "Get API version(date) of build", description = "Returns the date and API name")
|
||||||
@RequestMapping(value = "/",method = RequestMethod.GET,produces = "application/json;charset=utf-8")
|
@RequestMapping(value = "/",method = RequestMethod.GET,produces = "application/json;charset=utf-8")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
|
|||||||
Reference in New Issue
Block a user