国小君统一认证

dev
youhong.ai 2023-07-14 20:28:46 +08:00
parent fac5e483e2
commit 31c1ca0202
1 changed files with 66 additions and 2 deletions

View File

@ -1,10 +1,19 @@
package com.customization.youhong.guoxiaojun.sso.impl; package com.customization.youhong.guoxiaojun.sso.impl;
import aiyh.utils.Util; import aiyh.utils.Util;
import aiyh.utils.tool.cn.hutool.core.util.StrUtil;
import com.weaverboot.frame.ioc.anno.classAnno.WeaSsoIocComponent; import com.weaverboot.frame.ioc.anno.classAnno.WeaSsoIocComponent;
import com.weaverboot.frame.ioc.anno.methodAnno.WeaSsoIoc; import com.weaverboot.frame.ioc.anno.methodAnno.WeaSsoIoc;
import com.weaverboot.frame.ioc.handler.replace.weaReplaceParam.impl.WeaSsoParam; import com.weaverboot.frame.ioc.handler.replace.weaReplaceParam.impl.WeaSsoParam;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import weaver.integration.util.SessionUtil;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Objects;
/** /**
* <h1>poc</h1> * <h1>poc</h1>
@ -17,6 +26,9 @@ import org.apache.log4j.Logger;
@WeaSsoIocComponent("SsoGuoXiaoJunPocService") @WeaSsoIocComponent("SsoGuoXiaoJunPocService")
public class SsoGuoXiaoJunPocImpl { public class SsoGuoXiaoJunPocImpl {
public static final String API_SSO_URL = "/api/aiyh/guoxiaojun/sso";
public static final String LOGIN_URL = "/wui/index.html";
public static final String API_CHECK_OUT_URI = "/api/hrm/login/checkLogout";
private final Logger log = Util.getLogger(); private final Logger log = Util.getLogger();
/** /**
@ -26,7 +38,59 @@ public class SsoGuoXiaoJunPocImpl {
*/ */
@WeaSsoIoc(order = 1, description = "单点登录逻辑1") @WeaSsoIoc(order = 1, description = "单点登录逻辑1")
public void ssoLogin(WeaSsoParam weaSsoParam) { public void ssoLogin(WeaSsoParam weaSsoParam) {
String servletPath = weaSsoParam.getRequest().getServletPath(); try {
log.info(servletPath); HttpServletRequest request = weaSsoParam.getRequest();
String requestURI = request.getRequestURI();
HttpServletResponse response = weaSsoParam.getResponse();
HttpSession session = request.getSession(true);
Object weaverUser = session.getAttribute("weaver_user@bean");
if (Objects.isNull(weaverUser)) {
session.removeAttribute("outUserId");
}
if (requestURI.equals(API_SSO_URL)) {
// 登录oa系统
loginOa(request, response);
return;
}
if (requestURI.equals(API_CHECK_OUT_URI)) {
// 退出登录
session.removeAttribute("outUserId");
String logOutUrl = Util.getCusConfigValue("oauth2-proxy-logout-url");
response.setHeader("logOutUrl", logOutUrl);
return;
}
// sso 认证
if (session.getAttribute("outUserId") == null) {
authorSso(response, session);
}
} catch (IOException e) {
log.error("重定地址失败!" + Util.getErrString(e));
}
}
//
private void loginOa(HttpServletRequest request, HttpServletResponse response) throws IOException {
HttpSession session;
String outUserId = request.getHeader("X-Credential-Identifier");
// 登录处理
// String userId = Util.getCusConfigValueNullOrEmpty(outUserId,"1");
String userId = Util.getCusConfigValue(outUserId);
if (StrUtil.isBlank(userId)) {
return;
}
SessionUtil.createSession(userId, request, response);
session = request.getSession();
session.setAttribute("outUserId", Objects.isNull(outUserId) ? userId : outUserId);
response.sendRedirect(Util.getCusConfigValueNullOrEmpty("SSO_SUCCESS_REDIRECT_URL", "/wui/index.html"));
}
private void authorSso(HttpServletResponse response, HttpSession session) throws IOException {
Object outUserId = session.getAttribute("outUserId");
if (Objects.nonNull(outUserId)) {
return;
}
// 地址重定向到国小君aut2认证地址
response.sendRedirect(Util.getCusConfigValue("oauth2-proxy-url") + "?url="
+ URLEncoder.encode(Util.getCusConfigValue("OA_URL") + API_SSO_URL, "UTF-8"));
} }
} }