【单账号多地登录】初版

This commit is contained in:
huangweixiong 2022-07-13 10:47:23 +08:00
parent 9426860495
commit af6cc27fcd
10 changed files with 114 additions and 35 deletions

View File

@ -0,0 +1,23 @@
DROP PROCEDURE IF EXISTS del_idx;
create procedure del_idx(IN p_tablename varchar(200), IN p_idxname VARCHAR(200))
begin
DECLARE str VARCHAR(250);
set @str=concat(' drop index ',p_idxname,' on ',p_tablename);
select count(*) into @cnt from information_schema.statistics where table_name=p_tablename and index_name=p_idxname ;
if @cnt >0 then
PREPARE stmt FROM @str;
EXECUTE stmt ;
end if;
end ;
call del_idx('sys_user_token','user_id');

View File

@ -13,4 +13,5 @@ public class YaweiSSOProperties {
private String domain;
private String ssoKey;
private String keeperUrl;
private String logoutUrl;
}

View File

@ -13,6 +13,7 @@ import io.renren.modules.log.enums.LoginStatusEnum;
import io.renren.modules.log.service.SysLogLoginService;
import io.renren.modules.security.dto.LoginDTO;
import io.renren.modules.security.oauth2.Oauth2Filter;
import io.renren.modules.security.oauth2.SSOValidator;
import io.renren.modules.security.oauth2.YaWeiCookieManage;
import io.renren.modules.security.password.PasswordUtils;
import io.renren.modules.security.service.CaptchaService;
@ -26,6 +27,7 @@ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@ -59,6 +61,9 @@ public class LoginController {
@Autowired
private SysLogLoginService sysLogLoginService;
@Autowired(required = false)
private SSOValidator ssoValidator;
@Value("${yawei.enable}")
private Boolean yaweiEnable; // 亚微登录
@ -158,7 +163,14 @@ public class LoginController {
UserDetail user = SecurityUser.getUser();
//退出
sysUserTokenService.logout(user.getId());
// sysUserTokenService.logout(user.getId());
String token = request.getHeader(Constant.TOKEN_HEADER);
if (StringUtils.isBlank(token)) {
return new Result<>().error("请传入token");
}
sysUserTokenService.logoutByToken(token);
//用户信息
SysLogLoginEntity log = new SysLogLoginEntity();
@ -172,6 +184,10 @@ public class LoginController {
log.setCreateDate(new Date());
sysLogLoginService.save(log);
if (ssoValidator != null && ssoValidator.getLogoutUrl() != null) {
response.addHeader("REDIRECT", ssoValidator.getLogoutUrl());
}
return new Result();
}

View File

@ -24,8 +24,8 @@ public class CasSSOValidator implements SSOValidator {
private String serverUrlPrefix;
@Value("${cas.server-login-url}")
private String serverLoginUrl;
@Value("${cas.client-host-url}")
private String clientHostUrl;
@Value("${cas.server-logout-url}")
private String serverLogoutUrl;
private Cas30JsonServiceTicketValidator ticketValidator;
@ -88,6 +88,11 @@ public class CasSSOValidator implements SSOValidator {
return serverLoginUrl + "?service=" + URLEncoder.encode(removeCreditParame(callBackUrl));
}
@Override
public String getLogoutUrl() {
return serverLogoutUrl;
}
private static String getParema(String urlStr, String field) {
String result = "";
Pattern pXM = Pattern.compile(field + "=([^&|^#]*)");

View File

@ -11,4 +11,6 @@ public interface SSOValidator {
String removeCreditParame(String url);
String getLoginUrl(String callBackUrl);
String getLogoutUrl();
}

View File

@ -77,6 +77,11 @@ public class YaweiSSOValidator implements SSOValidator {
return keeperUrl;
}
@Override
public String getLogoutUrl() {
return yaweiSSOProperties.getLogoutUrl();
}
private String getSSOToken(String url){
String ssoMatchKey = yaweiSSOProperties.getSsoKey() + "=";
int startIndex = url.indexOf(ssoMatchKey) + ssoMatchKey.length();

View File

@ -26,6 +26,12 @@ public interface SysUserTokenService extends BaseService<SysUserTokenEntity> {
*/
void logout(Long userId);
/**
* 退出
* @param token 用户token
*/
void logoutByToken(String token);
/**
* 在线用户分页
*/

View File

@ -1,5 +1,6 @@
package io.renren.modules.security.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.renren.common.constant.Constant;
import io.renren.common.page.PageData;
@ -21,9 +22,9 @@ import java.util.Map;
@Service
public class SysUserTokenServiceImpl extends BaseServiceImpl<SysUserTokenDao, SysUserTokenEntity> implements SysUserTokenService {
/**
* 12小时后过期 修改成 7天
* 12小时后过期
*/
private final static int EXPIRE = 3600 * 24 * 7;
private final static int EXPIRE = 3600 * 12;
@Override
public Result createToken(Long userId) {
@ -34,37 +35,47 @@ public class SysUserTokenServiceImpl extends BaseServiceImpl<SysUserTokenDao, Sy
Date now = new Date();
//过期时间
Date expireTime = new Date(now.getTime() + EXPIRE * 1000);
token = TokenGenerator.generateValue();
SysUserTokenEntity tokenEntity = new SysUserTokenEntity();
tokenEntity = new SysUserTokenEntity();
tokenEntity.setUserId(userId);
tokenEntity.setToken(token);
tokenEntity.setUpdateDate(now);
tokenEntity.setExpireDate(expireTime);
//保存token
this.insert(tokenEntity);
//支持单账号多地登录
//判断是否生成过token
SysUserTokenEntity tokenEntity = baseDao.getByUserId(userId);
if(tokenEntity == null){
//生成一个token
token = TokenGenerator.generateValue();
tokenEntity = new SysUserTokenEntity();
tokenEntity.setUserId(userId);
tokenEntity.setToken(token);
tokenEntity.setUpdateDate(now);
tokenEntity.setExpireDate(expireTime);
//保存token
this.insert(tokenEntity);
}else{
//判断token是否过期
// if(tokenEntity.getExpireDate().getTime() < System.currentTimeMillis()){
// //token过期重新生成token
// token = TokenGenerator.generateValue();
// }else {
// token = tokenEntity.getToken();
// }
token = TokenGenerator.generateValue();
tokenEntity.setToken(token);
tokenEntity.setUpdateDate(now);
tokenEntity.setExpireDate(expireTime);
//更新token
this.updateById(tokenEntity);
}
// SysUserTokenEntity tokenEntity = baseDao.getByUserId(userId);
// if(tokenEntity == null){
// //生成一个token
// token = TokenGenerator.generateValue();
//
// tokenEntity = new SysUserTokenEntity();
// tokenEntity.setUserId(userId);
// tokenEntity.setToken(token);
// tokenEntity.setUpdateDate(now);
// tokenEntity.setExpireDate(expireTime);
//
// //保存token
// this.insert(tokenEntity);
// }else{
// //判断token是否过期
//// if(tokenEntity.getExpireDate().getTime() < System.currentTimeMillis()){
//// //token过期重新生成token
//// token = TokenGenerator.generateValue();
//// }else {
//// token = tokenEntity.getToken();
//// }
// token = TokenGenerator.generateValue();
// tokenEntity.setToken(token);
// tokenEntity.setUpdateDate(now);
// tokenEntity.setExpireDate(expireTime);
//
// //更新token
// this.updateById(tokenEntity);
// }
Map<String, Object> map = new HashMap<>(2);
map.put(Constant.TOKEN_HEADER, token);
@ -78,6 +89,14 @@ public class SysUserTokenServiceImpl extends BaseServiceImpl<SysUserTokenDao, Sy
baseDao.logout(userId, expireDate);
}
@Override
public void logoutByToken(String token) {
LambdaQueryWrapper<SysUserTokenEntity> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(SysUserTokenEntity::getToken, token);
baseDao.delete(wrapper);
}
@Override
public PageData<SysOnlineEntity> onlinePage(Map<String, Object> params) {
//转换成like

View File

@ -119,6 +119,7 @@ sso:
cas:
server-url-prefix: http://10.134.135.81:11188/cas
server-login-url: http://10.134.135.81:11188/cas/login
server-logout-url: http://10.134.135.81:11188/cas/logout
client-host-url: http://localhost:9999/#/
use-session: false
validation-type: cas3

View File

@ -1,3 +1,4 @@
sso.domain=jhoa.qd.gov.cn
sso.ssoKey=SSOToken
sso.keeperUrl=http://jhoa.qd.gov.cn/Keeper.aspx
sso.logoutUrl=http://jhoa.qd.gov.cn/LogoutSSO.aspx