2012-05-07 74 views
12

我們正在嘗試爲我們現有的基於Web的應用程序使用CAS服務器進行SSO。我們的目標是JASIG CAS登錄無CAS登錄屏幕

  • 在各種應用程序(包括跨域)中實現SSO。
  • 當它們被重定向到CAS Server登錄頁面時,爲不同的應用程序定製登錄(在UI條款中)頁面。
  • 無需進入CAS登錄頁面即可進行登錄,原因在於「小登錄 」部分嵌入在頁面本身中,用戶不會被重定向到CAS登錄頁面以獲得更好的可用性。

我們完成了第一個和第二個目標。但是遇到第三個問題。

對於此功能,我們試圖複製與第二個目標相同的操作,唯一不同的是從非CAS登錄頁面提交/發佈數據 (憑證,登錄憑單等)。

  • 我們不能使用iframe在小部分中顯示CAS登錄頁面,這容易受到瀏覽器兼容性問題的影響。
  • 我們不能使用ajax來獲取登錄票據和執行HTTP post(跨域問題)
  • 我們所做的是:在非cas登錄頁面的負載上檢索loginticket和執行id通過在服務器端進行HTTP發佈。 當我們發佈用戶名/密碼以及loginticket和execId時,CAS服務器而不是接受發佈數據 將用戶重定向到CAS登錄頁面,但重做瀏覽器並重新提交數據可以正常工作。原因 沒有在CAS和瀏覽器之間建立起來,因此CAS拒絕任何發佈數據。我們可以使用CAS restAPI,但它只會登錄用戶,無法幫助完成SSO。

有關我們如何處理這個問題的任何想法?

感謝, PRATIK

回答

0

我知道它的晚了,但如果有人正在尋找答案,這是我如何解決這個問題。這是我輸入的代碼casLoginView.jsp

<head> 
    <script language="javascript"> 
     function doAutoLogin() { 
      document.forms[0].submit(); 
     } 
    </script> 
</head> 
<body onload="doAutoLogin();"> 
    <form id="credentials" method="POST" action="<%= request.getContextPath() %>/login?service=<%= request.getParameter("service") %>"> 
     <input type="hidden" name="lt" value="${loginTicket}" /> 
     <input type="hidden" name="execution" value="${flowExecutionKey}" /> 
     <input type="hidden" name="_eventId" value="submit" /> 
     <input type="hidden" name="serviceLogin" value="<%= request.getParameter("serviceLogin") %>"/> 
     <input type="hidden" name="username" value="<%= request.getParameter("username") %>" /> 
     <input type="hidden" name="password" value="<%= request.getParameter("password") %>" /> 
     <% 
     if ("true".equals(request.getParameter("rememberMe"))) {%> 
      <input type="hidden" name="rememberMe" id="rememberMe" value="true"/> 
     <% } %> 

     <input type="submit" value="Submit" style="visibility: hidden;" /> 
    </form> 
    <% } else { 
     response.sendRedirect(request.getParameter("redirectURL")); 
     } 
    %> 
</body> 

而在您的web應用程序中,只需向您的CAS服務器發出POST請求。

希望它可以幫助

