Commit 2c6f6cf7 by 墨竹

fix:好办登陆调整

parent 9f6a7538
......@@ -19,7 +19,6 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<libraryVersion>3.0-SNAPSHOT</libraryVersion>
<jwt.version>3.18.2</jwt.version>
</properties>
<distributionManagement>
<repository>
......@@ -52,11 +51,6 @@
<version>${haoban-base-api}</version>
</dependency>
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>${jwt.version}</version>
</dependency>
<dependency>
<groupId>com.gic</groupId>
<artifactId>gic-commons</artifactId>
<version>3.0-SNAPSHOT</version>
......
package com.gic.haoban.manage.web.controller;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gic.clerk.api.dto.ClerkDTO;
......@@ -15,11 +12,11 @@ import com.gic.enterprise.api.dto.StoreDTO;
import com.gic.enterprise.api.service.EnterpriseService;
import com.gic.haoban.app.customer.dto.StaffOpenRelatedDTO;
import com.gic.haoban.app.customer.service.api.service.StaffMemberRelationApiService;
import com.gic.haoban.base.api.common.pojo.dto.WellDoneLoginDTO;
import com.gic.haoban.common.anno.IgnoreLogin;
import com.gic.haoban.common.utils.AuthRequestWellDoneUtil;
import com.gic.haoban.common.utils.EntityUtil;
import com.gic.haoban.common.utils.HaobanResponse;
import com.gic.haoban.common.utils.JwtUtil;
import com.gic.haoban.manage.api.dto.*;
import com.gic.haoban.manage.api.enums.SecretTypeEnum;
import com.gic.haoban.manage.api.service.*;
......@@ -29,7 +26,6 @@ import com.gic.haoban.manage.web.qo.GetUserByMemberCodeQo;
import com.gic.haoban.manage.web.qo.MemberLoginQo;
import com.gic.haoban.manage.web.qo.StaffLoginQO;
import com.gic.haoban.manage.web.utils.IPAddressUtil;
import com.gic.haoban.manage.web.utils.JwtUtil;
import com.gic.haoban.manage.web.vo.AppStaffVo;
import com.gic.haoban.manage.web.vo.MemberSendMessageVo;
import com.gic.haoban.manage.web.vo.StoreMemberVO;
......@@ -189,12 +185,13 @@ public class WxEnterpriseInfoController extends WebBaseController {
if (staffDTO == null) {
return resultResponse(HaoBanErrCode.ERR_6);
}
String staffId = staffDTO.getStaffId();
Map<String, String> map = new HashMap<>();
map.put("staffId", staffDTO.getStaffId());
map.put("staffId", staffId);
map.put("wxEnterpriseId", staffDTO.getWxEnterpriseId());
map.put("phoneNumber", staffDTO.getPhoneNumber());
String token = JwtUtil.genToken(map);
AuthRequestWellDoneUtil.setAppLoginUser(token, BeanUtil.mapToBean(map, WellDoneLoginDTO.class, false));
AuthRequestWellDoneUtil.setAppLoginUser(staffId, token);
StaffLoginDTO staffLoginDTO = new StaffLoginDTO();
BeanUtils.copyProperties(staffLoginQO, staffLoginDTO);
staffLoginDTO.setStaffIp(IPAddressUtil.getIpAddress(request));
......@@ -215,18 +212,11 @@ public class WxEnterpriseInfoController extends WebBaseController {
@RequestMapping(value = "welldone-token-refresh", method = RequestMethod.GET)
@IgnoreLogin
public HaobanResponse welldoneTokenRefresh() {
String token = AuthRequestWellDoneUtil.getToken();
if (ObjectUtil.isNull(token)) {
String token = AuthRequestWellDoneUtil.refreshToken();
if (StringUtils.isBlank(token)) {
return resultResponse(HaoBanErrCode.ERR_30010);
}
Map<String, String> tokenMap = JwtUtil.parseToken(token);
if (MapUtil.isEmpty(tokenMap)) {
return resultResponse(HaoBanErrCode.ERR_30009);
}
AuthRequestWellDoneUtil.delToken();
String newToken = JwtUtil.genToken(tokenMap);
AuthRequestWellDoneUtil.setAppLoginUser(newToken, BeanUtil.mapToBean(tokenMap, WellDoneLoginDTO.class, false));
return resultResponse(HaoBanErrCode.ERR_1, newToken);
return resultResponse(HaoBanErrCode.ERR_1, token);
}
/**
......@@ -243,6 +233,7 @@ public class WxEnterpriseInfoController extends WebBaseController {
/**
* 会员小程序-获取登录用户信息
*
* @param qo
* @return
*/
......
package com.gic.haoban.manage.web.utils;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* 生成token
*
* @author mozhu
* @date 2021-12-10 14:53:34
*/
public class JwtUtil {
private static final Logger logger = LoggerFactory.getLogger(JwtUtil.class);
/**
* 密钥 base64加密过的
*/
public static final String SECRET_KEY = "Wb9^vZBjaYiFyhj@dDv!2FXSeEWxJauW";
/**
* 发行人
*/
public static final String ISSUER = "gic_haoban";
/**
* 过期时间-7天
*/
private static final long expire = 604800;
/**
* 生成token
*
* @param claims
* @return
*/
public static String genToken(Map<String, String> claims) {
long time = System.currentTimeMillis();
claims.put("requestTime", String.valueOf(time));
//使用HMAC256进行加密
Algorithm algorithm = Algorithm.HMAC256(SECRET_KEY);
//创建jwt
JWTCreator.Builder builder = JWT.create()
.withIssuer(ISSUER)
.withIssuedAt(new Date())
.withExpiresAt(new Date(time + expire));
//传入参数
claims.forEach(builder::withClaim);
//签名加密
return builder.sign(algorithm);
}
/**
* 解密token
*
* @param token
* @return
* @throws RuntimeException
*/
public static Map<String, String> parseToken(String token) {
Algorithm algorithm = null;
try {
//使用HMAC256进行加密
algorithm = Algorithm.HMAC256(SECRET_KEY);
} catch (IllegalArgumentException e) {
logger.error("解密jwt失败:{}", e.getMessage(), e);
return null;
}
//解密
JWTVerifier verifier = JWT.require(algorithm).withIssuer(ISSUER).build();
DecodedJWT jwt = verifier.verify(token);
Map<String, Claim> map = jwt.getClaims();
Map<String, String> resultMap = new HashMap<>();
map.forEach((k, v) -> resultMap.put(k, v.asString()));
return resultMap;
}
public static void main(String[] args) {
String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJwaG9uZU51bWJlciI6IjEzNTc2MjIzNDQ3IiwiaXNzIjoiZ2ljX2hhb2JhbiIsInd4RW50ZXJwcmlzZUlkIjoiY2E2NmEwMWI3OTQ3NGM0MGIzZTdjN2Y5M2RhZjFhM2IiLCJzdGFmZklkIjoiZWM0ODI0NzBiZmIxNGFlZjkxMTBhYTIwM2Y5MmRmYjgifQ.rcP4U42mvvNKiiJXWoHVL8ad7doFmfgt5LuigrI_m5I";
Map<String, String> stringStringMap = parseToken(token);
String s = genToken(stringStringMap);
System.out.println(s);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment