|
@@ -0,0 +1,260 @@
|
|
|
+package com.zhongzheng.framework.web.service;
|
|
|
+
|
|
|
+import cn.hutool.core.lang.Validator;
|
|
|
+import cn.hutool.core.util.IdUtil;
|
|
|
+import cn.hutool.http.useragent.UserAgent;
|
|
|
+import cn.hutool.http.useragent.UserAgentUtil;
|
|
|
+import com.zhongzheng.common.constant.Constants;
|
|
|
+import com.zhongzheng.common.core.redis.RedisCache;
|
|
|
+import com.zhongzheng.common.utils.ServletUtils;
|
|
|
+import com.zhongzheng.common.utils.ip.AddressUtils;
|
|
|
+import com.zhongzheng.common.utils.ip.IpUtils;
|
|
|
+import com.zhongzheng.modules.company.entity.ClientBsLoginUser;
|
|
|
+import com.zhongzheng.modules.user.bo.UserLoginKeyAddBo;
|
|
|
+import com.zhongzheng.modules.user.domain.User;
|
|
|
+import com.zhongzheng.modules.user.entity.ClientLoginUser;
|
|
|
+import com.zhongzheng.modules.user.service.IUserLoginKeyService;
|
|
|
+import com.zhongzheng.modules.user.service.IUserService;
|
|
|
+import io.jsonwebtoken.Claims;
|
|
|
+import io.jsonwebtoken.Jwts;
|
|
|
+import io.jsonwebtoken.SignatureAlgorithm;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.security.authentication.AuthenticationManager;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+/**
|
|
|
+ * token验证处理
|
|
|
+ *
|
|
|
+ * @author zhongzheng
|
|
|
+ */
|
|
|
+@Component
|
|
|
+public class BsTokenService
|
|
|
+{
|
|
|
+ // 令牌自定义标识
|
|
|
+ @Value("${token.header}")
|
|
|
+ private String header;
|
|
|
+
|
|
|
+ // 令牌秘钥
|
|
|
+ @Value("${token.secret}")
|
|
|
+ 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;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RedisCache redisCache;
|
|
|
+
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IUserLoginKeyService iUserLoginKeyService;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private AuthenticationManager authenticationManager;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取用户身份信息
|
|
|
+ *
|
|
|
+ * @return 用户信息
|
|
|
+ */
|
|
|
+ public ClientBsLoginUser getLoginUser(HttpServletRequest request) {
|
|
|
+ //测试用户
|
|
|
+ String test_token = request.getHeader("X-Auth-Token");
|
|
|
+ if("test".equals(test_token)){
|
|
|
+ return getTestUser();
|
|
|
+ }
|
|
|
+ // 获取请求携带的令牌
|
|
|
+ String token = getToken(request);
|
|
|
+ if (Validator.isNotEmpty(token))
|
|
|
+ {
|
|
|
+ Claims claims = parseToken(token);
|
|
|
+ // 解析对应的权限以及用户信息
|
|
|
+ String uuid = (String) claims.get(Constants.BS_LOGIN_USER_KEY);
|
|
|
+ if(!iUserLoginKeyService.queryByUserKey(uuid)){
|
|
|
+ ClientBsLoginUser errorUser = new ClientBsLoginUser();
|
|
|
+ return errorUser;
|
|
|
+ }
|
|
|
+ String userKey = getTokenKey(uuid);
|
|
|
+ ClientBsLoginUser user = redisCache.getCacheObject(userKey);
|
|
|
+ return user;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private ClientBsLoginUser getTestUser(){
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置用户身份信息
|
|
|
+ */
|
|
|
+ public void setLoginUser(ClientBsLoginUser loginUser)
|
|
|
+ {
|
|
|
+ if (Validator.isNotNull(loginUser) && Validator.isNotEmpty(loginUser.getToken()))
|
|
|
+ {
|
|
|
+ refreshToken(loginUser);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除用户身份信息
|
|
|
+ */
|
|
|
+ public void delLoginUser(String token)
|
|
|
+ {
|
|
|
+ if (Validator.isNotEmpty(token))
|
|
|
+ {
|
|
|
+ String userKey = getTokenKey(token);
|
|
|
+ redisCache.deleteObject(userKey);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建令牌
|
|
|
+ *
|
|
|
+ * @param loginUser 用户信息
|
|
|
+ * @return 令牌
|
|
|
+ */
|
|
|
+ public String createToken(ClientBsLoginUser loginUser)
|
|
|
+ {
|
|
|
+ String token = IdUtil.simpleUUID();
|
|
|
+ loginUser.setToken(token);
|
|
|
+ setUserAgent(loginUser);
|
|
|
+ refreshToken(loginUser);
|
|
|
+ Map<String, Object> claims = new HashMap<>();
|
|
|
+ claims.put(Constants.BS_LOGIN_USER_KEY, token);
|
|
|
+ String secret = createToken(claims);
|
|
|
+ if(Validator.isNotEmpty(token)){
|
|
|
+ UserLoginKeyAddBo addBo = new UserLoginKeyAddBo();
|
|
|
+ addBo.setUserId(loginUser.getUser().getUserId());
|
|
|
+ addBo.setUserKey(token);
|
|
|
+ addBo.setIp(loginUser.getIpaddr());
|
|
|
+ addBo.setBrowser(loginUser.getBrowser());
|
|
|
+ addBo.setSecret(secret);
|
|
|
+ iUserLoginKeyService.insertByAddBo(addBo);
|
|
|
+ }
|
|
|
+ return secret;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 验证令牌有效期,相差不足20分钟,自动刷新缓存
|
|
|
+ *
|
|
|
+ * @param loginUser
|
|
|
+ * @return 令牌
|
|
|
+ */
|
|
|
+ public void verifyToken(ClientBsLoginUser loginUser)
|
|
|
+ {
|
|
|
+ long expireTime = loginUser.getExpireTime();
|
|
|
+ long currentTime = System.currentTimeMillis();
|
|
|
+ if (expireTime - currentTime <= MILLIS_MINUTE_TEN)
|
|
|
+ {
|
|
|
+ refreshToken(loginUser);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 刷新令牌有效期
|
|
|
+ *
|
|
|
+ * @param loginUser 登录信息
|
|
|
+ */
|
|
|
+ public void refreshToken(ClientBsLoginUser loginUser)
|
|
|
+ {
|
|
|
+ loginUser.setLoginTime(System.currentTimeMillis());
|
|
|
+ loginUser.setExpireTime(loginUser.getLoginTime() + expireTime * MILLIS_MINUTE);
|
|
|
+ // 根据uuid将loginUser缓存
|
|
|
+ String userKey = getTokenKey(loginUser.getToken());
|
|
|
+ redisCache.setCacheObject(userKey, loginUser, expireTime, TimeUnit.MINUTES);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置用户代理信息
|
|
|
+ *
|
|
|
+ * @param loginUser 登录信息
|
|
|
+ */
|
|
|
+ public void setUserAgent(ClientBsLoginUser loginUser)
|
|
|
+ {
|
|
|
+ UserAgent userAgent = UserAgentUtil.parse(ServletUtils.getRequest().getHeader("User-Agent"));
|
|
|
+ String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
|
|
|
+ loginUser.setIpaddr(ip);
|
|
|
+ loginUser.setLoginLocation(AddressUtils.getRealAddressByIP(ip));
|
|
|
+ loginUser.setBrowser(userAgent.getBrowser().getName());
|
|
|
+ loginUser.setOs(userAgent.getOs().getName());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从数据声明生成令牌
|
|
|
+ *
|
|
|
+ * @param claims 数据声明
|
|
|
+ * @return 令牌
|
|
|
+ */
|
|
|
+ private String createToken(Map<String, Object> claims)
|
|
|
+ {
|
|
|
+ String token = Jwts.builder()
|
|
|
+ .setClaims(claims)
|
|
|
+ .signWith(SignatureAlgorithm.HS512, secret).compact();
|
|
|
+ return token;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从令牌中获取数据声明
|
|
|
+ *
|
|
|
+ * @param token 令牌
|
|
|
+ * @return 数据声明
|
|
|
+ */
|
|
|
+ private Claims parseToken(String token)
|
|
|
+ {
|
|
|
+ return Jwts.parser()
|
|
|
+ .setSigningKey(secret)
|
|
|
+ .parseClaimsJws(token)
|
|
|
+ .getBody();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从令牌中获取用户名
|
|
|
+ *
|
|
|
+ * @param token 令牌
|
|
|
+ * @return 用户名
|
|
|
+ */
|
|
|
+ public String getUsernameFromToken(String token)
|
|
|
+ {
|
|
|
+ Claims claims = parseToken(token);
|
|
|
+ return claims.getSubject();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取请求token
|
|
|
+ *
|
|
|
+ * @param request
|
|
|
+ * @return token
|
|
|
+ */
|
|
|
+ public String getToken(HttpServletRequest request)
|
|
|
+ {
|
|
|
+ String token = request.getHeader(header);
|
|
|
+ if (Validator.isNotEmpty(token) && token.startsWith(Constants.BS_TOKEN_PREFIX))
|
|
|
+ {
|
|
|
+ token = token.replace(Constants.BS_TOKEN_PREFIX, "");
|
|
|
+ return token;
|
|
|
+ }else{
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getTokenKey(String uuid)
|
|
|
+ {
|
|
|
+ return Constants.BS_LOGIN_TOKEN_KEY + uuid;
|
|
|
+ }
|
|
|
+}
|