想和公众号小程序对接,该通过什么方式认证?可以实现在调用接口时能获取调用方token,或者租户信息
我也遇到过这个问题,我的方法是按照后台重新写2个工具类,一个是创建token的工具类,一个是根据当前请求头取到token获取当前用户信息的类
创建token
import org.springblade.core.launch.constant.TokenConstant; import org.springblade.core.secure.TokenInfo; import org.springblade.core.secure.utils.SecureUtil; import org.springblade.core.tool.support.Kv; import org.springblade.core.tool.utils.Func; import org.springblade.modules.plate.entity.PlateMember; import javax.servlet.http.HttpServletResponse; import java.util.HashMap; import java.util.Map; /** * 认证工具类 * * @author yun.can */ public class WechatTokenUtil { public final static String OPEN_ID = "openId"; public final static String PLATE_LICENSE = "license"; public final static String PLATE_MOBILE = "mobile"; public final static String PLATE_UNION_ID = "unionId"; /** * 创建认证token * * @param user 用户信息 * @return token */ public static Kv createAuthInfo(PlateMember user) { Kv authInfo = Kv.create(); //设置jwt参数 Mapparam = new HashMap<>(16); param.put(TokenConstant.TOKEN_TYPE, TokenConstant.ACCESS_TOKEN); param.put(TokenConstant.TENANT_ID, user.getTenantId()); param.put(TokenConstant.USER_ID, Func.toStr(user.getId())); param.put(TokenConstant.NICK_NAME, user.getNickName()); param.put(TokenConstant.AVATAR, user.getAvatar()); param.put(OPEN_ID, user.getOpenId()); param.put(PLATE_LICENSE, user.getLicense()); param.put(PLATE_MOBILE, user.getMobile()); param.put(PLATE_UNION_ID, user.getUnionid()); //拼装accessToken try { TokenInfo accessToken = SecureUtil.createJWT(param, "audience", "issuser", TokenConstant.ACCESS_TOKEN); //返回accessToken return authInfo.set(TokenConstant.TENANT_ID, user.getTenantId()) .set(TokenConstant.USER_ID, Func.toStr(user.getId())) .set(TokenConstant.NICK_NAME, user.getNickName()) .set(TokenConstant.AVATAR, Func.toStr(user.getAvatar(), TokenConstant.DEFAULT_AVATAR)) .set(TokenConstant.ACCESS_TOKEN, accessToken.getToken()) .set(TokenConstant.TOKEN_TYPE, TokenConstant.BEARER) .set(TokenConstant.EXPIRES_IN, accessToken.getExpire()) .set(OPEN_ID, user.getOpenId()) .set(PLATE_LICENSE, user.getLicense()) .set(PLATE_MOBILE, user.getMobile()) .set(PLATE_UNION_ID, user.getUnionid()) .set(TokenConstant.REFRESH_TOKEN, createRefreshToken(user).getToken()) .set(TokenConstant.LICENSE, TokenConstant.LICENSE_NAME); } catch (Exception ex) { return authInfo.set("error_code", HttpServletResponse.SC_UNAUTHORIZED).set("error_description", ex.getMessage()); } } /** * 创建refreshToken * * @param user 用户信息 * @return refreshToken */ private static TokenInfo createRefreshToken(PlateMember user) { Map param = new HashMap<>(16); param.put(TokenConstant.TOKEN_TYPE, TokenConstant.REFRESH_TOKEN); param.put(TokenConstant.USER_ID, Func.toStr(user.getId())); return SecureUtil.createJWT(param, "audience", "issuser", TokenConstant.REFRESH_TOKEN); } }
import io.jsonwebtoken.Claims; import org.springblade.common.exception.PlateException; import org.springblade.core.jwt.JwtUtil; import org.springblade.core.jwt.props.JwtProperties; import org.springblade.core.launch.constant.TokenConstant; import org.springblade.core.secure.utils.AuthUtil; import org.springblade.core.tool.support.Kv; import org.springblade.core.tool.utils.*; import org.springblade.modules.mobile.applet.dto.MemberInfo; import javax.servlet.http.HttpServletRequest; import java.util.Map; import static org.springblade.common.enums.PlateResultCode.WECHAT_AUTH_ERROR; /** * 获取当前用户信息类 * * @author : yun.can * @since : 2021-11-11 09:49 **/ public class WechatAuthUtil { private static final String PLATE_MEMBER_REQUEST_ATTR = "PLATE_MEMBER_REQUEST_ATTR"; private static final String HEADER = TokenConstant.HEADER; private static JwtProperties jwtProperties; /** * 获取配置类 * * @return jwtProperties */ private static JwtProperties getJwtProperties() { if (jwtProperties == null) { jwtProperties = SpringUtil.getBean(JwtProperties.class); } return jwtProperties; } /** * 获取用户信息 * * @return MemberInfo */ public static MemberInfo getUser() { HttpServletRequest request = WebUtil.getRequest(); if (request == null) { throw new PlateException(WECHAT_AUTH_ERROR); } // 优先从 request 中获取 Object plateMember = request.getAttribute(PLATE_MEMBER_REQUEST_ATTR); if (plateMember == null) { plateMember = getUser(request); if (plateMember != null) { // 设置到 request 中 request.setAttribute(PLATE_MEMBER_REQUEST_ATTR, plateMember); } } return (MemberInfo) plateMember; } /** * 获取用户信息 * * @param request request * @return BladeUser */ @SuppressWarnings("unchecked") public static MemberInfo getUser(HttpServletRequest request) { Claims claims = getClaims(request); if (claims == null) { throw new PlateException(WECHAT_AUTH_ERROR); } Long userId = Func.toLong(claims.get(TokenConstant.USER_ID)); String tenantId = Func.toStr(claims.get(TokenConstant.TENANT_ID)); String nickName = Func.toStr(claims.get(TokenConstant.NICK_NAME)); String avatar = Func.toStr(claims.get(TokenConstant.AVATAR)); String openId = Func.toStr(claims.get(WechatTokenUtil.OPEN_ID)); String license = Func.toStr(claims.get(WechatTokenUtil.PLATE_LICENSE)); String mobile = Func.toStr(claims.get(WechatTokenUtil.PLATE_MOBILE)); String unionId = Func.toStr(claims.get(WechatTokenUtil.PLATE_UNION_ID)); Kv detail = Kv.create().setAll((Map extends String, ?>) claims.get(TokenConstant.DETAIL)); MemberInfo plateMember = new MemberInfo(); plateMember.setOpenId(openId); plateMember.setAvatar(avatar); plateMember.setLicense(license); plateMember.setMobile(mobile); plateMember.setUnionid(unionId); plateMember.setMemberId(userId); plateMember.setTenantId(tenantId); plateMember.setNickName(nickName); plateMember.setDetail(detail); return plateMember; } /** * 获取Claims * * @param request request * @return Claims */ public static Claims getClaims(HttpServletRequest request) { String auth = request.getHeader(HEADER); Claims claims = null; String token; // 获取 Token 参数 if (StringUtil.isNotBlank(auth)) { token = JwtUtil.getToken(auth); } else { String parameter = request.getParameter(HEADER); token = JwtUtil.getToken(parameter); } // 获取 Token 值 if (StringUtil.isNotBlank(token)) { claims = AuthUtil.parseJWT(token); } // 判断 Token 状态 if (ObjectUtil.isNotEmpty(claims) && getJwtProperties().getState()) { String tenantId = Func.toStr(claims.get(TokenConstant.TENANT_ID)); String userId = Func.toStr(claims.get(TokenConstant.USER_ID)); String accessToken = JwtUtil.getAccessToken(tenantId, userId, token); if (!token.equalsIgnoreCase(accessToken)) { return null; } } return claims; } }
谢谢大佬,那还想问下这样重写了工具类的话对同步更新有没有影响
扫一扫访问 Blade技术社区 移动端