From 09cc58d7ac06aefc1773795ad880bf7e48667f99 Mon Sep 17 00:00:00 2001 From: Luke Taylor Date: Thu, 30 Oct 2008 05:44:38 +0000 Subject: [PATCH] SEC-1009: removed additional container adapter specific code --- .../AbstractAdapterAuthenticationToken.java | 110 ------------------ .../security/adapters/AuthByAdapter.java | 45 ------- .../adapters/AuthByAdapterProvider.java | 85 -------------- .../HttpRequestIntegrationFilter.java | 93 --------------- .../PrincipalSpringSecurityUserToken.java | 64 ---------- .../security/adapters/package.html | 11 -- 6 files changed, 408 deletions(-) delete mode 100644 core/src/main/java/org/springframework/security/adapters/AbstractAdapterAuthenticationToken.java delete mode 100644 core/src/main/java/org/springframework/security/adapters/AuthByAdapter.java delete mode 100644 core/src/main/java/org/springframework/security/adapters/AuthByAdapterProvider.java delete mode 100644 core/src/main/java/org/springframework/security/adapters/HttpRequestIntegrationFilter.java delete mode 100644 core/src/main/java/org/springframework/security/adapters/PrincipalSpringSecurityUserToken.java delete mode 100644 core/src/main/java/org/springframework/security/adapters/package.html diff --git a/core/src/main/java/org/springframework/security/adapters/AbstractAdapterAuthenticationToken.java b/core/src/main/java/org/springframework/security/adapters/AbstractAdapterAuthenticationToken.java deleted file mode 100644 index 2dd0e6d21e..0000000000 --- a/core/src/main/java/org/springframework/security/adapters/AbstractAdapterAuthenticationToken.java +++ /dev/null @@ -1,110 +0,0 @@ -/* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited - * - * 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.security.adapters; - -import org.springframework.security.GrantedAuthority; - -import org.springframework.security.providers.AbstractAuthenticationToken; - - -/** - * Convenience superclass for {@link AuthByAdapter} implementations. - * - * @author Ben Alex - * @version $Id$ - */ -public abstract class AbstractAdapterAuthenticationToken extends AbstractAuthenticationToken implements AuthByAdapter { - //~ Instance fields ================================================================================================ - - private int keyHash; - - //~ Constructors =================================================================================================== - - protected AbstractAdapterAuthenticationToken() { - super(null); - } - -/** - * The only way an AbstractAdapterAuthentication should be - * constructed. - * - * @param key the key that is hashed and made available via {@link - * #getKeyHash()} - * @param authorities the authorities granted to this principal - */ - protected AbstractAdapterAuthenticationToken(String key, GrantedAuthority[] authorities) { - super(authorities); - this.keyHash = key.hashCode(); - } - - //~ Methods ======================================================================================================== - - public boolean equals(Object obj) { - if (obj instanceof AbstractAdapterAuthenticationToken) { - if (!super.equals(obj)) { - return false; - } - - AbstractAdapterAuthenticationToken test = (AbstractAdapterAuthenticationToken) obj; - - return (this.getKeyHash() == test.getKeyHash()); - } - - return false; - } - - public int getKeyHash() { - return this.keyHash; - } - - /** - * Always returns true. - * - * @return DOCUMENT ME! - */ - public boolean isAuthenticated() { - return true; - } - - /** - * Iterates the granted authorities and indicates whether or not the specified role is held.

Comparison - * is based on the String returned by {@link GrantedAuthority#getAuthority}.

- * - * @param role the role being searched for in this object's granted authorities list - * - * @return true if the granted authority is held, or false otherwise - */ - public boolean isUserInRole(String role) { - GrantedAuthority[] authorities = super.getAuthorities(); - - for (int i = 0; i < authorities.length; i++) { - if (role.equals(authorities[i].getAuthority())) { - return true; - } - } - - return false; - } - - /** - * Setting is ignored. Always considered authenticated. - * - * @param ignored DOCUMENT ME! - */ - public void setAuthenticated(boolean ignored) { - // ignored - } -} diff --git a/core/src/main/java/org/springframework/security/adapters/AuthByAdapter.java b/core/src/main/java/org/springframework/security/adapters/AuthByAdapter.java deleted file mode 100644 index 4b3f8c01e2..0000000000 --- a/core/src/main/java/org/springframework/security/adapters/AuthByAdapter.java +++ /dev/null @@ -1,45 +0,0 @@ -/* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited - * - * 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.security.adapters; - -import org.springframework.security.Authentication; - - -/** - * Indicates a specialized, immutable, server-side only {@link Authentication} - * class. - * - *

- * Automatically considered valid by the {@link AuthByAdapterProvider}, - * provided the hash code presented by the implementation objects matches that - * expected by the AuthByAdapterProvider. - *

- * - * @author Ben Alex - * @version $Id$ - */ -public interface AuthByAdapter extends Authentication { - //~ Methods ======================================================================================================== - - /** - * Returns the hash code of the key that was passed to the constructor of the AuthByAdapter - * implementation. The implementation should convert the value to a hash code at construction time, rather than - * storing the key itself. - * - * @return the hash code of the key used when the object was created. - */ - int getKeyHash(); -} diff --git a/core/src/main/java/org/springframework/security/adapters/AuthByAdapterProvider.java b/core/src/main/java/org/springframework/security/adapters/AuthByAdapterProvider.java deleted file mode 100644 index 29879bb78b..0000000000 --- a/core/src/main/java/org/springframework/security/adapters/AuthByAdapterProvider.java +++ /dev/null @@ -1,85 +0,0 @@ -/* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited - * - * 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.security.adapters; - -import org.springframework.security.SpringSecurityMessageSource; -import org.springframework.security.Authentication; -import org.springframework.security.AuthenticationException; -import org.springframework.security.BadCredentialsException; - -import org.springframework.security.providers.AuthenticationProvider; - -import org.springframework.beans.factory.InitializingBean; - -import org.springframework.context.MessageSource; -import org.springframework.context.MessageSourceAware; -import org.springframework.context.support.MessageSourceAccessor; - -import org.springframework.util.Assert; - - -/** - * An {@link AuthenticationProvider} implementation that can authenticate an {@link AuthByAdapter}.

Configured in - * the bean context with a key that should match the key used by adapters to generate AuthByAdapter - * instances. It treats as valid any such instance presenting a hash code that matches the - * AuthByAdapterProvider-configured key.

- *

If the key does not match, a BadCredentialsException is thrown.

- */ -public class AuthByAdapterProvider implements InitializingBean, AuthenticationProvider, MessageSourceAware { - //~ Instance fields ================================================================================================ - - protected MessageSourceAccessor messages = SpringSecurityMessageSource.getAccessor(); - private String key; - - //~ Methods ======================================================================================================== - - public void afterPropertiesSet() throws Exception { - Assert.notNull(key, "A Key is required and should match that configured for the adapters"); - Assert.notNull(messages, "A message source must be set"); - } - - public Authentication authenticate(Authentication authentication) - throws AuthenticationException { - AuthByAdapter token = (AuthByAdapter) authentication; - - if (token.getKeyHash() == key.hashCode()) { - return authentication; - } else { - throw new BadCredentialsException(messages.getMessage("AuthByAdapterProvider.incorrectKey", - "The presented AuthByAdapter implementation does not contain the expected key")); - } - } - - public String getKey() { - return key; - } - - public void setKey(String key) { - this.key = key; - } - - public void setMessageSource(MessageSource messageSource) { - this.messages = new MessageSourceAccessor(messageSource); - } - - public boolean supports(Class authentication) { - if (AuthByAdapter.class.isAssignableFrom(authentication)) { - return true; - } else { - return false; - } - } -} diff --git a/core/src/main/java/org/springframework/security/adapters/HttpRequestIntegrationFilter.java b/core/src/main/java/org/springframework/security/adapters/HttpRequestIntegrationFilter.java deleted file mode 100644 index 00b961c0cf..0000000000 --- a/core/src/main/java/org/springframework/security/adapters/HttpRequestIntegrationFilter.java +++ /dev/null @@ -1,93 +0,0 @@ -/* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited - * - * 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.security.adapters; - -import org.springframework.security.Authentication; - -import org.springframework.security.context.SecurityContextHolder; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -import java.io.IOException; - -import java.security.Principal; - -import javax.servlet.Filter; -import javax.servlet.FilterChain; -import javax.servlet.FilterConfig; -import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; -import javax.servlet.http.HttpServletRequest; - - -/** - * Populates SecurityContext with the Authentication obtained from the container's - * HttpServletRequest.getUserPrincipal().

Use this filter with container adapters only.

- *

This filter never preserves the Authentication on the SecurityContext - it - * is replaced every request.

- *

See {@link org.springframework.security.context.HttpSessionContextIntegrationFilter} for further information.

- * - * @author Ben Alex - * @version $Id$ - */ -public class HttpRequestIntegrationFilter implements Filter { - //~ Static fields/initializers ===================================================================================== - - private static final Log logger = LogFactory.getLog(HttpRequestIntegrationFilter.class); - - //~ Methods ======================================================================================================== - - /** - * Does nothing. We use IoC container lifecycle services instead. - */ - public void destroy() {} - - public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) - throws IOException, ServletException { - if (request instanceof HttpServletRequest) { - Principal principal = ((HttpServletRequest) request).getUserPrincipal(); - - if ((principal != null) && principal instanceof Authentication) { - SecurityContextHolder.getContext().setAuthentication((Authentication) principal); - - if (logger.isDebugEnabled()) { - logger.debug("SecurityContextHolder updated with Authentication from container: '" + principal - + "'"); - } - } else { - if (logger.isDebugEnabled()) { - logger.debug("SecurityContextHolder not set with new Authentication as Principal was: '" - + principal + "'"); - } - } - } else { - throw new IllegalArgumentException("Only HttpServletRequest is acceptable"); - } - - chain.doFilter(request, response); - } - - /** - * Does nothing. We use IoC container lifecycle services instead. - * - * @param arg0 ignored - * - * @throws ServletException ignored - */ - public void init(FilterConfig arg0) throws ServletException {} -} diff --git a/core/src/main/java/org/springframework/security/adapters/PrincipalSpringSecurityUserToken.java b/core/src/main/java/org/springframework/security/adapters/PrincipalSpringSecurityUserToken.java deleted file mode 100644 index 0b1af281e7..0000000000 --- a/core/src/main/java/org/springframework/security/adapters/PrincipalSpringSecurityUserToken.java +++ /dev/null @@ -1,64 +0,0 @@ -/* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited - * - * 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.security.adapters; - -import org.springframework.security.GrantedAuthority; - -import java.security.Principal; - - -/** - * A {@link Principal} compatible {@link org.springframework.security.Authentication} object. - * - * @author Ben Alex - * @version $Id$ - */ -public class PrincipalSpringSecurityUserToken extends AbstractAdapterAuthenticationToken implements Principal { - //~ Instance fields ================================================================================================ - - private static final long serialVersionUID = 1L; - private Object principal; - private String password; - private String username; - - //~ Constructors =================================================================================================== - - public PrincipalSpringSecurityUserToken(String key, String username, String password, GrantedAuthority[] authorities, - Object principal) { - super(key, authorities); - this.username = username; - this.password = password; - this.principal = principal; - } - - //~ Methods ======================================================================================================== - - public Object getCredentials() { - return this.password; - } - - public String getName() { - return this.username; - } - - public Object getPrincipal() { - if (this.principal == null) { - return this.username; - } - - return this.principal; - } -} diff --git a/core/src/main/java/org/springframework/security/adapters/package.html b/core/src/main/java/org/springframework/security/adapters/package.html deleted file mode 100644 index b2e14dfc56..0000000000 --- a/core/src/main/java/org/springframework/security/adapters/package.html +++ /dev/null @@ -1,11 +0,0 @@ - - -Allows external containers to obtain authentication information from the -system. - -

It is recommended to use the org.springframework.security.ui.webapp -package for standard web applications, as it has much lower configuration -complexity.

- - -