You are viewing a plain text version of this content. The canonical link for it is here.
Posted to portalapps-dev@portals.apache.org by wo...@apache.org on 2009/09/28 17:31:19 UTC

svn commit: r819588 - in /portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy: SSOSiteCredentials.java impl/DefaultSSOSiteCredentials.java impl/RewritableHttpReverseProxyServiceImpl.java

Author: woonsan
Date: Mon Sep 28 15:31:18 2009
New Revision: 819588

URL: http://svn.apache.org/viewvc?rev=819588&view=rev
Log:
APA-17: Uses post method on form-based authentication.

Modified:
    portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/SSOSiteCredentials.java
    portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/DefaultSSOSiteCredentials.java
    portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/RewritableHttpReverseProxyServiceImpl.java

Modified: portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/SSOSiteCredentials.java
URL: http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/SSOSiteCredentials.java?rev=819588&r1=819587&r2=819588&view=diff
==============================================================================
--- portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/SSOSiteCredentials.java (original)
+++ portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/SSOSiteCredentials.java Mon Sep 28 15:31:18 2009
@@ -19,6 +19,8 @@
 public interface SSOSiteCredentials
 {
     
+    public String getBaseURL();
+    
     public String getHost();
     
     public int getPort();

Modified: portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/DefaultSSOSiteCredentials.java
URL: http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/DefaultSSOSiteCredentials.java?rev=819588&r1=819587&r2=819588&view=diff
==============================================================================
--- portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/DefaultSSOSiteCredentials.java (original)
+++ portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/DefaultSSOSiteCredentials.java Mon Sep 28 15:31:18 2009
@@ -5,6 +5,7 @@
 public class DefaultSSOSiteCredentials implements SSOSiteCredentials
 {
     
+    private String baseURL;
     private String host;
     private int port = -1;
     private String realm;
@@ -23,23 +24,39 @@
         this(null);
     }
     
-    public DefaultSSOSiteCredentials(String host)
+    public DefaultSSOSiteCredentials(String baseURL)
     {
-        this(host, -1);
+        this(baseURL, null);
     }
     
-    public DefaultSSOSiteCredentials(String host, int port)
+    public DefaultSSOSiteCredentials(String baseURL, String host)
     {
-        this(host, port, null);
+        this(baseURL, host, -1);
     }
     
-    public DefaultSSOSiteCredentials(String host, int port, String realm)
+    public DefaultSSOSiteCredentials(String baseURL, String host, int port)
     {
+        this(baseURL, host, port, null);
+    }
+    
+    public DefaultSSOSiteCredentials(String baseURL, String host, int port, String realm)
+    {
+        this.baseURL = baseURL;
         this.host = host;
         this.port = port;
         this.realm = realm;
     }
     
+    public String getBaseURL()
+    {
+        return baseURL;
+    }
+    
+    public void setBaseURL(String baseURL)
+    {
+        this.baseURL = baseURL;
+    }
+    
     public String getHost()
     {
         return host;

Modified: portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/RewritableHttpReverseProxyServiceImpl.java
URL: http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/RewritableHttpReverseProxyServiceImpl.java?rev=819588&r1=819587&r2=819588&view=diff
==============================================================================
--- portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/RewritableHttpReverseProxyServiceImpl.java (original)
+++ portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/RewritableHttpReverseProxyServiceImpl.java Mon Sep 28 15:31:18 2009
@@ -24,6 +24,7 @@
 import java.io.Reader;
 import java.io.Writer;
 import java.net.URI;
+import java.util.ArrayList;
 import java.util.Enumeration;
 import java.util.List;
 
@@ -32,12 +33,15 @@
 import javax.servlet.http.HttpSession;
 
 import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang.StringUtils;
 import org.apache.http.Header;
 import org.apache.http.HttpEntity;
 import org.apache.http.HttpResponse;
+import org.apache.http.NameValuePair;
 import org.apache.http.auth.AuthScope;
 import org.apache.http.auth.Credentials;
 import org.apache.http.auth.UsernamePasswordCredentials;
+import org.apache.http.client.entity.UrlEncodedFormEntity;
 import org.apache.http.client.methods.HttpGet;
 import org.apache.http.client.methods.HttpPost;
 import org.apache.http.client.methods.HttpRequestBase;
@@ -50,6 +54,7 @@
 import org.apache.http.entity.InputStreamEntity;
 import org.apache.http.impl.client.DefaultHttpClient;
 import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
+import org.apache.http.message.BasicNameValuePair;
 import org.apache.http.params.BasicHttpParams;
 import org.apache.http.params.HttpParams;
 import org.apache.http.protocol.HTTP;
@@ -212,42 +217,15 @@
         // redirection should be adjusted with local host header...
         httpClient.getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, false);
         
-        // set sso credentials if available
-        List<SSOSiteCredentials> credsList = getSSOSiteCredentials(proxyTargetURL, httpClient, request);
-        if (credsList != null && !credsList.isEmpty())
-        {
-            if (credsList.get(0).isFormAuthentication())
-            {
-                SSOSiteCredentials formCreds = credsList.get(0);
-                proxyTargetURL = new StringBuilder(proxyTargetURL)
-                .append(proxyTargetURL.indexOf('?') == -1 ? '?' : '&')
-                .append(formCreds.getFormUserField()).append('=').append(formCreds.getUsername())
-                .append('&')
-                .append(formCreds.getFormPwdField()).append('=').append(formCreds.getPassword())
-                .toString();
-            }
-            else
-            {
-                for (SSOSiteCredentials creds : credsList)
-                {
-                    AuthScope authScope = new AuthScope(creds.getHost(), creds.getPort(), creds.getRealm(), creds.getScheme());
-                    Credentials usernamePwdCreds = new UsernamePasswordCredentials(creds.getUsername(), creds.getPassword());
-                    httpClient.getCredentialsProvider().setCredentials(authScope, usernamePwdCreds);
-                }
-            }
-        }
-        
         String method = request.getMethod();
-        boolean isGetMethod = "GET".equals(method);
-        boolean isPostMethod = "POST".equals(method);
         
         HttpRequestBase httpRequest = null;
         
-        if (isGetMethod)
+        if ("GET".equals(method))
         {
             httpRequest = new HttpGet(proxyTargetURL);
         }
-        else if (isPostMethod)
+        else if ("POST".equals(method))
         {
             httpRequest = new HttpPost(proxyTargetURL);
             HttpEntity entity = new InputStreamEntity(request.getInputStream(), 4096);
@@ -258,6 +236,31 @@
             throw new IOException("Unsupported method: " + method);
         }
         
+        // set sso credentials if available
+        List<SSOSiteCredentials> credsList = getSSOSiteCredentials(proxyTargetURL, httpClient, request);
+        if (credsList != null && !credsList.isEmpty())
+        {
+            SSOSiteCredentials firstCreds = credsList.get(0);
+            
+            if (firstCreds.isFormAuthentication() && areSameURLPaths(firstCreds.getBaseURL(), proxyTargetURL))
+            {
+                httpRequest = new HttpPost(proxyTargetURL);
+                List <NameValuePair> formParams = new ArrayList<NameValuePair>();
+                formParams.add(new BasicNameValuePair(firstCreds.getFormUserField(), firstCreds.getUsername()));
+                formParams.add(new BasicNameValuePair(firstCreds.getFormPwdField(), firstCreds.getPassword()));
+                ((HttpPost) httpRequest).setEntity(new UrlEncodedFormEntity(formParams));
+            }
+            else
+            {
+                for (SSOSiteCredentials creds : credsList)
+                {
+                    AuthScope authScope = new AuthScope(creds.getHost(), creds.getPort(), creds.getRealm(), creds.getScheme());
+                    Credentials usernamePwdCreds = new UsernamePasswordCredentials(creds.getUsername(), creds.getPassword());
+                    httpClient.getCredentialsProvider().setCredentials(authScope, usernamePwdCreds);
+                }
+            }
+        }
+        
         // pass most headers to proxy target...
         for (Enumeration enumHeaderNames = request.getHeaderNames(); enumHeaderNames.hasMoreElements(); ) 
         {
@@ -537,5 +540,15 @@
         }
     }
     
+    private boolean areSameURLPaths(String url1, String url2)
+    {
+        if (url1 != null && url2 != null)
+        {
+            return StringUtils.removeEnd(url1, "/").equals(StringUtils.removeEnd(url2, "/"));
+        }
+        
+        return false;
+    }
+    
 }