Removed Acegi callback for now, will be back in another form.

This commit is contained in:
Arjen Poutsma
2008-02-09 22:47:04 +00:00
parent f53389389b
commit d74683bf34
5 changed files with 0 additions and 309 deletions

View File

@@ -1,138 +0,0 @@
/*
* Copyright 2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ws.soap.security.wss4j.callback.acegi;
import org.acegisecurity.Authentication;
import org.acegisecurity.AuthenticationException;
import org.acegisecurity.AuthenticationManager;
import org.acegisecurity.context.SecurityContextHolder;
import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
import org.acegisecurity.providers.dao.UserCache;
import org.acegisecurity.providers.dao.cache.NullUserCache;
import org.acegisecurity.userdetails.UserDetails;
import org.acegisecurity.userdetails.UserDetailsService;
import org.acegisecurity.userdetails.UsernameNotFoundException;
import org.apache.ws.security.WSPasswordCallback;
import org.apache.ws.security.WSSecurityException;
import org.springframework.dao.DataAccessException;
import org.springframework.util.Assert;
import org.springframework.ws.soap.security.wss4j.callback.AbstractWss4jCallbackHandler;
/** @author Tareq Abed Rabbo */
public class AcegiCallbackHandler extends AbstractWss4jCallbackHandler {
private AuthenticationManager authenticationManager;
private UserCache userCache = new NullUserCache();
private UserDetailsService userDetailsService;
public UserCache getUserCache() {
return userCache;
}
public void setUserCache(UserCache userCache) {
this.userCache = userCache;
}
public UserDetailsService getUserDetailsService() {
return userDetailsService;
}
public void setUserDetailsService(UserDetailsService userDetailsService) {
this.userDetailsService = userDetailsService;
}
public AuthenticationManager getAuthenticationManager() {
return authenticationManager;
}
public void setAuthenticationManager(AuthenticationManager authenticationManager) {
this.authenticationManager = authenticationManager;
}
public void afterPropertiesSet() throws Exception {
super.afterPropertiesSet();
if (isPasswordPlainTextRequired()) {
Assert.notNull(authenticationManager, "authenticationManager is required");
}
if (isPasswordDigestRequired()) {
Assert
.notNull(userDetailsService, "userDetailsService is required");
}
}
protected void validateUsernameTokenPlainText(WSPasswordCallback callback) throws WSSecurityException {
if (isPasswordPlainTextRequired()) {
Assert
.notNull(authenticationManager,
"authenticationManager is required to validate a usernameToken with a plain text password");
}
try {
Authentication authResult = authenticationManager
.authenticate(
new UsernamePasswordAuthenticationToken(callback.getIdentifer(), callback.getPassword()));
if (logger.isDebugEnabled()) {
logger
.debug("Authentication success: " + authResult.toString());
}
SecurityContextHolder.getContext().setAuthentication(authResult);
}
catch (AuthenticationException failed) {
if (logger.isDebugEnabled()) {
logger.debug("Authentication request for user '" + callback.getIdentifer() + "' failed: " +
failed.toString());
}
SecurityContextHolder.getContext().setAuthentication(null);
throw new WSSecurityException(WSSecurityException.FAILURE);
}
}
protected void validateUsernameTokenDigest(WSPasswordCallback callback) throws WSSecurityException {
if (isPasswordDigestRequired()) {
Assert
.notNull(userDetailsService,
"userDetailsService is required to validate a usernameToken with a digest password");
}
UserDetails user = loadUserDetails(callback.getIdentifer());
if (user != null) {
callback.setPassword(user.getPassword());
}
}
private UserDetails loadUserDetails(String username) throws DataAccessException {
UserDetails user = userCache.getUserFromCache(username);
if (user == null) {
try {
user = userDetailsService.loadUserByUsername(username);
}
catch (UsernameNotFoundException notFound) {
if (logger.isDebugEnabled()) {
logger.debug("Username '" + username + "' not found");
}
return null;
}
userCache.putUserInCache(user);
}
return user;
}
}

View File

@@ -1,80 +0,0 @@
/*
* Copyright 2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ws.soap.security.wss4j.callback.acegi;
import java.lang.reflect.Method;
import java.util.Vector;
import org.acegisecurity.context.SecurityContextHolder;
import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
import org.apache.ws.security.WSConstants;
import org.apache.ws.security.WSSecurityEngineResult;
import org.apache.ws.security.WSUsernameTokenPrincipal;
import org.apache.ws.security.handler.WSHandlerConstants;
import org.apache.ws.security.handler.WSHandlerResult;
import org.apache.ws.security.util.WSSecurityUtil;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.ThrowsAdvice;
import org.springframework.ws.context.MessageContext;
import org.springframework.ws.soap.security.wss4j.Wss4jSecurityInterceptor;
/**
* This class is responsible for setting Acegi's security context after the request is validated. It must be used in
* conjunction with AcegiCallbackHandler when validating a username token with a digest password.
*
* @author tareq.abedrabbo
*/
public class AcegiSecurityContextUpdateAdvice implements AfterReturningAdvice, ThrowsAdvice {
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
if (!(Wss4jSecurityInterceptor.class
.isAssignableFrom(target.getClass()))) {
throw new IllegalArgumentException(
"AcegiSecurityContextUpdateAdvice can only be applied to a Wss4jSecurityInterceptor");
}
MessageContext context = (MessageContext) args[0];
Vector wsHandlerResults = (Vector) context
.getProperty(WSHandlerConstants.RECV_RESULTS);
if (wsHandlerResults != null) {
WSHandlerResult handlerResult = (WSHandlerResult) wsHandlerResults
.get(0);
Vector results = handlerResult.getResults();
WSSecurityEngineResult actionResult = WSSecurityUtil
.fetchActionResult(results, WSConstants.UT);
if (actionResult != null) {
WSUsernameTokenPrincipal principal = (WSUsernameTokenPrincipal) actionResult
.getPrincipal();
if (principal.getPasswordType().equals(WSConstants.PASSWORD_DIGEST)) {
String user = principal.getName();
String password = principal.getPassword();
UsernamePasswordAuthenticationToken authRequest =
new UsernamePasswordAuthenticationToken(user, password);
SecurityContextHolder.getContext().setAuthentication(authRequest);
}
}
}
}
public void afterThrowing(Method method, Object[] args, Object target, Exception ex) throws Throwable {
SecurityContextHolder.getContext().setAuthentication(null);
}
}

