Search in sources :

Example 11 with JHttpContext

use of j.http.JHttpContext in project JFramework by gugumall.

the class LoginAgent method login.

/**
 * 向SSO Server 为以fromClientId为ID的SSO Client提供用户验证
 * @param fromClientId
 * @param request
 * @return
 */
LoginResult login(String fromClientId, HttpServletRequest request) {
    // 关联的SSO Client信息
    Client parent = parent();
    String _Interface = this.Interface;
    // 如使用相对地址,自动生成绝对地址
    if (!_Interface.startsWith("http"))
        _Interface = parent.getUrlDefault() + _Interface;
    // 登录请求参数及相关详情
    String[] paraNamesAndQueryString = getParameters(request);
    String paraNames = paraNamesAndQueryString[0];
    String paraValues = paraNamesAndQueryString[1];
    String queryString = paraNamesAndQueryString[2];
    // 把所有登录请求参数全部原样传送过去
    if (!paraNames.equals("")) {
        if (_Interface.indexOf("&") > 0 || _Interface.indexOf("?") > 0)
            _Interface += "&" + queryString.substring(1);
        else
            _Interface += queryString;
    }
    // md5拼串 - 登录请求参数名(多个逗号分隔)+按参数名顺序累加的各参数值+提供验证的SSO Client与SSO Server交互的passport(密钥)
    // 收到请求时,应该先获取_parameters参数,并按照其指明的参数顺序拼接各参数值,然后按照上述一样的方式得出md5值,与_verifier参数值相同才能通过验证
    // 详见j.app.sso.SSOClient.login()
    // System.out.println("1 - "+paraNames+paraValues+parent.getPassport());
    String md5 = JUtilMD5.MD5EncodeToHex(paraNames + paraValues + parent.getPassport());
    if (_Interface.indexOf("&") > 0 || _Interface.indexOf("?") > 0)
        _Interface += "&_parameters=" + paraNames + "&_verifier=" + md5;
    else
        _Interface += "?_parameters=" + paraNames + "&_verifier=" + md5;
    _Interface += "&" + Constants.SSO_USER_IP + "=" + JHttp.getRemoteIp(request);
    String userDomain = JUtilString.getProtocal(request.getRequestURL().toString()) + "://" + JUtilString.getHost(request.getRequestURL().toString());
    _Interface += "&" + Constants.SSO_USER_DOMAIN + "=" + JUtilString.encodeURI(userDomain, SysConfig.sysEncoding);
    // log.log("login agent interface:"+_Interface,-1);
    // 发起验证请求并获得结果
    String result = null;
    try {
        JHttp http = JHttp.getInstance();
        JHttpContext context = http.get(null, null, _Interface, SysConfig.sysEncoding);
        // log.log("context.getStatus():"+context.getStatus(), -1);
        result = context.getStatus() == 200 ? context.getResponseText() : null;
        context.finalize();
        context = null;
    } catch (Exception e) {
        log.log("agent login error,login agent interface - " + _Interface, -1);
        e.printStackTrace();
        result = null;
        log.log(e, Logger.LEVEL_FATAL);
    }
    if (result == null) {
        LoginResult loginResult = new LoginResult();
        loginResult.setResult(LoginResult.RESULT_ERROR);
        return loginResult;
    }
    if (result.indexOf("<" + Constants.SSO_USER_ID + ">") > -1 && result.indexOf("<" + Constants.SSO_LOGIN_RESULT_CODE + ">") > -1 && result.indexOf("<" + Constants.SSO_LOGIN_RESULT_MSG + ">") > -1) {
        Document doc = null;
        try {
            doc = JUtilDom4j.parseString(result, "UTF-8");
        } catch (Exception e) {
            log.log(e, Logger.LEVEL_ERROR);
            LoginResult loginResult = new LoginResult();
            loginResult.setResult(LoginResult.RESULT_ERROR);
            return loginResult;
        }
        Element root = doc.getRootElement();
        String sysId = root.elementText(Constants.SSO_SYS_ID);
        String machineId = root.elementText(Constants.SSO_MACHINE_ID);
        String userId = root.elementText(Constants.SSO_USER_ID);
        String code = root.elementText(Constants.SSO_LOGIN_RESULT_CODE);
        String msg = root.elementText(Constants.SSO_LOGIN_RESULT_MSG);
        LoginResult loginResult = new LoginResult();
        loginResult.setSysId(sysId);
        loginResult.setMachineId(machineId);
        loginResult.setUserId(userId);
        loginResult.setResult(Integer.parseInt(code));
        loginResult.setResultMsg(msg);
        List ms = root.element("messages").elements();
        for (int i = 0; i < ms.size(); i++) {
            Element m = (Element) ms.get(i);
            loginResult.setMessage(m.getName(), JObject.intSequence2String(m.getText()));
        }
        root = null;
        doc = null;
        return loginResult;
    } else {
        LoginResult loginResult = new LoginResult();
        loginResult.setResult(LoginResult.RESULT_ERROR);
        return loginResult;
    }
}
Also used : JHttpContext(j.http.JHttpContext) JHttp(j.http.JHttp) Element(org.dom4j.Element) List(java.util.List) JUtilString(j.util.JUtilString) Document(org.dom4j.Document)

