From 4dc531fc2dcd6517fffa2e9760225f5705537258 Mon Sep 17 00:00:00 2001
From: k94314517 <8417338+k94314517@user.noreply.gitee.com>
Date: 星期五, 19 七月 2024 20:11:27 +0800
Subject: [PATCH] 代码提交
---
server/service/src/main/java/com/doumee/config/Jwt/WebMvcConfig.java | 85 ++++++++++++++++++++++++++++++------------
1 files changed, 61 insertions(+), 24 deletions(-)
diff --git a/server/service/src/main/java/com/doumee/config/Jwt/WebMvcConfig.java b/server/service/src/main/java/com/doumee/config/Jwt/WebMvcConfig.java
index c827e52..5d72f5a 100644
--- a/server/service/src/main/java/com/doumee/config/Jwt/WebMvcConfig.java
+++ b/server/service/src/main/java/com/doumee/config/Jwt/WebMvcConfig.java
@@ -1,17 +1,20 @@
package com.doumee.config.Jwt;
+import com.alibaba.fastjson.JSONObject;
import com.doumee.biz.system.SystemDictDataBiz;
-import com.doumee.config.annotation.ErpLoginRequired;
+import com.doumee.biz.zbom.model.zhongtai.ZTConstants;
+import com.doumee.config.annotation.UserLoginRequired;
import com.doumee.config.annotation.LoginRequired;
import com.doumee.core.constants.ResponseStatus;
import com.doumee.core.exception.BusinessException;
import com.doumee.core.utils.Constants;
import com.doumee.core.utils.redis.RedisUtil;
import com.doumee.dao.business.model.Member;
+import com.doumee.dao.business.model.Users;
import io.jsonwebtoken.JwtException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.boot.web.servlet.FilterRegistrationBean;
+import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
@@ -25,7 +28,6 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-import java.lang.reflect.Method;
import java.util.Objects;
@Configuration
@@ -40,6 +42,12 @@
@Autowired
private RedisTemplate<String,Object> redisTemplate;
+
+ /**
+ * 鏄惁寮�鍙戣��
+ */
+ @Value("${debug_model}")
+ private Boolean isDebug;
/**
* 娣诲姞鎷︽埅鍣�
@@ -60,7 +68,7 @@
// Method method = handlerMethod.getMethod();
- // 鏈� @LoginRequired 娉ㄨВ锛岄渶瑕佺櫥褰曡璇�
+ // 鏈� @LoginRequired 娉ㄨВ锛岄渶瑕佺櫥褰曡璇� 瀹㈡埛绔娇鐢�
if (beanType.isAnnotationPresent(LoginRequired.class) || handlerMethod.hasMethodAnnotation(LoginRequired.class)) {
//鑾峰彇token
String token = request.getHeader(JwtTokenUtil.HEADER_KEY); // 浠� http 璇锋眰澶翠腑鍙栧嚭 token
@@ -69,50 +77,79 @@
} else {
throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"鏈櫥褰�");
}
- } else if(beanType.isAnnotationPresent(ErpLoginRequired.class) || handlerMethod.hasMethodAnnotation(ErpLoginRequired.class)){
- try {
+ // 鏈� @UserLoginRequired 娉ㄨВ锛岄渶瑕佺櫥褰曡璇� 鍐呴儴浜哄憳浣跨敤
+ } else if(beanType.isAnnotationPresent(UserLoginRequired.class) || handlerMethod.hasMethodAnnotation(UserLoginRequired.class)){
//ERP 涓氬姟娉ㄨВ
String token = request.getHeader(JwtTokenUtil.HEADER_KEY);
- String redisToken = RedisUtil.getObject(redisTemplate,Constants.RedisKeys.ERP_TOKEN,String.class);
- if(StringUtils.isBlank(redisToken)||!token.equals(redisToken)){
- throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"鎺堟潈宸插け鏁�");
+ if (StringUtils.isNotBlank(token)) {
+ checkPersonnelLogin(request,response);
+ } else {
+ throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"鏈櫥褰�");
}
- request.setAttribute(JwtTokenUtil.HEADER_KEY,token);
- } catch (IllegalArgumentException | JwtException e) {
- throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"鎺堟潈宸插け鏁�");
- }
-// request.setAttribute("token", token);
}
return true;
}
};
- registry.addInterceptor(TokenInterceptor).addPathPatterns("/web/**","/visitbiz/**");
+ registry.addInterceptor(TokenInterceptor).addPathPatterns("/web/**");
}
-
public Boolean checkLogin(HttpServletRequest request, HttpServletResponse response){
String token = request.getHeader(JwtTokenUtil.HEADER_KEY);
try {
- //鍒ゆ柇Token鏄惁瓒呮椂
- boolean expiration = JwtTokenUtil.isTokenExpired(token);
- if (expiration) {
+ //鑾峰彇璐﹀彿ID
+ Long memberId = JwtTokenUtil.getJwtPayLoad(token).getUserId();
+ String tokenRedis = (String) redisTemplate.opsForValue().get(ZTConstants.CUSTOMER+"_"+memberId);
+ if(StringUtils.isNotBlank(tokenRedis)){
+ if(!tokenRedis.equals(token)){
+ throw new BusinessException(ResponseStatus.TOKEN_EXCEED_TIME.getCode(),"闀挎椂闂存湭鎿嶄綔,璇烽噸鏂扮櫥褰�");
+ }
+ }else{
throw new BusinessException(ResponseStatus.TOKEN_EXCEED_TIME.getCode(),"闀挎椂闂存湭鎿嶄綔,璇烽噸鏂扮櫥褰�");
}
- //鑾峰彇璐﹀彿ID
- String memberId = JwtTokenUtil.getJwtPayLoad(token).getMemberId();
Member member = dao.queryForObject(" select * from `member` where id = ? limit 1 ", new BeanPropertyRowMapper<>(Member.class),memberId );
if(Objects.isNull(member)){
throw new BusinessException(ResponseStatus.DATA_EMPTY);
}
- if(member.getIsdeleted()== Constants.ONE){
+ if(Constants.equalsInteger(member.getIsdeleted(),Constants.ONE)){
throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"鐢ㄦ埛宸插垹闄�,璇疯仈绯荤鐞嗗憳");
}
- if(member.getStatus() != Constants.ZERO){
+ if(!Constants.equalsInteger(member.getStatus(),Constants.ZERO)){
throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"鐢ㄦ埛宸茬鐢�,璇疯仈绯荤鐞嗗憳");
}
request.setAttribute(JwtTokenUtil.UserId_Name, memberId);
- request.setAttribute(JwtTokenUtil.MEMBER, member);
+ request.setAttribute(JwtTokenUtil.UserType, ZTConstants.CUSTOMER);
+ return true;
+ } catch (IllegalArgumentException | JwtException e) {
+ throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"鏈櫥褰�");
+ }
+ }
+
+ public Boolean checkPersonnelLogin(HttpServletRequest request, HttpServletResponse response){
+ String token = request.getHeader(JwtTokenUtil.HEADER_KEY);
+ try {
+ //鑾峰彇璐﹀彿ID
+ Long userId = JwtTokenUtil.getJwtPayLoad(token).getUserId();
+ String tokenRedis = (String) redisTemplate.opsForValue().get(ZTConstants.BUSINESS+"_"+userId);
+ if(StringUtils.isNotBlank(tokenRedis)){
+ if(!tokenRedis.equals(token)){
+ throw new BusinessException(ResponseStatus.TOKEN_EXCEED_TIME.getCode(),"闀挎椂闂存湭鎿嶄綔,璇烽噸鏂扮櫥褰�");
+ }
+ }else{
+ throw new BusinessException(ResponseStatus.TOKEN_EXCEED_TIME.getCode(),"闀挎椂闂存湭鎿嶄綔,璇烽噸鏂扮櫥褰�");
+ }
+ Users users = dao.queryForObject(" select * from `users` where id = ? limit 1 ", new BeanPropertyRowMapper<>(Users.class),userId);
+ if(Objects.isNull(users)){
+ throw new BusinessException(ResponseStatus.DATA_EMPTY);
+ }
+ if(Constants.equalsInteger(users.getIsdeleted(),Constants.ONE)){
+ throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"鐢ㄦ埛宸插垹闄�,璇疯仈绯荤鐞嗗憳");
+ }
+ if(!StringUtils.equals(users.getStatus(),Constants.ONE+"")){
+ throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"鐢ㄦ埛宸茬鐢�,璇疯仈绯荤鐞嗗憳");
+ }
+ request.setAttribute(JwtTokenUtil.UserId_Name, userId);
+ request.setAttribute(JwtTokenUtil.UserType, ZTConstants.BUSINESS);
return true;
} catch (IllegalArgumentException | JwtException e) {
throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"鏈櫥褰�");
--
Gitblit v1.9.3