登录鉴权按照Source判断id来自于何处
This commit is contained in:
6
pom.xml
6
pom.xml
@@ -263,6 +263,12 @@
|
|||||||
<version>2.15.1</version>
|
<version>2.15.1</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-lang3</artifactId>
|
||||||
|
<version>3.13.0</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.stripe</groupId>
|
<groupId>com.stripe</groupId>
|
||||||
<artifactId>stripe-java</artifactId>
|
<artifactId>stripe-java</artifactId>
|
||||||
|
|||||||
@@ -4,11 +4,13 @@ import com.ai.da.common.response.Response;
|
|||||||
import com.google.common.collect.ImmutableMap;
|
import com.google.common.collect.ImmutableMap;
|
||||||
import com.ai.da.common.response.ResultEnum;
|
import com.ai.da.common.response.ResultEnum;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.validation.BindException;
|
import org.springframework.validation.BindException;
|
||||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
import org.springframework.web.bind.annotation.ResponseBody;
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author: dangweijian
|
* @author: dangweijian
|
||||||
@@ -36,6 +38,14 @@ public class ExceptionCatch {
|
|||||||
return Response.error(e.getCode(), e.getMsg());
|
return Response.error(e.getCode(), e.getMsg());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ResponseBody
|
||||||
|
@ResponseStatus(HttpStatus.UNAUTHORIZED)
|
||||||
|
@ExceptionHandler(UnauthorizedException.class)
|
||||||
|
public Response<String> unauthorizedExceptionCatch(UnauthorizedException e) {
|
||||||
|
log.error("Unauthorized: {}", e.getMessage());
|
||||||
|
return Response.error(401, e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
@ExceptionHandler(Exception.class)
|
@ExceptionHandler(Exception.class)
|
||||||
public Response<String> exceptionCatch(Exception e) {
|
public Response<String> exceptionCatch(Exception e) {
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package com.ai.da.common.config.exception;
|
||||||
|
|
||||||
|
public class UnauthorizedException extends RuntimeException {
|
||||||
|
|
||||||
|
public UnauthorizedException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public UnauthorizedException() {
|
||||||
|
super("Gateway token verification failed");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,17 +3,39 @@ package com.ai.da.common.context;
|
|||||||
import com.ai.da.model.vo.AuthPrincipalVo;
|
import com.ai.da.model.vo.AuthPrincipalVo;
|
||||||
|
|
||||||
public class UserContext {
|
public class UserContext {
|
||||||
private static ThreadLocal<AuthPrincipalVo> userHolder = new ThreadLocal<AuthPrincipalVo>();
|
private static final ThreadLocal<AuthPrincipalVo> userHolder = new ThreadLocal<>();
|
||||||
|
|
||||||
|
public static void setUserHolder(AuthPrincipalVo authPrincipalVo) {
|
||||||
|
userHolder.set(authPrincipalVo);
|
||||||
|
}
|
||||||
|
|
||||||
public static AuthPrincipalVo getUserHolder() {
|
public static AuthPrincipalVo getUserHolder() {
|
||||||
return userHolder.get();
|
AuthPrincipalVo holder = userHolder.get();
|
||||||
|
if (holder == null) {
|
||||||
|
throw new RuntimeException("User not authenticated");
|
||||||
|
}
|
||||||
|
if (!"AIDA".equals(holder.getSource())) {
|
||||||
|
throw new RuntimeException("Access denied: source must be AIDA");
|
||||||
|
}
|
||||||
|
return holder;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void delete() {
|
public static void delete() {
|
||||||
userHolder.remove();
|
userHolder.remove();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setUserHolder(AuthPrincipalVo authPrincipalVo) {
|
public static Long getUserId() {
|
||||||
userHolder.set(authPrincipalVo);
|
return getUserHolder().getId();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Long getBuyerId() {
|
||||||
|
AuthPrincipalVo holder = userHolder.get();
|
||||||
|
if (holder == null) {
|
||||||
|
throw new RuntimeException("User not authenticated");
|
||||||
|
}
|
||||||
|
if (!"BUYER".equals(holder.getSource())) {
|
||||||
|
throw new RuntimeException("Access denied: source must be BUYER");
|
||||||
|
}
|
||||||
|
return holder.getId();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -358,6 +358,8 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
|
|||||||
principal.setUsername(account.getUserName());
|
principal.setUsername(account.getUserName());
|
||||||
principal.setLanguage(account.getLanguage());
|
principal.setLanguage(account.getLanguage());
|
||||||
principal.setCountry(account.getCountry());
|
principal.setCountry(account.getCountry());
|
||||||
|
//区分买家端登录
|
||||||
|
principal.setSource("AIDA");
|
||||||
String token2 = tokenGenerateUtils.createToken(principal);
|
String token2 = tokenGenerateUtils.createToken(principal);
|
||||||
// 本地 JVM 缓存(适配旧逻辑)
|
// 本地 JVM 缓存(适配旧逻辑)
|
||||||
LocalCacheUtils.setTokenCache(String.valueOf(account.getId()), token2);
|
LocalCacheUtils.setTokenCache(String.valueOf(account.getId()), token2);
|
||||||
|
|||||||
@@ -11,14 +11,6 @@ spring:
|
|||||||
application:
|
application:
|
||||||
name: aida-back
|
name: aida-back
|
||||||
|
|
||||||
# ---------- 副数据源(back 私有,由 Nacos 统一管理) ----------
|
|
||||||
|
|
||||||
# ---------- Token 生成参数(由 TokenGenerateUtils 使用) ----------
|
|
||||||
security:
|
|
||||||
jwtSecret: JWTSECRET
|
|
||||||
jwtTokenHeader: Authorization
|
|
||||||
jwtTokenPrefix: Bearer-
|
|
||||||
jwtExpiration: 8640000000
|
|
||||||
|
|
||||||
# ---------- MinIO Buckets ----------
|
# ---------- MinIO Buckets ----------
|
||||||
minio:
|
minio:
|
||||||
|
|||||||
Reference in New Issue
Block a user