Example 12 with JHttpContext

use of j.http.JHttpContext in project JFramework by gugumall.

the class SSOConfig method tellServerToDelUrl.

/**
 * @param clientId
 * @param url
 * @throws Exception
 */
public static void tellServerToDelUrl(String clientId, String url) throws Exception {
    if (clientId == null && url == null)
        return;
    try {
        String delUrl = SSOConfig.getSsoServer() + "ssoserver" + Handlers.getActionPathPattern();
        delUrl += "?" + Handlers.getHandler("/ssoserver").getRequestBy() + "=ssodelurl";
        delUrl += "&" + Constants.SSO_CLIENT + "=" + clientId;
        delUrl += "&url=" + JUtilString.encodeURI(url, SysConfig.sysEncoding);
        delUrl += "&" + Constants.SSO_PASSPORT + "=" + Permission.getSSOPassport();
        log.log("tellServerToDelUrl on url - " + delUrl, Logger.LEVEL_DEBUG);
        int loop = 0;
        String _response = "";
        while (!Constants.RESPONSE_OK.equals(_response) && loop < 3) {
            JHttpContext context = http.get(null, hclient, delUrl);
            _response = context.getStatus() == 200 ? context.getResponseText() : null;
            context.finalize();
            context = null;
            loop++;
            if (!Constants.RESPONSE_OK.equals(_response)) {
                try {
                    Thread.sleep(3000);
                } catch (Exception e) {
                }
            }
        }
        log.log("tellServerToDelUrl - " + _response, Logger.LEVEL_DEBUG);
    } catch (Exception ex) {
        log.log(ex, Logger.LEVEL_ERROR);
    }
}
Also used : JHttpContext(j.http.JHttpContext) JUtilString(j.util.JUtilString)

Example 13 with JHttpContext

use of j.http.JHttpContext in project JFramework by gugumall.

the class SSOConfig method tellServerToLoginSameProtocalAsBackUrl.

/**
 * @param clientId
 * @param ssoUserId
 * @param ssoBackUrl
 * @param ssoLoginPage
 * @param infos
 * @return
 * @throws Exception
 */
