View Full Version : acegi with commons-httpclient
billsalvucci
Jul 15th, 2006, 04:20 PM
I have an application that transfers files using a MultiPart post request via commons-httpclient. I want to add acegi security to it.
I supose I could simply post a request to j_acegi_security_check with the username and password before I post the MultiPart, but I'm hoping that someone has done more a framework for this.
I'm thinking that an interceptor could be used to post to j_acegi_security_check, but I only want to do this when I need to. I can't keep state on the client because the session may have expired on the server. Maybe I write a simple AuthenticationServlet that I post to too see if I get an authentication error, if so, I do the post to j_acegi_security_check with the username password.
I suspect someone much smarter than me has already done something.
billsalvucci
Jul 24th, 2006, 03:11 PM
So here is what I did:
package com.rjlg.commons.frmwrk.httpclient;
import java.lang.reflect.Method;
import org.apache.commons.logging.*;
import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.auth.AuthScope;
import org.springframework.aop.MethodBeforeAdvice;
public class HttpClientAuthAdvice implements MethodBeforeAdvice {
private final static Log log =
LogFactory.getLog(HttpClientAuthAdvice.class);
private AuthScope authScope;
private Credentials credentials;
private AuthScope proxyAuthScope;
private Credentials proxyCredentials;
private boolean authenticationPreemptive = true;
public void before(Method arg0, Object[] arg1, Object arg2)
throws Throwable {
HttpClient client = (HttpClient) arg2;
client.getParams().setAuthenticationPreemptive(
this.isAuthenticationPreemptive());
log.debug("setting credentials: " + String.valueOf(this.getCredentials()));
log.debug("authScope: " + String.valueOf(this.getAuthScope()));
client.getState()
.setCredentials(this.getAuthScope(), this.getCredentials());
client.getState()
.setProxyCredentials(this.getProxyAuthScope(), this.getProxyCredentials());
HttpMethod method = (HttpMethod) ((arg1.length == 1) ? arg1[0] : arg1[1]);
method.setDoAuthentication(true);
method.setFollowRedirects(false);
}
public Credentials getCredentials() {
return credentials;
}
public void setCredentials(Credentials credentials) {
this.credentials = credentials;
}
public AuthScope getAuthScope() {
return this.authScope;
}
public void setAuthScope(AuthScope scope) {
this.authScope = scope;
}
public boolean isAuthenticationPreemptive() {
return authenticationPreemptive;
}
public void setAuthenticationPreemptive(boolean authenticationPreemptive) {
this.authenticationPreemptive = authenticationPreemptive;
}
public AuthScope getProxyAuthScope() {
return proxyAuthScope;
}
public void setProxyAuthScope(AuthScope proxyAuthScope) {
this.proxyAuthScope = proxyAuthScope;
}
public Credentials getProxyCredentials() {
return proxyCredentials;
}
public void setProxyCredentials(Credentials proxyCredentials) {
this.proxyCredentials = proxyCredentials;
}
}
<bean id="httpClient"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target">
<bean class="org.apache.commons.httpclient.HttpClient">
<constructor-arg
type="org.apache.commons.httpclient.HttpConnectionManage r"
index="0">
<bean
class="org.apache.commons.httpclient.MultiThreadedHttpCon nectionManager"/>
</constructor-arg>
<property name="hostConfiguration">
<ref bean="hostConfiguration"/>
</property>
</bean>
</property>
<property name="interceptorNames">
<list>
<value>httpClientAuthAdvisor</value>
</list>
</property>
</bean>
<bean id="httpClientAuthAdvisor" class="org.springframework.aop.support.RegexpMethodPointc utAdvisor" >
<property name="advice">
<ref local="httpClientAuthAdvice" />
</property>
<property name="patterns">
<list>
<value>.*executeMethod.*</value>
</list>
</property>
</bean>
<bean id="httpClientAuthAdvice" class="com.rjlg.commons.frmwrk.httpclient.HttpClientAuthA dvice">
<property name="authScope">
<ref bean="authScope"/>
</property>
<property name="credentials">
<ref bean="credentials"/>
</property>
<property name="proxyAuthScope">
<ref bean="proxyAuthScope"/>
</property>
<property name="proxyCredentials">
<ref bean="proxyCredentials"/>
</property>
<property name="authenticationPreemptive">
<value>true</value>
</property>
</bean>
Powered by vBulletin® Version 4.2.1 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.