后端和管理端都用的若依框架。
但是前段的小程序需要微信授权登录。这时候就需要在若依框架上重新再起一套token验证。
首先创建两个类(只要放在你能够引用得到的位置就可以):
第一个:实体
package com.ruoyi.system.toeknUnits;public class WxLoginUser{ private static final long serialVersionUID = 1L; private Long userId; private Long deptId; private String token; private Long loginTime; private Long expireTime; private String ipaddr; private String loginLocation; private String browser; private String os; private String openId; private String nickName;// getset我不粘贴了, 也可以使用lombok的@Data}
第二个service:
package com.ruoyi.system.toeknUnits;import com.ruoyi.common.constant.Constants;import com.ruoyi.common.core.redis.RedisCache;import com.ruoyi.common.utils.ServletUtils;import com.ruoyi.common.utils.StringUtils;import com.ruoyi.common.utils.ip.AddressUtils;import com.ruoyi.common.utils.ip.IpUtils;import com.ruoyi.common.utils.uuid.IdUtils;import eu.bitwalker.useragentutils.UserAgent;import io.jsonwebtoken.*;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;import javax.servlet.http.HttpServletRequest;import java.util.HashMap;import java.util.Map;import java.util.concurrent.TimeUnit;@Componentpublic class WxTokenService{ // 令牌自定义标识 @Value("${token.header}") private String header; // 令牌秘钥 private String secret = "这个自己随便定义一段英文"; // 令牌有效期(默认30分钟) @Value("${token.expireTime}") private int expireTime; protected static final long MILLIS_SECOND = 1000; protected static final long MILLIS_MINUTE = 60 * MILLIS_SECOND; private static final Long MILLIS_MINUTE_TEN = 20 * 60 * 1000L; private static final String wx_prefix = "tf"; @Autowired private RedisCache redisCache; public WxLoginUser getWxUser(HttpServletRequest request) { // 获取请求携带的令牌 String token = getToken(request); if (StringUtils.isNotEmpty(token)) { try { // 解析对应的权限以及用户信息 String userKey = token; WxLoginUser user = redisCache.getCacheObject(userKey); return user; } catch (Exception e) { e.printStackTrace(); } } return null; } public void setWxLoginUser(WxLoginUser wxUser) { if (StringUtils.isNotNull(wxUser) && StringUtils.isNotEmpty(wxUser.getToken())) { refreshToken(wxUser); } } public void delWxUser(String token) { if (StringUtils.isNotEmpty(token)) { String userKey = getTokenKey(token); redisCache.deleteObject(userKey); } } public String createToken(WxLoginUser wxUser) { String token = IdUtils.fastUUID(); wxUser.setToken(token); setUserAgent(wxUser); refreshToken(wxUser); Map<String, Object> claims = new HashMap<>(); claims.put(wx_prefix, token); return createToken(claims); } public void verifyToken(WxLoginUser wxUser) { long expireTime = wxUser.getExpireTime(); long currentTime = System.currentTimeMillis(); if (expireTime - currentTime <= MILLIS_MINUTE_TEN) { refreshToken(wxUser); } } public void refreshToken(WxLoginUser wxUser) { wxUser.setLoginTime(System.currentTimeMillis()); wxUser.setExpireTime(wxUser.getLoginTime() + expireTime * MILLIS_MINUTE); // 根据uuid将loginUser缓存 String userKey = getTokenKey(wxUser.getToken()); redisCache.setCacheObject(userKey, wxUser, expireTime, TimeUnit.MINUTES); } public void setUserAgent(WxLoginUser wxUser) { UserAgent userAgent = UserAgent.parseUserAgentString(ServletUtils.getRequest().getHeader("User-Agent")); String ip = IpUtils.getIpAddr(); wxUser.setIpaddr(ip); wxUser.setLoginLocation(AddressUtils.getRealAddressByIP(ip)); wxUser.setBrowser(userAgent.getBrowser().getName()); wxUser.setOs(userAgent.getOperatingSystem().getName()); } private String createToken(Map<String, Object> claims) { String token = Jwts.builder() .setClaims(claims) .signWith(SignatureAlgorithm.HS512, secret).compact(); return token; } private Claims parseToken(String token) { JwtParser parser = Jwts.parser(); JwtParser jwtParser = parser.setSigningKey(secret); Jws<Claims> claimsJws = jwtParser.parseClaimsJws(token); Claims body = claimsJws.getBody(); System.out.println(body); return Jwts.parser() .setSigningKey(secret) .parseClaimsJws(token) .getBody(); } public String getUsernameFromToken(String token) { Claims claims = parseToken(token); return claims.getSubject(); } public String getToken(HttpServletRequest request) { String token = request.getHeader(header); if (StringUtils.isNotEmpty(token) && token.startsWith(wx_prefix)) { token = token.replace(Constants.TOKEN_PREFIX, ""); return token; }else{ return null; } } private String getTokenKey(String uuid) { return "tf: " + uuid; }}
下一步: 找到
com.ruoyi.framework.security.filter;
这个文件
package com.ruoyi.framework.security.filter;import java.io.IOException;import javax.servlet.FilterChain;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.ruoyi.system.toeknUnits.WxLoginUser;import com.ruoyi.system.toeknUnits.WxTokenService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;import org.springframework.security.core.context.SecurityContextHolder;import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;import org.springframework.stereotype.Component;import org.springframework.web.filter.OncePerRequestFilter;import com.ruoyi.common.core.domain.model.LoginUser;import com.ruoyi.common.utils.SecurityUtils;import com.ruoyi.common.utils.StringUtils;import com.ruoyi.framework.web.service.TokenService;@Componentpublic class JwtAuthenticationTokenFilter extends OncePerRequestFilter{ @Autowired private TokenService tokenService; @Autowired private WxTokenService wxTokenService; @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException { if( wxTokenService.getToken(request) == null){ System.out.println("管理员用户"); LoginUser loginUser = tokenService.getLoginUser(request); if (StringUtils.isNotNull(loginUser) && StringUtils.isNull(SecurityUtils.getAuthentication())) { tokenService.verifyToken(loginUser); UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(loginUser, null, loginUser.getAuthorities()); authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request)); SecurityContextHolder.getContext().setAuthentication(authenticationToken); } }else{// 小程序逻辑 System.out.println("微信用户"); WxLoginUser wxUser = wxTokenService.getWxUser(request); if (StringUtils.isNotNull(wxUser) && StringUtils.isNull(SecurityUtils.getAuthentication())) { wxTokenService.verifyToken(wxUser); UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(wxUser, null, null); authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request)); SecurityContextHolder.getContext().setAuthentication(authenticationToken); } } chain.doFilter(request, response); }}
添加你获取code之类的。就是添加白名单。
来源地址:https://blog.csdn.net/weixin_45729937/article/details/130562949