package com.changhong.epc.zuul.config.login; import com.changhong.epc.constter.system.prop.ChangHongProperties; import com.changhong.passport.client.web.filters.Ki4soClientFilter; import com.changhong.passport.common.utils.StringUtils; import com.changhong.passport.core.authentication.EncryCredential; import com.changhong.passport.core.model.EncryCredentialInfo; import com.iemsoft.framework.cloud.core.tools.SpringUtil; import lombok.extern.slf4j.Slf4j; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; import java.lang.management.ManagementFactory; import java.util.Objects; @Slf4j public class LoginFilterRegistrationBean extends Ki4soClientFilter{ @Override public void doInit(FilterConfig filterConfig) throws ServletException { super.doInit(filterConfig); String env = ManagementFactory.getRuntimeMXBean().getInputArguments() .stream() .filter(val->val.indexOf("-Denv")>=0) .map(val->val.substring(val.indexOf('=')+1, val.length())) .findFirst() .orElse(""); if(Objects.equals(env, "DEV")) { this.keyService = new KeyServiceImpl(this.ki4soServerFetchKeyUrl, this.ki4soClientAppId); this.encryCredentialManager.setKeyService(this.keyService); } } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletResponse servletResponse = (HttpServletResponse)response; HttpServletRequest servletRequest = (HttpServletRequest)request; HttpSession session = servletRequest.getSession(); try { servletRequest.setAttribute("ki4so_server_logout_url", this.ki4soServerLogoutUrl); log.info(new StringBuilder().append("===========,login sesseion:").append(session).toString()); log.info(new StringBuilder().append("============,user:").append(this.ki4soSession.contains(session.getId())).toString()); if (!this.ki4soSession.contains(session.getId())) { log.info("============,USER_STATE_IN_SESSION_KEY为空。"); String ki4so_client_ec = getClientEC(servletRequest); // System.out.println(ki4so_client_ec); log.info(new StringBuilder().append("ki4so_client_ec:").append(ki4so_client_ec).toString()); if (StringUtils.isEmpty(ki4so_client_ec)){ servletResponse.sendRedirect(SpringUtil.getBean(ChangHongProperties.class).getLoginUrl()); chain.doFilter(request, response); return; } log.info(new StringBuilder().append("===========,ki4soKey:").append(this.ki4soKey).toString()); if (this.ki4soKey == null) { try { this.ki4soKey = this.keyService.findKeyByAppId(this.ki4soClientAppId); log.info(new StringBuilder().append("===========,after_ki4soKey:").append(this.ki4soKey).toString()); } catch (Exception e) { // System.err.println(e.getMessage()); } } EncryCredentialInfo encryCredentialInfo = this.encryCredentialManager.decrypt(new EncryCredential(ki4so_client_ec)); log.info(new StringBuilder().append("解密用户信息是否为null:").append(encryCredentialInfo != null).toString()); if (encryCredentialInfo != null) { log.info(new StringBuilder().append("encryCredentialInfo valid:").append(encryCredentialInfo.toString()).toString()); boolean valid = this.encryCredentialManager.checkEncryCredentialInfo(encryCredentialInfo); log.info(new StringBuilder().append("valid:").append(valid).toString()); if (valid) { encryCredentialInfo.setSessionId(session.getId()); this.ki4soSession.set(session.getId(), encryCredentialInfo); this.ki4soSession.set(encryCredentialInfo.getUserId(), encryCredentialInfo); if (this.appClientLoginHandler != null) { this.appClientLoginHandler.loginClient(encryCredentialInfo, servletRequest, servletResponse); } // String url = servletRequest.getRequestURL().toString(); // String qs = servletRequest.getQueryString(); // if (qs != null) { // qs = remove(qs, "KI4SO_CLIENT_EC").replace("&&", "&"); // if (qs.startsWith("&")) { // qs = qs.substring(1, qs.length()); // } // if (qs.endsWith("&")) { // qs = qs.substring(0, qs.length() - 1); // } // if (qs.length() > 0) { // url = new StringBuilder().append(url).append("?").append(qs).toString(); // } // } // URI uri = new URI(SpringUtil.getBean(ChangHongProperties.class).getLoginSendRedirect()); // url = String.format("http://%s:%s%s" // , uri.getHost() // , uri.getPort() // , ObjectUtil.empty(new URI(url).getQuery()) ? new URI(url).getPath() : new URI(url).getPath() + "?" +new URI(url).getQuery()); String url = SpringUtil.getBean(ChangHongProperties.class).getLoginSendRedirect(); log.info(new StringBuilder().append("转发url:").append(url).toString()); servletResponse.sendRedirect(url); return; } } servletResponse.sendRedirect(buildRedirectToKi4soServer(servletRequest)); return; } log.info("========doFilter======="); chain.doFilter(request, response); return; } catch (Exception e) { log.info(e.getMessage()); servletResponse.sendRedirect(buildRedirectToKi4soServer(servletRequest)); } } }