|
@@ -1,13 +1,10 @@
|
|
|
package com.ruoyi.framework.web.service;
|
|
|
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.concurrent.TimeUnit;
|
|
|
-import javax.servlet.http.HttpServletRequest;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.beans.factory.annotation.Value;
|
|
|
-import org.springframework.stereotype.Component;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import cn.hutool.http.HttpUtil;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import com.ruoyi.common.constant.Constants;
|
|
|
+import com.ruoyi.common.core.domain.entity.SysUser;
|
|
|
import com.ruoyi.common.core.domain.model.LoginUser;
|
|
|
import com.ruoyi.common.core.redis.RedisCache;
|
|
|
import com.ruoyi.common.utils.ServletUtils;
|
|
@@ -15,10 +12,19 @@ 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 com.ruoyi.system.service.ISysUserService;
|
|
|
import eu.bitwalker.useragentutils.UserAgent;
|
|
|
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.stereotype.Component;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
/**
|
|
|
* token验证处理
|
|
@@ -26,8 +32,7 @@ import io.jsonwebtoken.SignatureAlgorithm;
|
|
|
* @author ruoyi
|
|
|
*/
|
|
|
@Component
|
|
|
-public class TokenService
|
|
|
-{
|
|
|
+public class TokenService {
|
|
|
// 令牌自定义标识
|
|
|
@Value("${token.header}")
|
|
|
private String header;
|
|
@@ -40,6 +45,9 @@ public class TokenService
|
|
|
@Value("${token.expireTime}")
|
|
|
private int expireTime;
|
|
|
|
|
|
+ @Value("${third.userTokenInfo}")
|
|
|
+ private String dajiaoguan_token;
|
|
|
+
|
|
|
protected static final long MILLIS_SECOND = 1000;
|
|
|
|
|
|
protected static final long MILLIS_MINUTE = 60 * MILLIS_SECOND;
|
|
@@ -49,17 +57,44 @@ public class TokenService
|
|
|
@Autowired
|
|
|
private RedisCache redisCache;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private ISysUserService userService;
|
|
|
+
|
|
|
/**
|
|
|
* 获取用户身份信息
|
|
|
*
|
|
|
* @return 用户信息
|
|
|
*/
|
|
|
- public LoginUser getLoginUser(HttpServletRequest request)
|
|
|
- {
|
|
|
+ public LoginUser getLoginUser(HttpServletRequest request) {
|
|
|
// 获取请求携带的令牌
|
|
|
String token = getToken(request);
|
|
|
- if (StringUtils.isNotEmpty(token))
|
|
|
- {
|
|
|
+ // 处理外部渠道过来的token数据
|
|
|
+ if (StrUtil.isNotBlank(request.getHeader("FROM_OUT")) && StrUtil.isNotBlank(token)) {
|
|
|
+ // 通过token换取用户信息
|
|
|
+ String getResult = HttpUtil.createGet(dajiaoguan_token).header("Authorization", token).execute().body();
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(getResult);
|
|
|
+ int code = jsonObject.getInteger("code");
|
|
|
+ if (code == 1) {
|
|
|
+ // 查询系统中是否存在该用户
|
|
|
+ SysUser sysUser = userService.selectUserByUserName("admin");
|
|
|
+ if (sysUser == null) {
|
|
|
+ // 将此用户新增入系统
|
|
|
+ sysUser = new SysUser();
|
|
|
+ JSONObject data = jsonObject.getJSONObject("data");
|
|
|
+ sysUser.setUserId(data.getLong("userId"));
|
|
|
+ sysUser.setUserName(data.getString("phone"));
|
|
|
+ sysUser.setPhonenumber(data.getString("phone"));
|
|
|
+ sysUser.setDelFlag("0");
|
|
|
+ sysUser.setStatus("0");
|
|
|
+ sysUser.setNickName(data.getString("name"));
|
|
|
+ // 新加入的用户属于普通用户角色
|
|
|
+ sysUser.setRoleIds(new Long[]{2L});
|
|
|
+ userService.insertUser(sysUser);
|
|
|
+ } else {
|
|
|
+// redisCache.
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else if (StringUtils.isNotEmpty(token)) {
|
|
|
Claims claims = parseToken(token);
|
|
|
// 解析对应的权限以及用户信息
|
|
|
String uuid = (String) claims.get(Constants.LOGIN_USER_KEY);
|
|
@@ -73,10 +108,8 @@ public class TokenService
|
|
|
/**
|
|
|
* 设置用户身份信息
|
|
|
*/
|
|
|
- public void setLoginUser(LoginUser loginUser)
|
|
|
- {
|
|
|
- if (StringUtils.isNotNull(loginUser) && StringUtils.isNotEmpty(loginUser.getToken()))
|
|
|
- {
|
|
|
+ public void setLoginUser(LoginUser loginUser) {
|
|
|
+ if (StringUtils.isNotNull(loginUser) && StringUtils.isNotEmpty(loginUser.getToken())) {
|
|
|
refreshToken(loginUser);
|
|
|
}
|
|
|
}
|
|
@@ -84,10 +117,8 @@ public class TokenService
|
|
|
/**
|
|
|
* 删除用户身份信息
|
|
|
*/
|
|
|
- public void delLoginUser(String token)
|
|
|
- {
|
|
|
- if (StringUtils.isNotEmpty(token))
|
|
|
- {
|
|
|
+ public void delLoginUser(String token) {
|
|
|
+ if (StringUtils.isNotEmpty(token)) {
|
|
|
String userKey = getTokenKey(token);
|
|
|
redisCache.deleteObject(userKey);
|
|
|
}
|
|
@@ -99,8 +130,7 @@ public class TokenService
|
|
|
* @param loginUser 用户信息
|
|
|
* @return 令牌
|
|
|
*/
|
|
|
- public String createToken(LoginUser loginUser)
|
|
|
- {
|
|
|
+ public String createToken(LoginUser loginUser) {
|
|
|
String token = IdUtils.fastUUID();
|
|
|
loginUser.setToken(token);
|
|
|
setUserAgent(loginUser);
|
|
@@ -117,12 +147,10 @@ public class TokenService
|
|
|
* @param loginUser
|
|
|
* @return 令牌
|
|
|
*/
|
|
|
- public void verifyToken(LoginUser loginUser)
|
|
|
- {
|
|
|
+ public void verifyToken(LoginUser loginUser) {
|
|
|
long expireTime = loginUser.getExpireTime();
|
|
|
long currentTime = System.currentTimeMillis();
|
|
|
- if (expireTime - currentTime <= MILLIS_MINUTE_TEN)
|
|
|
- {
|
|
|
+ if (expireTime - currentTime <= MILLIS_MINUTE_TEN) {
|
|
|
refreshToken(loginUser);
|
|
|
}
|
|
|
}
|
|
@@ -132,8 +160,7 @@ public class TokenService
|
|
|
*
|
|
|
* @param loginUser 登录信息
|
|
|
*/
|
|
|
- public void refreshToken(LoginUser loginUser)
|
|
|
- {
|
|
|
+ public void refreshToken(LoginUser loginUser) {
|
|
|
loginUser.setLoginTime(System.currentTimeMillis());
|
|
|
loginUser.setExpireTime(loginUser.getLoginTime() + expireTime * MILLIS_MINUTE);
|
|
|
// 根据uuid将loginUser缓存
|
|
@@ -146,8 +173,7 @@ public class TokenService
|
|
|
*
|
|
|
* @param loginUser 登录信息
|
|
|
*/
|
|
|
- public void setUserAgent(LoginUser loginUser)
|
|
|
- {
|
|
|
+ public void setUserAgent(LoginUser loginUser) {
|
|
|
UserAgent userAgent = UserAgent.parseUserAgentString(ServletUtils.getRequest().getHeader("User-Agent"));
|
|
|
String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
|
|
|
loginUser.setIpaddr(ip);
|
|
@@ -162,8 +188,7 @@ public class TokenService
|
|
|
* @param claims 数据声明
|
|
|
* @return 令牌
|
|
|
*/
|
|
|
- private String createToken(Map<String, Object> claims)
|
|
|
- {
|
|
|
+ private String createToken(Map<String, Object> claims) {
|
|
|
String token = Jwts.builder()
|
|
|
.setClaims(claims)
|
|
|
.signWith(SignatureAlgorithm.HS512, secret).compact();
|
|
@@ -176,8 +201,7 @@ public class TokenService
|
|
|
* @param token 令牌
|
|
|
* @return 数据声明
|
|
|
*/
|
|
|
- private Claims parseToken(String token)
|
|
|
- {
|
|
|
+ private Claims parseToken(String token) {
|
|
|
return Jwts.parser()
|
|
|
.setSigningKey(secret)
|
|
|
.parseClaimsJws(token)
|
|
@@ -190,8 +214,7 @@ public class TokenService
|
|
|
* @param token 令牌
|
|
|
* @return 用户名
|
|
|
*/
|
|
|
- public String getUsernameFromToken(String token)
|
|
|
- {
|
|
|
+ public String getUsernameFromToken(String token) {
|
|
|
Claims claims = parseToken(token);
|
|
|
return claims.getSubject();
|
|
|
}
|
|
@@ -202,18 +225,15 @@ public class TokenService
|
|
|
* @param request
|
|
|
* @return token
|
|
|
*/
|
|
|
- private String getToken(HttpServletRequest request)
|
|
|
- {
|
|
|
+ private String getToken(HttpServletRequest request) {
|
|
|
String token = request.getHeader(header);
|
|
|
- if (StringUtils.isNotEmpty(token) && token.startsWith(Constants.TOKEN_PREFIX))
|
|
|
- {
|
|
|
+ if (StringUtils.isNotEmpty(token) && token.startsWith(Constants.TOKEN_PREFIX)) {
|
|
|
token = token.replace(Constants.TOKEN_PREFIX, "");
|
|
|
}
|
|
|
return token;
|
|
|
}
|
|
|
|
|
|
- private String getTokenKey(String uuid)
|
|
|
- {
|
|
|
+ private String getTokenKey(String uuid) {
|
|
|
return Constants.LOGIN_TOKEN_KEY + uuid;
|
|
|
}
|
|
|
}
|