Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#5050 租户id和登录token进行绑定,并优先使用,若没有则使用前端传递的 #5057

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public interface CommonConstant {
String PREFIX_USER_TOKEN = "prefix_user_token:";
// /** Token缓存时间:3600秒即一小时 */
// int TOKEN_EXPIRE_TIME = 3600;
String PREFIX_USER_TENANT = "prefix_user_tenat:";

/** 登录二维码 */
String LOGIN_QRCODE_PRE = "QRCODELOGIN:";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ private static boolean jwtTokenRefresh(String token, String userName, String pas
// 设置Toekn缓存有效时间
redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, newAuthorization);
redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME * 2 / 1000);
// 刷新登录的token和租户id的绑定过期时间
redisUtil.expire(CommonConstant.PREFIX_USER_TENANT+token,JwtUtil.EXPIRE_TIME * 2 / 1000);
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.crazycake.shiro.RedisClusterManager;
import org.crazycake.shiro.RedisManager;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.config.JeecgBaseConfig;
import org.jeecg.config.shiro.filters.CustomShiroFilterFactoryBean;
Expand Down Expand Up @@ -47,6 +48,8 @@ public class ShiroConfig {
private LettuceConnectionFactory lettuceConnectionFactory;
@Autowired
private Environment env;
@Autowired
private RedisUtil redisUtil;
@Resource
private JeecgBaseConfig jeecgBaseConfig;

Expand Down Expand Up @@ -154,7 +157,7 @@ public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) {
Map<String, Filter> filterMap = new HashMap<String, Filter>(1);
//如果cloudServer为空 则说明是单体 需要加载跨域配置【微服务跨域切换】
Object cloudServer = env.getProperty(CommonConstant.CLOUD_SERVER_KEY);
filterMap.put("jwt", new JwtFilter(cloudServer==null));
filterMap.put("jwt", new JwtFilter(cloudServer==null,redisUtil));
shiroFilterFactoryBean.setFilters(filterMap);
// <!-- 过滤链定义,从上向下顺序执行,一般将/**放在最为下边
filterChainDefinitionMap.put("/**", "jwt");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ public boolean jwtTokenRefresh(String token, String userName, String passWord) {
// 设置超时时间
redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, newAuthorization);
redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME *2 / 1000);
// 刷新登录的token和租户id的绑定过期时间
redisUtil.expire(CommonConstant.PREFIX_USER_TENANT+token,JwtUtil.EXPIRE_TIME * 2 / 1000);
log.debug("——————————用户在线操作,更新token保证不掉线—————————jwtTokenRefresh——————— "+ token);
}
//update-begin--Author:scott Date:20191005 for:解决每次请求,都重写redis中 token缓存问题
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.jeecg.common.config.TenantContext;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.config.shiro.JwtToken;
import org.springframework.http.HttpHeaders;
Expand All @@ -31,11 +32,18 @@ public class JwtFilter extends BasicHttpAuthenticationFilter {
*/
private boolean allowOrigin = true;

private RedisUtil redisUtil;

public JwtFilter(){}
public JwtFilter(boolean allowOrigin){
this.allowOrigin = allowOrigin;
}

public JwtFilter(boolean allowOrigin,RedisUtil redisUtil){
this.allowOrigin = allowOrigin;
this.redisUtil = redisUtil;
}

/**
* 执行登录认证
*
Expand Down Expand Up @@ -72,6 +80,11 @@ protected boolean executeLogin(ServletRequest request, ServletResponse response)
JwtToken jwtToken = new JwtToken(token);
// 提交给realm进行登入,如果错误他会抛出异常并被捕获
getSubject(request, response).login(jwtToken);
// 本地线程写入登录租户id
Object tenantId = redisUtil.get(CommonConstant.PREFIX_USER_TENANT + token);
if (tenantId!=null) {
TenantContext.setTenant(String.valueOf(tenantId));
}
// 如果没有抛出异常则代表登入成功,返回true
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ private String getToken(SysUser user) {
redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token);
// 设置超时时间 1个小时
redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME * 1 / 1000);
// 刷新登录的token和租户id的绑定过期时间
redisUtil.expire(CommonConstant.PREFIX_USER_TENANT+token,JwtUtil.EXPIRE_TIME * 1 / 1000);
return token;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.config.TenantContext;
import org.jeecg.common.constant.CacheConstant;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.constant.SymbolConstant;
Expand Down Expand Up @@ -461,6 +462,9 @@ private Result<JSONObject> userInfo(SysUser sysUser, Result<JSONObject> result)
return loginTenantError;
}

// 设置登录的token和租户id进行绑定
redisUtil.set(CommonConstant.PREFIX_USER_TENANT+token, TenantContext.getTenant(),JwtUtil.EXPIRE_TIME * 2 / 1000);

//3.设置登录用户信息
obj.put("userInfo", sysUser);

Expand Down Expand Up @@ -616,6 +620,9 @@ public Result<JSONObject> mLogin(@RequestBody SysLoginModel sysLoginModel) throw
redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token);
redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME*2 / 1000);

// 登录的token和租户id的绑定
redisUtil.set(CommonConstant.PREFIX_USER_TENANT+token,TenantContext.getTenant(),JwtUtil.EXPIRE_TIME * 2 / 1000);

//token 信息
obj.put("token", token);
result.setResult(obj);
Expand Down