Browse Source

token

lh_vote_java
lenghui 5 months ago
parent
commit
9f23212543
  1. 16
      pom.xml
  2. 13
      src/main/java/com/lh/bean/dto/TokenDTO.java
  3. 29
      src/main/java/com/lh/controller/VoteController.java
  4. 72
      src/main/java/com/lh/until/Utils.java

16
pom.xml

@ -58,6 +58,22 @@
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
<!-- Apache HttpClient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<!-- Jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>9.0.41</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>

13
src/main/java/com/lh/bean/dto/TokenDTO.java

@ -0,0 +1,13 @@
package com.lh.bean.dto;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class TokenDTO {
private int jwcode;
private String username;
}

29
src/main/java/com/lh/controller/VoteController.java

@ -1,29 +1,40 @@
package com.lh.controller;
import com.lh.bean.RespBean;
import com.lh.bean.Voter;
import com.lh.service.VoteService;
import com.lh.until.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
@CrossOrigin
@RestController
@RequestMapping
public class VoteController {
@Autowired
private VoteService voteService;
//投票
@PostMapping("/vote")
public RespBean vote(@RequestBody Voter voter) throws Exception {
String voterJwcode = voter.getJwCode();
String candidateJwcode = voter.getCandidateJwCode();
String voterName = voter.getName();
//投票.
@PostMapping("/vote/{candidateJwcode}")
public RespBean vote(@PathVariable("candidateJwcode") String candidateJwcode,@RequestBody String token) throws Exception {
token = "token=" + token.substring(0, token.length() - 1);
Utils utils = new Utils();
String voterJwcode = String.valueOf(utils.getJwcode(token));
String voterName = utils.getUsername(token);
Integer result =voteService.insertVote(voterJwcode, candidateJwcode, voterName);
return RespBean.ok("投票成功!今日还可以投" + result + "次");
}
//获取所有候选人
@GetMapping ("/getCandidates/{voterJwcode}")
public RespBean getCandidates(@PathVariable("voterJwcode") String voterJwcode) {
@PostMapping ("/getCandidates")
public RespBean getCandidates(@RequestBody String token) throws IOException {
//System.out.println(token);
////将token的值分离出来
//int startIndex = token.indexOf('=') + 1; // 找到等号的位置并移动到等号后一位
//String tokenValue = token.substring(startIndex); // 提取等号后面的部分
token = token.substring(10, token.length() - 2);
System.out.println(token);
String voterJwcode = String.valueOf(new Utils().getJwcode(token));
return RespBean.ok("获取成功",voteService.getCandidates(voterJwcode));
}

72
src/main/java/com/lh/until/Utils.java

@ -0,0 +1,72 @@
package com.lh.until;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lh.bean.dto.TokenDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
@Slf4j
@Component
public class Utils {
@Resource
private StringRedisTemplate stringRedisTemplate;
// 获取token中的信息
public TokenDTO analysisToken(String token) throws IOException {
// 编码Token
String url = "http://39.101.133.168:8828/hljw/api/v2/member/info";
// 创建HttpClient实例
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
// 创建POST请求
HttpPost postRequest = new HttpPost(url);
postRequest.setHeader("Content-Type", "application/x-www-form-urlencoded");
System.out.println(token);
// 设置请求体参数
StringEntity entity = new StringEntity(token, ContentType.APPLICATION_FORM_URLENCODED);
postRequest.setEntity(entity);
// 发送请求并获取响应
try (CloseableHttpResponse response = httpClient.execute(postRequest)) {
int responseCode = response.getStatusLine().getStatusCode(); // 获取状态码
// 检查响应状态
if (responseCode == 200) {
// 读取响应体
String responseBody = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
// 使用Jackson解析JSON响应体
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonResponse = objectMapper.readTree(responseBody);
JsonNode dataNode = jsonResponse.get("data");
TokenDTO tokenDTO = new TokenDTO();
tokenDTO.setJwcode(dataNode.get("jwcode").asInt());
tokenDTO.setUsername(dataNode.get("username").asText());
return tokenDTO;
}
else {
throw new RuntimeException("Failed : HTTP error code : " + responseCode);
}
}
}
}
// 获取token中的jwcode
public Integer getJwcode(String token) throws IOException {
TokenDTO dto = analysisToken(token);
return dto.getJwcode();
}
public String getUsername(String token) throws IOException {
TokenDTO dto = analysisToken(token);
return dto.getUsername();
}
}
Loading…
Cancel
Save