Moved Spring Security handlers to default callback packag, getting rid of the (ugly) springsecurity package.
This commit is contained in:
@@ -1,110 +0,0 @@
|
||||
/*
|
||||
* 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.springsecurity;
|
||||
|
||||
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;
|
||||
import org.springframework.ws.soap.security.wss4j.callback.AbstractWsPasswordCallbackHandler;
|
||||
import org.springframework.ws.soap.security.wss4j.callback.UsernameTokenPrincipalCallback;
|
||||
|
||||
/**
|
||||
* 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 SpringSecurityDigestPasswordValidationCallbackHandler 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;
|
||||
}
|
||||
}
|
||||
@@ -1,91 +0,0 @@
|
||||
/*
|
||||
* 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.springsecurity;
|
||||
|
||||
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;
|
||||
import org.springframework.ws.soap.security.wss4j.callback.AbstractWsPasswordCallbackHandler;
|
||||
|
||||
/**
|
||||
* 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 SpringSecurityPlainTextPasswordValidationCallbackHandler 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user