Commit 66d83a54 authored by 霍传世's avatar 霍传世

HaiNaAi测试小程序

parent 9cd9ac1b
...@@ -40,6 +40,30 @@ ...@@ -40,6 +40,30 @@
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20250107</version> <!-- 使用最新版本 -->
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>
......
package com.yuda.hainafacetofaceai.Interceptor;
import lombok.extern.slf4j.Slf4j;
import netscape.javascript.JSObject;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
@Component
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {
@Autowired
private StringRedisTemplate redisTemplate;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
log.info("登录拦截认证");
String key = request.getHeader("Token");
if(key!=null&&redisTemplate.opsForValue().get(key)!=null){
String value = redisTemplate.opsForValue().get(key);
JSONObject jsonObject = new JSONObject(value);
String zkzNum = jsonObject.get("zkzNum").toString();
log.info("学号"+zkzNum+"->"+"登录");
redisTemplate.opsForValue().set(key,value);
return true;
}else{
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
Map data = new HashMap<String,Object>(){{
put("tag",1);
put("code",50001);
put("message","登录状态过期");
}};
response.getWriter().write(new JSONObject(data).toString());
return false;
}
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
}
package com.yuda.hainafacetofaceai.config;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class AppConfig {
@Bean
public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder){
return restTemplateBuilder.build();
}
}
package com.yuda.hainafacetofaceai.config;
import com.yuda.hainafacetofaceai.Interceptor.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
@Autowired
private LoginInterceptor loginInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(loginInterceptor).addPathPatterns("/businessApi/**").excludePathPatterns("/login");;
}
}
package com.yuda.hainafacetofaceai.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("*")
.allowCredentials(true); // 允许发送 cookies
}
}
\ No newline at end of file
package com.yuda.hainafacetofaceai.constant;
public class AppStaticParams {
public static final String appKey = "HN62b8cc6dc1e86";
public static final String appSecret = "d7b8fdd207fbe8ac3ca278ec5fc1f5c4";
}
package com.yuda.hainafacetofaceai.constant;
public class AppThirdPartUrls {
public static final String examList = "https://openapi.hina.com/api/aiExam/examList";
public static final String candidateCreate = "https://openapi.hina.com/api/aiExam/candidateCreate";
public static final String candidateGet = "https://openapi.hina.com/api/aiExam/candidateCreate";
public static final String candidateSendMessage = "https://openapi.hina.com/api/aiExam/candidateCreate";
public static final String candidateMDelete = "https://openapi.hina.com/api/aiExam/candidateCreate";
}
package com.yuda.hainafacetofaceai.controller;
import com.yuda.hainafacetofaceai.entity.ExamStudent;
import com.yuda.hainafacetofaceai.util.AppUtil;
import org.apache.tomcat.util.security.MD5Encoder;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.sql.Time;
import java.util.concurrent.TimeUnit;
@RestController
@RequestMapping("/login")
public class LoginController {
@Autowired
private StringRedisTemplate redisTemplate;
@Autowired
private AppUtil appUtil;
@PostMapping
public ResponseEntity LoginGo(@RequestBody ExamStudent examStudent){
if(examStudent.getZkzNum().equals("9053464144")&&examStudent.getIdCard().equals("039519938378128944")){
redisTemplate.opsForValue().set("51096606163",new JSONObject(examStudent).toString(),2*60*60, TimeUnit.SECONDS);
return ResponseEntity.ok("51096606163");
}
return ResponseEntity.badRequest().body("账号密码错误");
}
}
package com.yuda.hainafacetofaceai.controller; package com.yuda.hainafacetofaceai.controller;
import com.yuda.hainafacetofaceai.constant.AppThirdPartUrls;
import com.yuda.hainafacetofaceai.entity.CandidateCreateQuery;
import com.yuda.hainafacetofaceai.entity.CandidateMDelete;
import com.yuda.hainafacetofaceai.entity.ExamQuery;
import com.yuda.hainafacetofaceai.util.AppUtil;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.bind.annotation.RequestMapping; import java.util.HashMap;
import org.springframework.web.bind.annotation.RestController; import java.util.Map;
@RestController @RestController
@RequestMapping("/businessApi") @RequestMapping("/businessApi")
@Slf4j
public class businessController { public class businessController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private AppUtil appUtil;
@PostMapping("/examList")
public ResponseEntity examList(@RequestBody ExamQuery examQuery){
HttpHeaders headers = new HttpHeaders();
headers.set("Content-Type","application/json");
JSONObject requestBodyObject = new JSONObject(examQuery);
HttpEntity<String> entity = new HttpEntity<>(requestBodyObject.toString(),headers);
ResponseEntity<String> response = restTemplate.exchange(
appUtil.ApiUrlGen(AppThirdPartUrls.examList),
HttpMethod.POST,
entity,
String.class
);
return ResponseEntity.ok(response.getBody());
}
@PostMapping("/candidateCreate")
public ResponseEntity candidateCreate(@RequestBody CandidateCreateQuery candidateCreateQuery){
HttpHeaders headers = new HttpHeaders();
headers.set("Content-Type","application/json");
JSONObject requestBodyObject = new JSONObject(candidateCreateQuery);
HttpEntity<JSONObject> entity = new HttpEntity<>(requestBodyObject,headers);
ResponseEntity<JSONObject> response = restTemplate.exchange(
appUtil.ApiUrlGen(AppThirdPartUrls.candidateCreate),
HttpMethod.POST,
entity,
JSONObject.class
);
return ResponseEntity.ok(response.getBody());
}
@PostMapping("/candidateGet")
public ResponseEntity candidateGet(@RequestParam("outId")String outId){
HttpHeaders headers = new HttpHeaders();
headers.set("Content-Type","application/json");
Map<String,Object> map = new HashMap<String,Object>();
map.put("outId",outId);
JSONObject requestBodyObject = new JSONObject(map);
HttpEntity<JSONObject> entity = new HttpEntity<>(requestBodyObject,headers);
ResponseEntity<JSONObject> response = restTemplate.exchange(
appUtil.ApiUrlGen(AppThirdPartUrls.candidateGet),
HttpMethod.POST,
entity,
JSONObject.class
);
return ResponseEntity.ok(response.getBody());
}
@PostMapping("/candidateSendMessage")
public ResponseEntity candidateSendMessage(@RequestParam("outId")String outId){
HttpHeaders headers = new HttpHeaders();
headers.set("Content-Type","application/json");
Map<String,Object> map = new HashMap<String,Object>();
map.put("outId",outId);
JSONObject requestBodyObject = new JSONObject(map);
HttpEntity<JSONObject> entity = new HttpEntity<>(requestBodyObject,headers);
ResponseEntity<JSONObject> response = restTemplate.exchange(
appUtil.ApiUrlGen(AppThirdPartUrls.candidateGet),
HttpMethod.POST,
entity,
JSONObject.class
);
return ResponseEntity.ok(response.getBody());
}
@PostMapping("/candidateMDelete")
public ResponseEntity candidateMDelete(@RequestBody CandidateMDelete candidateMDelete){
HttpHeaders headers = new HttpHeaders();
headers.set("Content-Type","application/json");
JSONObject requestBodyObject = new JSONObject(candidateMDelete);
HttpEntity<JSONObject> entity = new HttpEntity<>(requestBodyObject,headers);
ResponseEntity<JSONObject> response = restTemplate.exchange(
appUtil.ApiUrlGen(AppThirdPartUrls.candidateGet),
HttpMethod.POST,
entity,
JSONObject.class
);
return ResponseEntity.ok(response.getBody());
}
} }
package com.yuda.hainafacetofaceai.entity;
import lombok.Data;
import org.json.JSONObject;
@Data
public class CandidateCreateQuery {
private String examConnectCode;
private String outId;
private String showName;
private String phone;
private String email;
private JSONObject param;
private String sendMessage;
private String repeatVersion;
}
package com.yuda.hainafacetofaceai.entity;
import lombok.Data;
import java.util.List;
@Data
public class CandidateMDelete {
private List<String> outIdList;
}
package com.yuda.hainafacetofaceai.entity;
import lombok.Data;
@Data
public class ExamQuery {
private String examName;
private String examRemark;
private String examConnectCode;
}
package com.yuda.hainafacetofaceai.entity;
import lombok.Data;
@Data
public class ExamStudent {
private String zkzNum;
private String idCard;
}
package com.yuda.hainafacetofaceai.util;
import com.yuda.hainafacetofaceai.constant.AppStaticParams;
import jdk.nashorn.internal.runtime.logging.Logger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
@Component
@Slf4j
public class AppUtil {
public String ApiUrlGen(String url){
String timestamp = String.valueOf(System.currentTimeMillis());
timestamp = timestamp.substring(0, 10);
String str = AppStaticParams.appKey + timestamp + AppStaticParams.appSecret;
String md5Str = md5(str);
String suffixParams = "?appKey="+ AppStaticParams.appKey+"&sign="+md5Str+"&timestamp="+timestamp;
return url+suffixParams;
}
public String md5(String input) {
try {
// 创建 MD5 摘要实例
MessageDigest md = MessageDigest.getInstance("MD5");
// 计算 MD5 值
byte[] hashBytes = md.digest(input.getBytes());
// 转换为 32 位小写字符串
StringBuilder sb = new StringBuilder();
for (byte b : hashBytes) {
sb.append(String.format("%02x", b)); // 格式化成 16 进制
}
return sb.toString(); // 返回 32 位 MD5 字符串
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
}
spring.application.name=HaiNaFaceToFaceAI spring.application.name=HaiNaFaceToFaceAI
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=
spring.redis.database=1
spring.redis.timeout=2000
package com.yuda.hainafacetofaceai; package com.yuda.hainafacetofaceai;
import com.yuda.hainafacetofaceai.util.AppUtil;
import org.apache.tomcat.util.security.MD5Encoder;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest @SpringBootTest
class HaiNaFaceToFaceAiApplicationTests { class HaiNaFaceToFaceAiApplicationTests {
@Autowired
private AppUtil appUtil;
@Test @Test
void contextLoads() { void contextLoads() {
System.out.println(appUtil.md5("51096606163"));
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment