mirror of
https://gitee.com/lab1024/smart-admin.git
synced 2025-10-02 10:16:38 +08:00
add jwt decode and remove shopping cart API parameter memberId
This commit is contained in:
parent
2d1f2210bb
commit
cfcc3396ea
@ -29,6 +29,15 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
-->
|
-->
|
||||||
|
|
||||||
|
<!--JWT-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.auth0</groupId>
|
||||||
|
<artifactId>java-jwt</artifactId>
|
||||||
|
<version>3.8.2</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-aop</artifactId>
|
<artifactId>spring-boot-starter-aop</artifactId>
|
||||||
|
@ -1,47 +1,25 @@
|
|||||||
package net.lab1024.smartadmin.module.system.royalcanin;
|
package net.lab1024.smartadmin.module.system.royalcanin;
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import eu.bitwalker.useragentutils.UserAgent;
|
|
||||||
import io.jsonwebtoken.Claims;
|
|
||||||
import io.jsonwebtoken.Jwts;
|
|
||||||
import io.jsonwebtoken.SignatureAlgorithm;
|
|
||||||
import net.lab1024.smartadmin.common.constant.JudgeEnum;
|
|
||||||
import net.lab1024.smartadmin.common.heartbeat.StringUtil;
|
import net.lab1024.smartadmin.common.heartbeat.StringUtil;
|
||||||
import net.lab1024.smartadmin.module.business.log.LogService;
|
|
||||||
import net.lab1024.smartadmin.module.business.log.userloginlog.domain.UserLoginLogEntity;
|
|
||||||
import net.lab1024.smartadmin.module.system.royalcanin.member.*;
|
import net.lab1024.smartadmin.module.system.royalcanin.member.*;
|
||||||
import net.lab1024.smartadmin.util.MapRemoveNullUtil;
|
import net.lab1024.smartadmin.util.MapRemoveNullUtil;
|
||||||
|
import net.lab1024.smartadmin.util.SmartJWTUtil;
|
||||||
import net.lab1024.smartadmin.util.SmartHttpUtil;
|
import net.lab1024.smartadmin.util.SmartHttpUtil;
|
||||||
import net.lab1024.smartadmin.util.SmartIPUtil;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import java.time.LocalDateTime;
|
|
||||||
import java.time.ZoneId;
|
|
||||||
import java.util.Date;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class MemberService {
|
public class MemberService {
|
||||||
private String url = "http://miniapp-test.royalcanin.com.cn:7080/crm/member/";
|
private String url = "http://miniapp-test.royalcanin.com.cn:7080/crm/member/";
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private LogService logService;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 过期时间一天
|
|
||||||
*/
|
|
||||||
private static final int EXPIRE_SECONDS = 1 * 24 * 3600;
|
|
||||||
/**
|
|
||||||
* jwt加密字段
|
|
||||||
*/
|
|
||||||
private static final String CLAIM_ID_KEY = "id";
|
|
||||||
|
|
||||||
@Value("${jwt.key}")
|
|
||||||
private String jwtKey;
|
|
||||||
|
|
||||||
|
|
||||||
public String memberGet(MemberGetEntity memberGetEntity, HttpServletRequest request)throws Exception {
|
public String memberGet(MemberGetEntity memberGetEntity, HttpServletRequest request)throws Exception {
|
||||||
Map<String, String> paramMap = MapRemoveNullUtil.setConditionMap(memberGetEntity);
|
Map<String, String> paramMap = MapRemoveNullUtil.setConditionMap(memberGetEntity);
|
||||||
@ -52,7 +30,7 @@ public class MemberService {
|
|||||||
if(jsonObject.getString("code").equals("0")){
|
if(jsonObject.getString("code").equals("0")){
|
||||||
JSONObject json = JSONObject.parseObject(jsonObject.getString("data"));
|
JSONObject json = JSONObject.parseObject(jsonObject.getString("data"));
|
||||||
String id = json.getString("id");
|
String id = json.getString("id");
|
||||||
jsonObject.put("xaccessToken",generateToken(id));
|
jsonObject.put("xaccessToken", SmartJWTUtil.generateToken(id));
|
||||||
return jsonObject.toJSONString();
|
return jsonObject.toJSONString();
|
||||||
}else {
|
}else {
|
||||||
return sb;
|
return sb;
|
||||||
@ -83,7 +61,7 @@ public class MemberService {
|
|||||||
return SmartHttpUtil.httpPostRaw(url+"check",jsonObject.toString(),null,"utf-8");
|
return SmartHttpUtil.httpPostRaw(url+"check",jsonObject.toString(),null,"utf-8");
|
||||||
}
|
}
|
||||||
|
|
||||||
public String memberUpdate(MemberUpdateEntity memberUpdateEntity)throws Exception{
|
public String memberUpdate(MemberUpdateEntity memberUpdateEntity){
|
||||||
Map<String, String> paramMap = MapRemoveNullUtil.setConditionMap(memberUpdateEntity);
|
Map<String, String> paramMap = MapRemoveNullUtil.setConditionMap(memberUpdateEntity);
|
||||||
paramMap.put("id",StringUtil.toString(memberUpdateEntity.getId()).equals("0")?null:StringUtil.toString(memberUpdateEntity.getId()));
|
paramMap.put("id",StringUtil.toString(memberUpdateEntity.getId()).equals("0")?null:StringUtil.toString(memberUpdateEntity.getId()));
|
||||||
paramMap.put("cityId",StringUtil.toString(memberUpdateEntity.getCityId()).equals("0")?null:StringUtil.toString(memberUpdateEntity.getCityId()));
|
paramMap.put("cityId",StringUtil.toString(memberUpdateEntity.getCityId()).equals("0")?null:StringUtil.toString(memberUpdateEntity.getCityId()));
|
||||||
@ -94,25 +72,7 @@ public class MemberService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 功能描述: 生成JWT TOKEN
|
|
||||||
*
|
|
||||||
* @return
|
|
||||||
* @auther Carl
|
|
||||||
* @date 2021/12/8 0012
|
|
||||||
*/
|
|
||||||
public String generateToken(String UserId) {
|
|
||||||
Long id = Long.parseLong(UserId);
|
|
||||||
/**将token设置为jwt格式*/
|
|
||||||
String baseToken = UUID.randomUUID().toString();
|
|
||||||
LocalDateTime localDateTimeNow = LocalDateTime.now();
|
|
||||||
LocalDateTime localDateTimeExpire = localDateTimeNow.plusSeconds(EXPIRE_SECONDS);
|
|
||||||
Date from = Date.from(localDateTimeNow.atZone(ZoneId.systemDefault()).toInstant());
|
|
||||||
Date expire = Date.from(localDateTimeExpire.atZone(ZoneId.systemDefault()).toInstant());
|
|
||||||
|
|
||||||
Claims jwtClaims = Jwts.claims().setSubject(baseToken);
|
|
||||||
jwtClaims.put(CLAIM_ID_KEY, id);
|
|
||||||
String compactJws = Jwts.builder().setClaims(jwtClaims).setNotBefore(from).setExpiration(expire).signWith(SignatureAlgorithm.HS512, jwtKey).compact();
|
|
||||||
return compactJws;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -53,6 +53,9 @@ public class SMSController {
|
|||||||
String tamp = requestMap.get("tamp").toString();
|
String tamp = requestMap.get("tamp").toString();
|
||||||
String msgNum = requestMap.get("msgNum").toString();
|
String msgNum = requestMap.get("msgNum").toString();
|
||||||
String hash = MD5Utils.getMD5Code(KEY + "@" + tamp + "@" + msgNum);
|
String hash = MD5Utils.getMD5Code(KEY + "@" + tamp + "@" + msgNum);
|
||||||
|
if(tamp == "")
|
||||||
|
return ResponseDTO.wrap(OrderResponseCodeConst.SMS_FAIL);
|
||||||
|
|
||||||
if (tamp.compareTo(currentTime) > 0) {
|
if (tamp.compareTo(currentTime) > 0) {
|
||||||
if (hash.equalsIgnoreCase(requestHash)){
|
if (hash.equalsIgnoreCase(requestHash)){
|
||||||
//校验成功
|
//校验成功
|
||||||
|
@ -17,11 +17,13 @@ import net.lab1024.smartadmin.module.system.royalcanin.good.model.ProductMasterQ
|
|||||||
import net.lab1024.smartadmin.module.system.royalcanin.good.model.ProductQueryEntity;
|
import net.lab1024.smartadmin.module.system.royalcanin.good.model.ProductQueryEntity;
|
||||||
import net.lab1024.smartadmin.module.system.royalcanin.good.service.CartService;
|
import net.lab1024.smartadmin.module.system.royalcanin.good.service.CartService;
|
||||||
import net.lab1024.smartadmin.module.system.royalcanin.good.service.GoodService;
|
import net.lab1024.smartadmin.module.system.royalcanin.good.service.GoodService;
|
||||||
|
import net.lab1024.smartadmin.util.SmartJWTUtil;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -33,6 +35,8 @@ public class GoodController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private RoyalcaninOperateLogService royalcaninOperateLogService;
|
private RoyalcaninOperateLogService royalcaninOperateLogService;
|
||||||
|
|
||||||
|
public static final String TOKEN_NAME = "x-access-token";
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private CartService cartService;
|
private CartService cartService;
|
||||||
|
|
||||||
@ -125,12 +129,12 @@ public class GoodController {
|
|||||||
|
|
||||||
@ApiOperation(value = "删除购物车信息", notes = "删除购物车信息")
|
@ApiOperation(value = "删除购物车信息", notes = "删除购物车信息")
|
||||||
@PostMapping("royalcanin/cancelCartProductInfo")
|
@PostMapping("royalcanin/cancelCartProductInfo")
|
||||||
public ResponseDTO<String> cancelCartProductInfo(String memberId,@RequestBody List<String> productCode){
|
public ResponseDTO<String> cancelCartProductInfo(HttpServletRequest request, @RequestBody List<String> productCode){
|
||||||
|
String memberId = SmartJWTUtil.decodeToken(request.getHeader(TOKEN_NAME));
|
||||||
for (int i = 0 ;i < productCode.size();i++) {
|
for (int i = 0 ;i < productCode.size();i++) {
|
||||||
cartService.cancelProduct(memberId,productCode.get(i));
|
cartService.cancelProduct(memberId,productCode.get(i));
|
||||||
}
|
}
|
||||||
return ResponseDTO.succ();
|
return ResponseDTO.succ();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -14,7 +14,7 @@ public class MemberAddressAddEntity {
|
|||||||
/**
|
/**
|
||||||
* 会员ID
|
* 会员ID
|
||||||
*/
|
*/
|
||||||
private int memberId;
|
private String memberId;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 省份ID
|
* 省份ID
|
||||||
@ -24,12 +24,12 @@ public class MemberAddressAddEntity {
|
|||||||
/**
|
/**
|
||||||
* 城市ID
|
* 城市ID
|
||||||
*/
|
*/
|
||||||
private int cityId;
|
private String cityId;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 区ID
|
* 区ID
|
||||||
*/
|
*/
|
||||||
private int districtId;
|
private String districtId;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 地址
|
* 地址
|
||||||
|
@ -16,7 +16,7 @@ public class MemberAddressDeleteEntity {
|
|||||||
/**
|
/**
|
||||||
* 会员ID
|
* 会员ID
|
||||||
*/
|
*/
|
||||||
private int memberId;
|
private String memberId;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 宠物ID
|
* 宠物ID
|
||||||
|
@ -19,5 +19,5 @@ public class MemberAddressGetAllEntity {
|
|||||||
/**
|
/**
|
||||||
* 会员ID
|
* 会员ID
|
||||||
*/
|
*/
|
||||||
private int memberId;
|
private String memberId;
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,7 @@ public class MemberAddressUpdateEntity {
|
|||||||
/**
|
/**
|
||||||
* 会员ID
|
* 会员ID
|
||||||
*/
|
*/
|
||||||
private int memberId;
|
private String memberId;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 会员地址ID
|
* 会员地址ID
|
||||||
@ -32,12 +32,12 @@ public class MemberAddressUpdateEntity {
|
|||||||
/**
|
/**
|
||||||
* 城市ID
|
* 城市ID
|
||||||
*/
|
*/
|
||||||
private int cityId;
|
private String cityId;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 区ID
|
* 区ID
|
||||||
*/
|
*/
|
||||||
private int districtId;
|
private String districtId;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 地址
|
* 地址
|
||||||
|
@ -0,0 +1,85 @@
|
|||||||
|
package net.lab1024.smartadmin.util;
|
||||||
|
|
||||||
|
import com.auth0.jwt.JWT;
|
||||||
|
import com.auth0.jwt.JWTVerifier;
|
||||||
|
import com.auth0.jwt.algorithms.Algorithm;
|
||||||
|
import com.auth0.jwt.exceptions.JWTVerificationException;
|
||||||
|
import com.auth0.jwt.interfaces.DecodedJWT;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class SmartJWTUtil {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 过期时间一天
|
||||||
|
*/
|
||||||
|
private static final int EXPIRE_SECONDS = 1 * 24 * 3600;
|
||||||
|
/**
|
||||||
|
* jwt加密字段
|
||||||
|
*/
|
||||||
|
private static final String CLAIM_ID_KEY = "id";
|
||||||
|
|
||||||
|
//设置过期时间
|
||||||
|
private static final long EXPIRE_DATE = 30 * 60 * 100000;
|
||||||
|
//token秘钥
|
||||||
|
private static final String TOKEN_SECRET = "smart-admin-jwt-key";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static String generateToken(String userId) {
|
||||||
|
String token;
|
||||||
|
try {
|
||||||
|
//过期时间
|
||||||
|
Date date = new Date(System.currentTimeMillis() + EXPIRE_DATE);
|
||||||
|
//秘钥及加密算法
|
||||||
|
Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
|
||||||
|
//设置头部信息
|
||||||
|
Map<String, Object> header = new HashMap<>();
|
||||||
|
header.put("typ", "JWT");
|
||||||
|
header.put("alg", "HS256");
|
||||||
|
//携带UserId信息,生成签名
|
||||||
|
token = JWT.create()
|
||||||
|
.withHeader(header)
|
||||||
|
.withClaim(CLAIM_ID_KEY, userId)
|
||||||
|
.withExpiresAt(date)
|
||||||
|
.sign(algorithm);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return token;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* JWT解密
|
||||||
|
*
|
||||||
|
* @Author: RZH
|
||||||
|
* @Date: 2020/4/6 9:30
|
||||||
|
*/
|
||||||
|
public static String decodeToken(final String token) {
|
||||||
|
String userId = null;
|
||||||
|
try {
|
||||||
|
JWTVerifier verifier = JWT.require(Algorithm.HMAC256(TOKEN_SECRET))
|
||||||
|
.build();
|
||||||
|
DecodedJWT jwt = verifier.verify(token);
|
||||||
|
if (jwt != null) {
|
||||||
|
userId = jwt.getClaim(CLAIM_ID_KEY).asString();
|
||||||
|
}
|
||||||
|
} catch (JWTVerificationException exception) {
|
||||||
|
exception.printStackTrace();
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
String token = SmartJWTUtil.generateToken("844350");
|
||||||
|
System.out.println(SmartJWTUtil.decodeToken(token));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user