【单账号多地登录】初版

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 domain;
private String ssoKey; private String ssoKey;
private String keeperUrl; 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.log.service.SysLogLoginService;
import io.renren.modules.security.dto.LoginDTO; import io.renren.modules.security.dto.LoginDTO;
import io.renren.modules.security.oauth2.Oauth2Filter; 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.oauth2.YaWeiCookieManage;
import io.renren.modules.security.password.PasswordUtils; import io.renren.modules.security.password.PasswordUtils;
import io.renren.modules.security.service.CaptchaService; 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.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -59,6 +61,9 @@ public class LoginController {
@Autowired @Autowired
private SysLogLoginService sysLogLoginService; private SysLogLoginService sysLogLoginService;
@Autowired(required = false)
private SSOValidator ssoValidator;
@Value("${yawei.enable}") @Value("${yawei.enable}")
private Boolean yaweiEnable; // 亚微登录 private Boolean yaweiEnable; // 亚微登录
@ -158,7 +163,14 @@ public class LoginController {
UserDetail user = SecurityUser.getUser(); 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(); SysLogLoginEntity log = new SysLogLoginEntity();
@ -172,6 +184,10 @@ public class LoginController {
log.setCreateDate(new Date()); log.setCreateDate(new Date());
sysLogLoginService.save(log); sysLogLoginService.save(log);
if (ssoValidator != null && ssoValidator.getLogoutUrl() != null) {
response.addHeader("REDIRECT", ssoValidator.getLogoutUrl());
}
return new Result(); return new Result();
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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