package com.hxzk.controller;
|
|
import cloud.tianai.captcha.application.ImageCaptchaApplication;
|
import cloud.tianai.captcha.application.vo.CaptchaResponse;
|
import cloud.tianai.captcha.application.vo.ImageCaptchaVO;
|
import cloud.tianai.captcha.common.constant.CaptchaTypeConstant;
|
import cloud.tianai.captcha.common.response.ApiResponse;
|
import cloud.tianai.captcha.validator.common.model.dto.ImageCaptchaTrack;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
import lombok.Data;
|
import org.apache.commons.lang3.StringUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.core.ParameterizedTypeReference;
|
import org.springframework.http.HttpEntity;
|
import org.springframework.http.HttpMethod;
|
import org.springframework.http.ResponseEntity;
|
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.client.RestTemplate;
|
|
import javax.servlet.http.HttpServletRequest;
|
import javax.xml.ws.Response;
|
import java.io.BufferedReader;
|
import java.io.InputStreamReader;
|
import java.net.HttpURLConnection;
|
import java.net.URL;
|
import java.util.Collections;
|
import java.util.concurrent.ThreadLocalRandom;
|
|
@RestController
|
@RequestMapping("/")
|
public class CodeController {
|
@Autowired
|
private RestTemplate restTemplate;
|
|
@PostMapping("/code")
|
@ResponseBody
|
public CaptchaResponse<ImageCaptchaVO> genCaptcha(HttpServletRequest request, @RequestParam(value = "type", required = false) String type) {
|
// 构建请求参数
|
HttpEntity<String> entity = new HttpEntity<>("Parameters", null);
|
ResponseEntity<CaptchaResponse<ImageCaptchaVO>> response = restTemplate.exchange(
|
"http://localhost:8086/gen",
|
HttpMethod.GET,
|
entity,
|
new ParameterizedTypeReference<CaptchaResponse<ImageCaptchaVO>>() {},
|
"type", type
|
);
|
|
// 返回结果
|
return response.getBody();
|
}
|
|
@PostMapping("/checkCode")
|
public ApiResponse<?> checkCaptcha(@RequestBody Data data) {
|
// 远端 check 服务的 URL
|
String url = "http://localhost:8086/check";
|
|
// 发送 POST 请求到远端服务
|
ApiResponse<?> response = restTemplate.postForObject(url, data, ApiResponse.class);
|
|
if (response.isSuccess()) {
|
return ApiResponse.ofSuccess(Collections.singletonMap("id", data.getId()));
|
}
|
return response;
|
}
|
|
|
|
@lombok.Data
|
public static class Data {
|
private String id;
|
private ImageCaptchaTrack data;
|
}
|
}
|