View File

@@ -1,6 +0,0 @@
package org.springframework.ws.soap.security.wss4j;
public class AxiomWss4jMessageInterceptorAcegiCallbackHandlerTest
extends Wss4jMessageInterceptorAcegiCallbackHandlerTestCase {
}

View File

@@ -1,6 +0,0 @@
package org.springframework.ws.soap.security.wss4j;
public class SaajWss4jMessageInterceptorAcegiCallbackHandlerTest
extends Wss4jMessageInterceptorAcegiCallbackHandlerTestCase {
}

View File

@@ -1,79 +0,0 @@
package org.springframework.ws.soap.security.wss4j;
import java.util.Properties;
import org.acegisecurity.Authentication;
import org.acegisecurity.context.SecurityContextHolder;
import org.acegisecurity.userdetails.memory.InMemoryDaoImpl;
import org.apache.ws.security.WSConstants;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.NameMatchMethodPointcutAdvisor;
import org.springframework.ws.context.DefaultMessageContext;
import org.springframework.ws.context.MessageContext;
import org.springframework.ws.server.EndpointInterceptor;
import org.springframework.ws.soap.SoapMessage;
import org.springframework.ws.soap.security.wss4j.callback.acegi.AcegiCallbackHandler;
import org.springframework.ws.soap.security.wss4j.callback.acegi.AcegiSecurityContextUpdateAdvice;
public abstract class Wss4jMessageInterceptorAcegiCallbackHandlerTestCase extends Wss4jTestCase {
private Properties users = new Properties();
protected void onSetup() throws Exception {
users.setProperty("Bert", "Ernie,ROLE_TEST");
}
public void testValidateUsernameTokenDigest() throws Exception {
EndpointInterceptor interceptor = prepareInterceptor("UsernameToken", true, true);
SoapMessage message = loadMessage("usernameTokenDigest-soap.xml");
MessageContext messageContext = new DefaultMessageContext(message, getMessageFactory());
interceptor.handleRequest(messageContext, null);
assertValidateUsernameToken(message);
}
protected void assertValidateUsernameToken(SoapMessage message) throws Exception {
Object result = getMessage(message);
assertNotNull("No result returned", result);
assertXpathNotExists("Security Header not removed", "/SOAP-ENV:Envelope/SOAP-ENV:Header/wsse:Security",
getDocument(message));
Authentication authentication = SecurityContextHolder.getContext()
.getAuthentication();
assertNotNull("authentication must not be null", authentication);
}
protected EndpointInterceptor prepareInterceptor(String actions, boolean validating, boolean digest)
throws Exception {
Wss4jSecurityInterceptor interceptor = new Wss4jSecurityInterceptor();
if (validating) {
interceptor.setValidationActions(actions);
}
else {
interceptor.setSecurementActions(actions);
}
AcegiCallbackHandler callbackHandler = new AcegiCallbackHandler();
InMemoryDaoImpl userDetailsService = new InMemoryDaoImpl();
userDetailsService.setUserProperties(users);
userDetailsService.afterPropertiesSet();
callbackHandler.setUserDetailsService(userDetailsService);
if (digest) {
callbackHandler.setPasswordDigestRequired(true);
callbackHandler.setPasswordPlainTextRequired(false);
interceptor.setSecurementPasswordType(WSConstants.PW_DIGEST);
}
else {
callbackHandler.setPasswordDigestRequired(false);
callbackHandler.setPasswordPlainTextRequired(true);
interceptor.setSecurementPasswordType(WSConstants.PW_TEXT);
}
interceptor.setValidationCallbackHandler(callbackHandler);
interceptor.afterPropertiesSet();
ProxyFactory factory = new ProxyFactory(interceptor);
AcegiSecurityContextUpdateAdvice advice = new AcegiSecurityContextUpdateAdvice();
NameMatchMethodPointcutAdvisor advisor = new NameMatchMethodPointcutAdvisor(advice);
advisor.setMappedName("handleRequest");
factory.addAdvisor(advisor);
return (EndpointInterceptor) factory.getProxy();
}
}