0
  1. 你必須得到的CAS官方客戶端源代碼(CAS-客戶爲核心,https://github.com/apereo/java-cas-client)複印件,並確保你可以編譯它。

  2. 您需要更改org.jasig.cas.client.authentication處的doFilter()函數代碼。客戶端源代碼中的AuthenticationFilter如下所示。

    final HttpServletRequest request = (HttpServletRequest) servletRequest; 
    final HttpServletResponse response = (HttpServletResponse) servletResponse; 
    final HttpSession session = request.getSession(false); 
    final Assertion assertion = session != null ? (Assertion) session.getAttribute(CONST_CAS_ASSERTION) : null; 
    
    if(request.getServletPath().toLowerCase().equals("/caslogout.jsp")) 
    { 
        // Set the custom client login page when you logout from CAS server. 
        request.setAttribute("casServerLogoutUrl",casServerLoginUrl.replace("login","logout")); 
        request.setAttribute("customServerLoginUrl",customServerLoginUrl); 
    
        //We must remove the attribute of CONST_CAS_ASSERTION manually 
        if(session!=null) 
         session.removeAttribute(CONST_CAS_ASSERTION); 
    
        filterChain.doFilter(request, response); 
        return; 
    } 
    
    if (assertion != null) { 
        filterChain.doFilter(request, response); 
        return; 
    } 
    
    // Although the custom login page must called caslogin, here you can change it. 
    if(request.getServletPath().toLowerCase().equals("/caslogin.jsp")) 
    { 
        //Set the a default parameter to the caslogin 
        request.setAttribute("defaultServerIndexUrl",defaultServerIndexUrl); 
        request.setAttribute("casServerLoginUrl",casServerLoginUrl); 
        filterChain.doFilter(request, response); 
        return; 
    } 
    
    final String serviceUrl = constructServiceUrl(request, response); 
    final String ticket = CommonUtils.safeGetParameter(request,getArtifactParameterName()); 
    final boolean wasGatewayed = this.gatewayStorage.hasGatewayedAlready(request, serviceUrl); 
    
    if (CommonUtils.isNotBlank(ticket) || wasGatewayed) { 
        filterChain.doFilter(request, response); 
        return; 
    } 
    
    final String modifiedServiceUrl; 
    
    log.debug("no ticket and no assertion found"); 
    if (this.gateway) { 
        log.debug("setting gateway attribute in session"); 
        modifiedServiceUrl = this.gatewayStorage.storeGatewayInformation(request, serviceUrl); 
    } else { 
        modifiedServiceUrl = serviceUrl; 
    } 
    
    if (log.isDebugEnabled()) { 
        log.debug("Constructed service url: " + modifiedServiceUrl); 
    } 
    
    final String urlToRedirectTo = CommonUtils.constructRedirectUrl(this.casServerLoginUrl, getServiceParameterName(), modifiedServiceUrl, this.renew, this.gateway); 
    
    if (log.isDebugEnabled()) { 
        log.debug("redirecting to \"" + urlToRedirectTo + "\""); 
    } 
    
    // Add a custom server login url parameter to the CAS login url. 
    response.sendRedirect(urlToRedirectTo+"&customLogin=custom&customLoginPage="+customServerLoginUrl); 
    
  3. 自己編譯的CAS客戶端核心添加到您的客戶端Web應用程序的依賴。

  4. 將caslogin.jsp添加到您的客戶端Web應用程序。

 <form method="GET" action="<%=request.getAttribute("casServerLoginUrl")%>"> 
 
    <p>Username : <input type="text" name="username" /></p> 
 
    <p>Password : <input type="password" name="password" /></p> 
 
    <p><input type="submit" value="Login" /></p> 
 
    <input type="hidden" name="auto" value="true" /> 
 
    <input type="hidden" name="service" value="<%=request.getParameter("service")==null?request.getAttribute("defaultServerIndexUrl"):request.getParameter("service")%>" />

  • 編輯在客戶端應用中web.xml。添加以下代碼中CASFilter
  • 的濾波器

    <init-param> 
     
        <param-name>defaultServerIndexUrl</param-name> 
     
        <param-value>http://clientip:port/webappname/index.jsp</param-value> 
     
    </init-param> 
     
    <init-param> 
     
        <param-name>customServerLoginUrl</param-name> 
     
        <param-value>http://clientip:port/webappname/caslogin.jsp</param-value> 
     
    </init-param>

  • 編輯在CAS-服務器的web應用的代碼/ WEB-INF /視圖/ JSP/CAS服務器Web應用程序中的默認/ ui/casLoginView.jsp。
  • <% 
     
        String auto=request.getParameter("auto"); 
     
        String customLogin=request.getParameter("customLogin"); 
     
    
     
        if(auto!=null&&auto.equals("true")) 
     
        { 
     
        %> 
     
        <html> 
     
        <head> 
     
         <script language="javascript"> 
     
         function doAutoLogin() 
     
         { 
     
          document.forms[0].submit(); 
     
         } 
     
         </script> 
     
        </head> 
     
        <body onload="doAutoLogin()"> 
     
        <form id="credentials" method="POST" action="<%=request.getContextPath()%>/login?service=<%=request.getParameter("service")%>"> 
     
         <input type="hidden" name="lt" value="${loginTicket}" /> 
     
         <input type="hidden" name="execution" value="${flowExecutionKey}" /> 
     
         <input type="hidden" name="_eventId" value="submit" /> 
     
         <input type="hidden" name="username" value="<%=request.getParameter("username")%>" /> 
     
         <input type="hidden" name="password" value="<%=request.getParameter("password")%>" /> 
     
         <input type="hidden" name="login_form" value="<%=request.getParameter("login_form")%>" /> 
     
         <input type="hidden" name="rememberMe" value="true" /> 
     
         <input type="submit" value="Submit" style="visibility: hidden" /> 
     
        </form> 
     
        </body> 
     
        </html> 
     
    
     
        <% 
     
    } 
     
    else if(customLogin!=null&&customLogin.equals("custom")) 
     
    { 
     
        response.sendRedirect(request.getParameter("customLoginPage")+"?service="+request.getParameter("service")); 
     
    %> 
     
    <% 
     
    } 
     
    else 
     
    {%> 
     
    <!-- The Orgin Source Code of casLoginView.jsp!!!!!!!!!!!!!!!!!!!!!!!!! --> 
     
    <%}%>

    1. 確保你可以登錄與CAS caslogin.jsp。
    2. 將您自己的登錄頁面的內容放入caslogin.jsp。
    3. 現在你可以登錄CAS用自己的caslogin.jsp

    我也做一個關於如何登錄使用客戶端自定義登錄屏幕,而不是服務器登錄srceen CAS樣品。你可以下載它
    https://github.com/yangminxing/cas-custom-login-page

    0

    登錄無需登錄屏幕頁面CAS,我自定義的流登錄(另寫動作狀態)

    1.In登錄-webflow.xml你寫的其他動作狀態在action-state id =「generateLoginTicket」中進行轉換。在這個動作狀態下(我稱它爲submitNotUseForm),我做同樣的「realSubmit」動作狀態。

    2.In評估 「submitNotUseForm」 的 - >類AuthenticationViaFormAction,我寫的方法submitNotForm(),並檢查:

    2.1:如果沒有服務調用,它調用 「viewLoginForm」 否則返回值我得到的參數從請求集憑據

    2.2:其他的一切都同樣的方法提交

    這對我的工作!