public static String tellServerToLoginSameProtocalAsBackUrl(String clientIdOrUrl, String ssoUserId, String ssoBackUrl, String ssoLoginPage, ConcurrentMap infos) throws Exception {
    try {
        String loginUrl = SSOConfig.getSsoServer() + "ssoserver" + Handlers.getActionPathPattern();
        loginUrl += "?" + Handlers.getHandler("/ssoserver").getRequestBy() + "=ssologinauto";
        if (JUtilString.getProtocal(ssoBackUrl).equals("http")) {
            loginUrl = JUtilString.replaceAll(loginUrl, "https://", "http://");
        }
        Map paras = new HashMap();
        paras.put(Constants.SSO_CLIENT, clientIdOrUrl);
        paras.put(Constants.SSO_USER_ID, ssoUserId);
        paras.put(Constants.SSO_LOGIN_INFO, JObject.serializable2String(infos, false));
        paras.put(Constants.SSO_PASSPORT, Permission.getSSOPassport());
        int loop = 0;
        String _response = "";
        while ((_response == null || !_response.startsWith(Constants.RESPONSE_OK + ":")) && loop < 3) {
            JHttpContext context = new JHttpContext();
            context.setAllowedErrorCodes(new String[] { "301" });
            context = http.post(context, hclient, loginUrl, paras);
            _response = context.getStatus() == 200 ? context.getResponseText() : null;
            context.finalize();
            context = null;
            loop++;
            if (_response == null || !_response.startsWith(Constants.RESPONSE_OK + ":")) {
                try {
                    Thread.sleep(3000);
                } catch (Exception e) {
                }
            }
        }
        log.log("tellServerToLogin - " + _response, Logger.LEVEL_DEBUG);
        if (_response == null || !_response.startsWith(Constants.RESPONSE_OK + ":")) {
            return null;
        } else {
            String globalSessionId = _response.substring(Constants.RESPONSE_OK.length() + 1, _response.lastIndexOf(":"));
            String token = _response.substring(_response.lastIndexOf(":") + 1);
            if (clientIdOrUrl.matches(JUtilString.RegExpHttpUrl)) {
                loginUrl = SSOConfig.getSsoServer(SSOConfig.getSsoClientByIdOrUrl(clientIdOrUrl), clientIdOrUrl) + "ssoserver" + Handlers.getActionPathPattern();
            } else if (ssoBackUrl.matches(JUtilString.RegExpHttpUrl)) {
                loginUrl = SSOConfig.getSsoServer(SSOConfig.getSsoClientByIdOrUrl(clientIdOrUrl), ssoBackUrl) + "ssoserver" + Handlers.getActionPathPattern();
            } else {
                loginUrl = SSOConfig.getSsoServer() + "ssoserver" + Handlers.getActionPathPattern();
            }
            loginUrl += "?" + Handlers.getHandler("/ssoserver").getRequestBy() + "=jump";
            loginUrl += "&" + Constants.SSO_CLIENT + "=" + clientIdOrUrl;
            if (ssoBackUrl != null) {
                loginUrl += "&" + Constants.SSO_BACK_URL + "=" + JUtilString.encodeURI(ssoBackUrl, SysConfig.sysEncoding);
            }
            if (ssoLoginPage != null) {
                loginUrl += "&" + Constants.SSO_LOGIN_PAGE + "=" + JUtilString.encodeURI(ssoLoginPage, SysConfig.sysEncoding);
            }
            loginUrl += "&" + Constants.SSO_GLOBAL_SESSION_ID + "=" + globalSessionId;
            loginUrl += "&" + Constants.SSO_TOKEN + "=" + token;
            log.log("sso jump url - " + loginUrl, Logger.LEVEL_DEBUG);
            return loginUrl;
        }
    } catch (Exception ex) {
        log.log(ex, Logger.LEVEL_ERROR);
        return null;
    }
}
Also used : JHttpContext(j.http.JHttpContext) HashMap(java.util.HashMap) JUtilString(j.util.JUtilString) HashMap(java.util.HashMap) ConcurrentMap(j.util.ConcurrentMap) Map(java.util.Map)

Example 14 with JHttpContext

use of j.http.JHttpContext in project JFramework by gugumall.

the class SSOConfig method tellServerToLogin.

/**
 * @param clientId
 * @param ssoUserId
 * @param ssoBackUrl
 * @param ssoLoginPage
 * @param infos
 * @return
 * @throws Exception
 */
