【单点登录】支持配置文件是否启用单点逻辑

This commit is contained in:
huangweixiong 2022-06-03 14:27:12 +08:00
parent 3555b6ca05
commit a64bdb60ce
2 changed files with 44 additions and 48 deletions

View File

@ -20,6 +20,7 @@ import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.web.filter.authc.AuthenticatingFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@ -30,6 +31,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Field;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Map;
@ -49,6 +51,9 @@ public class Oauth2Filter extends AuthenticatingFilter {
@Autowired
private SysUserDao sysUserDao;
@Value("${yawei.enable}")
private Boolean yaweiEnable;
@Override
protected AuthenticationToken createToken(ServletRequest request, ServletResponse response) throws Exception {
//获取请求token
@ -88,51 +93,14 @@ public class Oauth2Filter extends AuthenticatingFilter {
@Override
protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
boolean yaweiHandle = yaweiHandle((HttpServletRequest) request, (HttpServletResponse) response);
if (yaweiHandle) {
//处理成功肯定从cookie或写cookie拿到了认证信息
//亚威认证通过为自己添加上token
TicketManager tm = new TicketManager();
String currentUser = null;
if (tm.LoadTicket((HttpServletRequest) request)) {
currentUser = tm.getUserID();
} else {
currentUser = getYaweiUserIdCookieInResponse((HttpServletResponse) response);
currentUser = tm.DecData(currentUser);
}
if (yaweiEnable) {
return yaweiHandle((HttpServletRequest) request, (HttpServletResponse) response);
}else {
String requesturi = ((HttpServletRequest) request).getHeader("REQUESTURI");
if (currentUser != null) {
LambdaQueryWrapper<SysUserEntity> queryWrapper = new QueryWrapper<SysUserEntity>().lambda()
.eq(SysUserEntity::getUsername, currentUser);
SysUserEntity sysUserEntity = sysUserDao.selectOne(queryWrapper);
if (sysUserEntity != null) {
String currentToken = getRequestToken((HttpServletRequest) request);
HttpServletResponse httpresponse = (HttpServletResponse) response;
if (StringUtils.isBlank(currentToken)) {
Result<Map> result = sysUserTokenService.createToken(sysUserEntity.getId());
Object token = result.getData().get(Constant.TOKEN_HEADER);
currentToken = (String) token;
Cookie cookie = new Cookie(Constant.TOKEN_HEADER, currentToken);
cookie.setPath("/");
httpresponse.addCookie(cookie);
httpresponse.addHeader(Constant.TOKEN_HEADER, currentToken);
httpresponse.addHeader("REDIRECT", ((HttpServletRequest) request).getHeader("REQUESTURI"));
}
request.setAttribute(Constant.TOKEN_HEADER, currentToken);
// return executeLogin(request, response);
boolean success = executeLogin(request, response);
return success;
} else {
send401Error((HttpServletResponse) response, "未找到用户");
return false;
}
}
((HttpServletResponse)response).addHeader("REDIRECT", "/#/login");
}
send401Error((HttpServletResponse) response, "登录失败");
return false;
}
@ -237,13 +205,38 @@ public class Oauth2Filter extends AuthenticatingFilter {
redirectToYaweiLogin(request, response);
return false;
} else {
// String domainName = yaweiSSOProperties.getDomain();
// tm.SaveTicket(response, domainName);
YaWeiCookieManage.saveTicket(response, tm);
return true;
//认证通过
LambdaQueryWrapper<SysUserEntity> queryWrapper = new QueryWrapper<SysUserEntity>().lambda()
.eq(SysUserEntity::getUsername, tm.getUserID());
SysUserEntity sysUserEntity = sysUserDao.selectOne(queryWrapper);
if (sysUserEntity != null) {
String currentToken = getRequestToken((HttpServletRequest) request);
HttpServletResponse httpresponse = (HttpServletResponse) response;
if (StringUtils.isBlank(currentToken)) {
Result<Map> result = sysUserTokenService.createToken(sysUserEntity.getId());
Object token = result.getData().get(Constant.TOKEN_HEADER);
currentToken = (String) token;
Cookie cookie = new Cookie(Constant.TOKEN_HEADER, currentToken);
cookie.setPath("/");
httpresponse.addCookie(cookie);
httpresponse.addHeader(Constant.TOKEN_HEADER, currentToken);
String yaweiToken = yaweiSSOProperties.getSsoKey() + "=" + strResponse;
if (requestUri.indexOf(yaweiToken) > 0) {
requestUri = requestUri.replace(yaweiToken, "");
if (requestUri.endsWith("?")) {
requestUri = requestUri.substring(0,requestUri.length() -1);
}
}
httpresponse.addHeader("REDIRECT", requestUri);
}
request.setAttribute(Constant.TOKEN_HEADER, currentToken);
return executeLogin(request, response);
}
}
}
return true;
return false;
}
private void redirectToYaweiLogin(HttpServletRequest request, HttpServletResponse response) throws IOException, IllegalAccessException, NoSuchFieldException {

View File

@ -83,4 +83,7 @@ mybatis-plus:
#系统上线日期,用于统计能力浏览记录
system:
startDay: 2022-04-01
startDay: 2022-04-01
yawei:
enable: true