Moved Spring Security handlers to default callback packag, getting rid of the (ugly) springsecurity package.

This commit is contained in:
Arjen Poutsma
2008-02-25 14:34:37 +00:00
parent c68df1a55d
commit 1922082b41
12 changed files with 228 additions and 37 deletions

View File

@@ -0,0 +1,108 @@
/*
* Copyright 2008 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;
import java.io.IOException;
import javax.security.auth.callback.UnsupportedCallbackException;
import org.apache.ws.security.WSPasswordCallback;
import org.apache.ws.security.WSUsernameTokenPrincipal;
import org.springframework.dao.DataAccessException;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.security.providers.UsernamePasswordAuthenticationToken;
import org.springframework.security.providers.dao.UserCache;
import org.springframework.security.providers.dao.cache.NullUserCache;
import org.springframework.security.userdetails.UserDetails;
import org.springframework.security.userdetails.UserDetailsService;
import org.springframework.security.userdetails.UsernameNotFoundException;
import org.springframework.util.Assert;
import org.springframework.ws.soap.security.callback.CleanupCallback;
/**
* Callback handler that validates a password digest using an Spring Security <code>UserDetailsService</code>. Logic
* based on Spring Security's <code>DigestProcessingFilter</code>.
* <p/>
* An Spring Security <code>UserDetailService</code> is used to load <code>UserDetails</code> from. The digest of the
* password contained in this details object is then compared with the digest in the message.
*
* @author Arjen Poutsma
* @see org.springframework.security.userdetails.UserDetailsService
* @see org.springframework.security.ui.digestauth.DigestProcessingFilter
* @since 1.5.0
*/
public class SpringDigestPasswordValidationCallbackHandler extends AbstractWsPasswordCallbackHandler {
private UserCache userCache = new NullUserCache();
private UserDetailsService userDetailsService;
/** Sets the users cache. Not required, but can benefit performance. */
public void setUserCache(UserCache userCache) {
this.userCache = userCache;
}
/** Sets the Spring Security user details service. Required. */
public void setUserDetailsService(UserDetailsService userDetailsService) {
this.userDetailsService = userDetailsService;
}
public void afterPropertiesSet() throws Exception {
Assert.notNull(userDetailsService, "userDetailsService is required");
}
protected void handleUsernameToken(WSPasswordCallback callback) throws IOException, UnsupportedCallbackException {
String identifier = callback.getIdentifer();
UserDetails user = loadUserDetails(identifier);
if (user != null) {
callback.setPassword(user.getPassword());
}
}
protected void handleUsernameTokenPrincipal(UsernameTokenPrincipalCallback callback)
throws IOException, UnsupportedCallbackException {
WSUsernameTokenPrincipal principal = callback.getPrincipal();
UsernamePasswordAuthenticationToken authRequest =
new UsernamePasswordAuthenticationToken(principal, principal.getPassword());
if (logger.isDebugEnabled()) {
logger.debug("Authentication success: " + authRequest.toString());
}
SecurityContextHolder.getContext().setAuthentication(authRequest);
}
protected void handleCleanup(CleanupCallback callback) throws IOException, UnsupportedCallbackException {
SecurityContextHolder.clearContext();
}
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

@@ -0,0 +1,90 @@
/*
* Copyright 2008 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;
import java.io.IOException;
import javax.security.auth.callback.UnsupportedCallbackException;
import org.apache.ws.security.WSPasswordCallback;
import org.apache.ws.security.WSSecurityException;
import org.springframework.security.Authentication;
import org.springframework.security.AuthenticationException;
import org.springframework.security.AuthenticationManager;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.security.providers.UsernamePasswordAuthenticationToken;
import org.springframework.util.Assert;
import org.springframework.ws.soap.security.callback.CleanupCallback;
/**
* Callback handler that validates a certificate uses an Spring Security <code>AuthenticationManager</code>. Logic based
* on Spring Security's <code>BasicProcessingFilter</code>.
* <p/>
* This handler requires an Spring Security <code>AuthenticationManager</code> to operate. It can be set using the
* <code>authenticationManager</code> property. An Spring Security <code>UsernamePasswordAuthenticationToken</code> is
* created with the username as principal and password as credentials.
*
* @author Arjen Poutsma
* @see org.springframework.security.providers.UsernamePasswordAuthenticationToken
* @see org.springframework.security.ui.basicauth.BasicProcessingFilter
* @since 1.5.0
*/
public class SpringPlainTextPasswordValidationCallbackHandler extends AbstractWsPasswordCallbackHandler {
private AuthenticationManager authenticationManager;
private boolean ignoreFailure = false;
/** Sets the Spring Security authentication manager. Required. */
public void setAuthenticationManager(AuthenticationManager authenticationManager) {
this.authenticationManager = authenticationManager;
}
public void setIgnoreFailure(boolean ignoreFailure) {
this.ignoreFailure = ignoreFailure;
}
public void afterPropertiesSet() throws Exception {
Assert.notNull(authenticationManager, "authenticationManager is required");
}
protected void handleCleanup(CleanupCallback callback) throws IOException, UnsupportedCallbackException {
SecurityContextHolder.clearContext();
}
protected void handleUsernameTokenUnknown(WSPasswordCallback callback)
throws IOException, UnsupportedCallbackException {
String identifier = callback.getIdentifer();
try {
Authentication authResult = authenticationManager
.authenticate(new UsernamePasswordAuthenticationToken(identifier, 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 '" + identifier + "' failed: " + failed.toString());
}
SecurityContextHolder.clearContext();
if (!ignoreFailure) {
throw new WSSecurityException(WSSecurityException.FAILED_AUTHENTICATION);
}
}
}
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.ws.soap.security.xwss.callback.springsecurity;
package org.springframework.ws.soap.security.xwss.callback;
import java.io.IOException;
import java.security.cert.X509Certificate;
@@ -49,7 +49,7 @@ import org.springframework.ws.soap.security.callback.CleanupCallback;
* @see com.sun.xml.wss.impl.callback.CertificateValidationCallback
* @since 1.5.0
*/
public class SpringSecurityCertificateValidationCallbackHandler extends AbstractCallbackHandler {
public class SpringCertificateValidationCallbackHandler extends AbstractCallbackHandler {
private AuthenticationManager authenticationManager;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.ws.soap.security.xwss.callback.springsecurity;
package org.springframework.ws.soap.security.xwss.callback;
import java.io.IOException;
import javax.security.auth.callback.Callback;
@@ -34,7 +34,6 @@ import org.springframework.security.userdetails.UsernameNotFoundException;
import org.springframework.util.Assert;
import org.springframework.ws.soap.security.callback.AbstractCallbackHandler;
import org.springframework.ws.soap.security.callback.CleanupCallback;
import org.springframework.ws.soap.security.xwss.callback.DefaultTimestampValidator;
/**
* Callback handler that validates a password digest using an Spring Security <code>UserDetailsService</code>. Logic
@@ -53,7 +52,7 @@ import org.springframework.ws.soap.security.xwss.callback.DefaultTimestampValida
* @see org.springframework.security.ui.digestauth.DigestProcessingFilter
* @since 1.5.0
*/
public class SpringSecurityDigestPasswordValidationCallbackHandler extends AbstractCallbackHandler {
public class SpringDigestPasswordValidationCallbackHandler extends AbstractCallbackHandler {
private UserCache userCache = new NullUserCache();

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.ws.soap.security.xwss.callback.springsecurity;
package org.springframework.ws.soap.security.xwss.callback;
import java.io.IOException;
import javax.security.auth.callback.Callback;
@@ -49,7 +49,7 @@ import org.springframework.ws.soap.security.callback.CleanupCallback;
* @see org.springframework.security.ui.basicauth.BasicProcessingFilter
* @since 1.5.0
*/
public class SpringSecurityPlainTextPasswordValidationCallbackHandler extends AbstractCallbackHandler {
public class SpringPlainTextPasswordValidationCallbackHandler extends AbstractCallbackHandler {
private AuthenticationManager authenticationManager;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.ws.soap.security.xwss.callback.springsecurity;
package org.springframework.ws.soap.security.xwss.callback;
import java.io.IOException;
import javax.security.auth.callback.Callback;
@@ -37,7 +37,7 @@ import org.springframework.ws.soap.security.callback.AbstractCallbackHandler;
* @author Arjen Poutsma
* @since 1.5.0
*/
public class SpringSecurityUsernamePasswordCallbackHandler extends AbstractCallbackHandler {
public class SpringUsernamePasswordCallbackHandler extends AbstractCallbackHandler {
protected void handleInternal(Callback callback) throws IOException, UnsupportedCallbackException {
if (callback instanceof UsernameCallback) {

View File

@@ -1,6 +0,0 @@
<html>
<body>
Contains <code>CallbackHandler</code> implementations for XWSS that use
<a href="http://springframework.org/security">Spring Security</a>.
</body>
</html>

View File

@@ -16,8 +16,8 @@ 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.springsecurity.SpringSecurityDigestPasswordValidationCallbackHandler;
import org.springframework.ws.soap.security.wss4j.callback.springsecurity.SpringSecurityPlainTextPasswordValidationCallbackHandler;
import org.springframework.ws.soap.security.wss4j.callback.SpringDigestPasswordValidationCallbackHandler;
import org.springframework.ws.soap.security.wss4j.callback.SpringPlainTextPasswordValidationCallbackHandler;
public abstract class Wss4jMessageInterceptorSpringSecurityCallbackHandlerTestCase extends Wss4jTestCase {
@@ -82,8 +82,8 @@ public abstract class Wss4jMessageInterceptorSpringSecurityCallbackHandlerTestCa
interceptor.setSecurementActions(actions);
}
if (digest) {
SpringSecurityDigestPasswordValidationCallbackHandler callbackHandler =
new SpringSecurityDigestPasswordValidationCallbackHandler();
SpringDigestPasswordValidationCallbackHandler callbackHandler =
new SpringDigestPasswordValidationCallbackHandler();
InMemoryDaoImpl userDetailsService = new InMemoryDaoImpl();
userDetailsService.setUserProperties(users);
userDetailsService.afterPropertiesSet();
@@ -93,8 +93,8 @@ public abstract class Wss4jMessageInterceptorSpringSecurityCallbackHandlerTestCa
interceptor.afterPropertiesSet();
}
else {
SpringSecurityPlainTextPasswordValidationCallbackHandler callbackHandler =
new SpringSecurityPlainTextPasswordValidationCallbackHandler();
SpringPlainTextPasswordValidationCallbackHandler callbackHandler =
new SpringPlainTextPasswordValidationCallbackHandler();
Authentication authResult = new TestingAuthenticationToken("Bert", "Ernie", new GrantedAuthority[0]);
control.expectAndReturn(mock.authenticate(new UsernamePasswordAuthenticationToken("Bert", "Ernie")),
authResult);

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.ws.soap.security.xwss.callback.springsecurity;
package org.springframework.ws.soap.security.xwss.callback;
import java.io.InputStream;
import java.security.KeyStore;
@@ -33,9 +33,9 @@ import org.springframework.security.providers.TestingAuthenticationToken;
import org.springframework.security.providers.x509.X509AuthenticationToken;
import org.springframework.ws.soap.security.callback.CleanupCallback;
public class SpringSecurityCertificateValidationCallbackHandlerTest extends TestCase {
public class SpringCertificateValidationCallbackHandlerTest extends TestCase {
private SpringSecurityCertificateValidationCallbackHandler callbackHandler;
private SpringCertificateValidationCallbackHandler callbackHandler;
private MockControl control;
@@ -46,7 +46,7 @@ public class SpringSecurityCertificateValidationCallbackHandlerTest extends Test
private CertificateValidationCallback callback;
protected void setUp() throws Exception {
callbackHandler = new SpringSecurityCertificateValidationCallbackHandler();
callbackHandler = new SpringCertificateValidationCallbackHandler();
control = MockControl.createControl(AuthenticationManager.class);
mock = (AuthenticationManager) control.getMock();
callbackHandler.setAuthenticationManager(mock);

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.ws.soap.security.xwss.callback.springsecurity;
package org.springframework.ws.soap.security.xwss.callback;
import com.sun.xml.wss.impl.callback.PasswordValidationCallback;
import junit.framework.TestCase;
@@ -28,9 +28,9 @@ import org.springframework.security.userdetails.UserDetailsService;
import org.springframework.security.userdetails.UsernameNotFoundException;
import org.springframework.ws.soap.security.callback.CleanupCallback;
public class SpringSecurityDigestPasswordValidationCallbackHandlerTest extends TestCase {
public class SpringDigestPasswordValidationCallbackHandlerTest extends TestCase {
private SpringSecurityDigestPasswordValidationCallbackHandler callbackHandler;
private SpringDigestPasswordValidationCallbackHandler callbackHandler;
private MockControl control;
@@ -43,7 +43,7 @@ public class SpringSecurityDigestPasswordValidationCallbackHandlerTest extends T
private PasswordValidationCallback callback;
protected void setUp() throws Exception {
callbackHandler = new SpringSecurityDigestPasswordValidationCallbackHandler();
callbackHandler = new SpringDigestPasswordValidationCallbackHandler();
control = MockControl.createControl(UserDetailsService.class);
mock = (UserDetailsService) control.getMock();
callbackHandler.setUserDetailsService(mock);

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.ws.soap.security.xwss.callback.springsecurity;
package org.springframework.ws.soap.security.xwss.callback;
import com.sun.xml.wss.impl.callback.PasswordValidationCallback;
import junit.framework.TestCase;
@@ -29,9 +29,9 @@ import org.springframework.security.providers.TestingAuthenticationToken;
import org.springframework.security.providers.UsernamePasswordAuthenticationToken;
import org.springframework.ws.soap.security.callback.CleanupCallback;
public class SpringSecurityPlainTextPasswordValidationCallbackHandlerTest extends TestCase {
public class SpringPlainTextPasswordValidationCallbackHandlerTest extends TestCase {
private SpringSecurityPlainTextPasswordValidationCallbackHandler callbackHandler;
private SpringPlainTextPasswordValidationCallbackHandler callbackHandler;
private MockControl control;
@@ -44,7 +44,7 @@ public class SpringSecurityPlainTextPasswordValidationCallbackHandlerTest extend
private String password;
protected void setUp() throws Exception {
callbackHandler = new SpringSecurityPlainTextPasswordValidationCallbackHandler();
callbackHandler = new SpringPlainTextPasswordValidationCallbackHandler();
control = MockControl.createControl(AuthenticationManager.class);
mock = (AuthenticationManager) control.getMock();
callbackHandler.setAuthenticationManager(mock);

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.ws.soap.security.xwss.callback.springsecurity;
package org.springframework.ws.soap.security.xwss.callback;
import com.sun.xml.wss.impl.callback.PasswordCallback;
import com.sun.xml.wss.impl.callback.UsernameCallback;
@@ -24,12 +24,12 @@ import org.springframework.security.Authentication;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.security.providers.UsernamePasswordAuthenticationToken;
public class SpringSecurityUsernamePasswordCallbackHandlerTest extends TestCase {
public class SpringUsernamePasswordCallbackHandlerTest extends TestCase {
private SpringSecurityUsernamePasswordCallbackHandler handler;
private SpringUsernamePasswordCallbackHandler handler;
protected void setUp() throws Exception {
handler = new SpringSecurityUsernamePasswordCallbackHandler();
handler = new SpringUsernamePasswordCallbackHandler();
Authentication authentication = new UsernamePasswordAuthenticationToken("Bert", "Ernie");
SecurityContextHolder.getContext().setAuthentication(authentication);
}