public static String tellServerToLogin(String clientIdOrUrl, String ssoUserId, String ssoBackUrl, String ssoLoginPage, ConcurrentMap infos) throws Exception {
    try {
        String loginUrl = SSOConfig.getSsoServer() + "ssoserver" + Handlers.getActionPathPattern();
        loginUrl += "?" + Handlers.getHandler("/ssoserver").getRequestBy() + "=ssologinauto";
        Map paras = new HashMap();
        paras.put(Constants.SSO_CLIENT, clientIdOrUrl);
        paras.put(Constants.SSO_USER_ID, ssoUserId);
        paras.put(Constants.SSO_LOGIN_INFO, JObject.serializable2String(infos, false));
        paras.put(Constants.SSO_PASSPORT, Permission.getSSOPassport());
        int loop = 0;
        String _response = "";
        while ((_response == null || !_response.startsWith(Constants.RESPONSE_OK + ":")) && loop < 3) {
            JHttpContext context = new JHttpContext();
            context.setAllowedErrorCodes(new String[] { "301" });
            context = http.post(context, hclient, loginUrl, paras);
            _response = context.getStatus() == 200 ? context.getResponseText() : null;
            context.finalize();
            context = null;
            loop++;
            if (_response == null || !_response.startsWith(Constants.RESPONSE_OK + ":")) {
                try {
                    Thread.sleep(3000);
                } catch (Exception e) {
                }
            }
        }
        if (_response == null || !_response.startsWith(Constants.RESPONSE_OK + ":")) {
            return null;
        } else {
            String globalSessionId = _response.substring(Constants.RESPONSE_OK.length() + 1, _response.lastIndexOf(":"));
            String token = _response.substring(_response.lastIndexOf(":") + 1);
            if (clientIdOrUrl.matches(JUtilString.RegExpHttpUrl)) {
                loginUrl = SSOConfig.getSsoServer(SSOConfig.getSsoClientByIdOrUrl(clientIdOrUrl), clientIdOrUrl) + "ssoserver" + Handlers.getActionPathPattern();
            } else if (ssoBackUrl.matches(JUtilString.RegExpHttpUrl)) {
                loginUrl = SSOConfig.getSsoServer(SSOConfig.getSsoClientByIdOrUrl(clientIdOrUrl), ssoBackUrl) + "ssoserver" + Handlers.getActionPathPattern();
            } else {
                loginUrl = SSOConfig.getSsoServer() + "ssoserver" + Handlers.getActionPathPattern();
            }
            loginUrl += "?" + Handlers.getHandler("/ssoserver").getRequestBy() + "=jump";
            loginUrl += "&" + Constants.SSO_CLIENT + "=" + clientIdOrUrl;
            if (ssoBackUrl != null) {
                loginUrl += "&" + Constants.SSO_BACK_URL + "=" + JUtilString.encodeURI(ssoBackUrl, SysConfig.sysEncoding);
            }
            if (ssoLoginPage != null) {
                loginUrl += "&" + Constants.SSO_LOGIN_PAGE + "=" + JUtilString.encodeURI(ssoLoginPage, SysConfig.sysEncoding);
            }
            loginUrl += "&" + Constants.SSO_GLOBAL_SESSION_ID + "=" + globalSessionId;
            loginUrl += "&" + Constants.SSO_TOKEN + "=" + token;
            log.log("sso jump url - " + loginUrl, Logger.LEVEL_DEBUG);
            return loginUrl;
        }
    } catch (Exception ex) {
        log.log(ex, Logger.LEVEL_ERROR);
        return null;
    }
}
Also used : JHttpContext(j.http.JHttpContext) HashMap(java.util.HashMap) JUtilString(j.util.JUtilString) HashMap(java.util.HashMap) ConcurrentMap(j.util.ConcurrentMap) Map(java.util.Map)

Example 15 with JHttpContext

use of j.http.JHttpContext in project JFramework by gugumall.

the class SSONotifier method login.

/**
 * 通知登录
 * @param client
 * @param globalSessionId
 * @param userId
 * @param userIp
 */
public void login(Client client, String globalSessionId, String userId, String userIp) {
    long now = SysUtil.getNow();
    String md5 = JUtilMD5.MD5EncodeToHex(client.getPassport() + now + globalSessionId + userId + userIp);
    String url = client.getUrlDefault() + client.getLoginInterface();
    if (url.indexOf("?") > 0) {
        url += "&" + Constants.SSO_MD5_STRING + "=" + md5;
    } else {
        url += "?" + Constants.SSO_MD5_STRING + "=" + md5;
    }
    url += "&" + Constants.SSO_TIME + "=" + now;
    url += "&" + Constants.SSO_GLOBAL_SESSION_ID + "=" + globalSessionId;
    url += "&" + Constants.SSO_USER_ID + "=" + userId;
    url += "&" + Constants.SSO_USER_IP + "=" + userIp;
    url += "&" + Constants.SSO_PASSPORT + "=" + Permission.getSSOPassport();
    int loop = 0;
    while (loop < 3) {
        // 最多尝试3次
        loop++;
        try {
            JHttpContext context = http.get(null, httpClient, url);
            String response = context.getStatus() == 200 ? context.getResponseText() : null;
            context.finalize();
            context = null;
            // log.log("notify "+url+",client response:"+response,-1);
            break;
        } catch (Exception e) {
        }
    }
}
Also used : JHttpContext(j.http.JHttpContext)

Aggregations

JHttpContext (j.http.JHttpContext)26 JUtilString (j.util.JUtilString)16 RemoteException (java.rmi.RemoteException)8 JHttp (j.http.JHttp)6 HttpClient (org.apache.http.client.HttpClient)5 HashMap (java.util.HashMap)4 ConcurrentMap (j.util.ConcurrentMap)3 Map (java.util.Map)2 InitialContext (javax.naming.InitialContext)2 Client (j.app.sso.Client)1 JObject (j.common.JObject)1 JDFSFile (j.fs.JDFSFile)1 ServiceBase (j.service.server.ServiceBase)1 ConcurrentList (j.util.ConcurrentList)1 JUtilTextWriter (j.util.JUtilTextWriter)1 File (java.io.File)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Properties (java.util.Properties)1 Context (javax.naming.Context)1