Browse Source

用户信息关联

master
maojian 2 months ago
parent
commit
5e7471e6fa
  1. 6
      opai-api/pom.xml
  2. 2
      opai-api/src/main/java/com/bw/opai/app/dto/Task.java
  3. 31
      opai-api/src/main/java/com/bw/opai/app/service/impl/AppServiceImpl.java
  4. 7
      opai-api/src/main/java/com/bw/opai/utils/Constants.java
  5. 37
      opai-api/src/main/java/com/bw/opai/utils/JwtUtil.java

6
opai-api/pom.xml

@ -85,7 +85,11 @@
<artifactId>mybatis-plus-boot-starter</artifactId> <artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3.4</version> <version>3.4.3.4</version>
</dependency> </dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>

2
opai-api/src/main/java/com/bw/opai/app/dto/Task.java

@ -57,7 +57,7 @@ public class Task {
/** /**
* 用户标识 * 用户标识
*/ */
private String userId;
private Integer userId;
/** /**
* 完成时间 * 完成时间

31
opai-api/src/main/java/com/bw/opai/app/service/impl/AppServiceImpl.java

@ -7,6 +7,8 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope; import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.auth.UsernamePasswordCredentials;
@ -18,6 +20,7 @@ import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -36,7 +39,9 @@ import com.bw.opai.app.service.AppService;
import com.bw.opai.common.Res; import com.bw.opai.common.Res;
import com.bw.opai.utils.Constants; import com.bw.opai.utils.Constants;
import com.bw.opai.utils.DownLoadUtil; import com.bw.opai.utils.DownLoadUtil;
import com.bw.opai.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -63,6 +68,14 @@ public class AppServiceImpl implements AppService {
@Value("${elasticsearch.index-name}") @Value("${elasticsearch.index-name}")
private String indexName; private String indexName;
// Nacos 加载密钥并设置一个默认值防止配置缺失导致启动失败
@Value("${jwt.secret:my_secret_key_default_2026}")
private String secret;
@Autowired
private HttpServletRequest request;
@Override @Override
public Res<?> getApps(Integer page, Integer size) { public Res<?> getApps(Integer page, Integer size) {
// 参数校验 // 参数校验
@ -133,6 +146,11 @@ public class AppServiceImpl implements AppService {
log.error("启动应用请求异常, param={},download error:{}", param, downloadRes); log.error("启动应用请求异常, param={},download error:{}", param, downloadRes);
return Res.fail("启动应用请求异常"); return Res.fail("启动应用请求异常");
} }
//获取Authorization
String authorization = request.getHeader(Constants.AUTHORIZATION);
//获取用户id
Claims claims = JwtUtil.getClaimsFromToken(authorization, secret);
int userId = (int) claims.get(Constants.USERID);
//任务录入表中 //任务录入表中
Task task = new Task(); Task task = new Task();
task.setId(taskId); task.setId(taskId);
@ -140,7 +158,7 @@ public class AppServiceImpl implements AppService {
task.setAppId(appId); task.setAppId(appId);
task.setDel(0); task.setDel(0);
task.setStatus(0); task.setStatus(0);
task.setUserId("1");
task.setUserId(userId);
taskMapper.insert(task); taskMapper.insert(task);
// ---------- 返回 ---------- // ---------- 返回 ----------
return Res.ok(task); return Res.ok(task);
@ -163,10 +181,11 @@ public class AppServiceImpl implements AppService {
} }
// 从登录上下文获取 userId你项目里已有 先写死 // 从登录上下文获取 userId你项目里已有 先写死
String userId = "1";
if (userId == null || userId.trim().equals("")) {
return Res.fail("未获取到用户信息");
}
String authorization = request.getHeader(Constants.AUTHORIZATION);
log.info("authorization:{}",authorization);
//获取用户id
Claims claims = JwtUtil.getClaimsFromToken(authorization, secret);
int userId = (int) claims.get(Constants.USERID);
try { try {
Page<Task> pageParam = new Page<>(page, size); Page<Task> pageParam = new Page<>(page, size);
@ -174,7 +193,7 @@ public class AppServiceImpl implements AppService {
LambdaQueryWrapper<Task> wrapper = new LambdaQueryWrapper<>(); LambdaQueryWrapper<Task> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(Task::getDel, 0) wrapper.eq(Task::getDel, 0)
.eq(Task::getAppId,appId) .eq(Task::getAppId,appId)
.eq(userId != null, Task::getUserId, userId)
.eq(Task::getUserId, userId)
.orderByDesc(Task::getCreateTime); .orderByDesc(Task::getCreateTime);
Page<Task> result = taskMapper.selectPage(pageParam, wrapper); Page<Task> result = taskMapper.selectPage(pageParam, wrapper);

7
opai-api/src/main/java/com/bw/opai/utils/Constants.java

@ -21,4 +21,11 @@ public class Constants {
* 任务id常量 * 任务id常量
*/ */
public static final String TASKID = "taskId"; public static final String TASKID = "taskId";
/**
*用户id
*/
public static final String USERID = "userId";
public static final String AUTHORIZATION = "Authorization";
} }

37
opai-api/src/main/java/com/bw/opai/utils/JwtUtil.java

@ -0,0 +1,37 @@
package com.bw.opai.utils;
import com.alibaba.fastjson.JSONObject;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
/**
* 各个系统之间互认的通行证工具类
* 密钥与配置均通过 Nacos 动态管理
*/
public class JwtUtil {
/**
* Token 中获取载荷
*/
public static Claims getClaimsFromToken(String token,String secret) {
try {
return Jwts.parser()
.setSigningKey(secret)
.parseClaimsJws(token)
.getBody();
} catch (Exception e) {
// 如果签名不对Token 过期会返回 null
return null;
}
}
public static void main(String[] args) {
String token = "eyJhbGciOiJIUzUxMiJ9.eyJhcHBJZCI6MiwiZXhwIjoxNzcwMTIwOTU5LCJ1c2VySWQiOjQsImlhdCI6MTc3MDExMzc1OSwidXNlcm5hbWUiOiJvcGFpQWRtaW4ifQ.wg9Yr30bRZxoTepmATx6B5xK2l7GCdedjHh2tU3w49-99z9_kHwPbTFC2thYd-Py5oQDwvsxBDscs0qoOKXHrg";
Claims c = JwtUtil.getClaimsFromToken(token, "");
System.out.println(JSONObject.toJSONString(c));
}
}
Loading…
Cancel
Save