Author: ningjiang Date: Thu May 27 02:10:51 2010 New Revision: 948653 URL: http://svn.apache.org/viewvc?rev=948653&view=rev Log: CAMEL-2756 Introduced plugable AuthenticationConverter into camel-spring-security
Added: camel/trunk/components/camel-spring-security/src/main/java/org/apache/camel/component/spring/security/converter/AuthenticationConverter.java (with props) camel/trunk/components/camel-spring-security/src/main/java/org/apache/camel/component/spring/security/converter/DefaultAuthenticationConverter.java (with props) Removed: camel/trunk/components/camel-spring-security/src/main/java/org/apache/camel/component/spring/security/converter/SprintSecurityConverter.java camel/trunk/components/camel-spring-security/src/main/resources/META-INF/services/ Modified: camel/trunk/components/camel-spring-security/src/main/java/org/apache/camel/component/spring/security/SpringSecurityAuthorizationPolicy.java camel/trunk/components/camel-spring-security/src/main/java/org/apache/camel/component/spring/security/config/SpringSecurityAuthorizationPolicyParser.java camel/trunk/components/camel-spring-security/src/main/resources/schema/camel-spring-security.xsd Modified: camel/trunk/components/camel-spring-security/src/main/java/org/apache/camel/component/spring/security/SpringSecurityAuthorizationPolicy.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring-security/src/main/java/org/apache/camel/component/spring/security/SpringSecurityAuthorizationPolicy.java?rev=948653&r1=948652&r2=948653&view=diff ============================================================================== --- camel/trunk/components/camel-spring-security/src/main/java/org/apache/camel/component/spring/security/SpringSecurityAuthorizationPolicy.java (original) +++ camel/trunk/components/camel-spring-security/src/main/java/org/apache/camel/component/spring/security/SpringSecurityAuthorizationPolicy.java Thu May 27 02:10:51 2010 @@ -16,10 +16,14 @@ */ package org.apache.camel.component.spring.security; +import javax.security.auth.Subject; + import org.apache.camel.CamelAuthorizationException; import org.apache.camel.Exchange; import org.apache.camel.Message; import org.apache.camel.Processor; +import org.apache.camel.component.spring.security.converter.AuthenticationConverter; +import org.apache.camel.component.spring.security.converter.DefaultAuthenticationConverter; import org.apache.camel.processor.DelegateProcessor; import org.apache.camel.spi.AuthorizationPolicy; import org.apache.camel.spi.RouteContext; @@ -44,6 +48,7 @@ public class SpringSecurityAuthorization private static final transient Log LOG = LogFactory.getLog(SpringSecurityAuthorizationPolicy.class); private AccessDecisionManager accessDecisionManager; private AuthenticationManager authenticationManager; + private AuthenticationConverter authenticationConverter; private ApplicationEventPublisher eventPublisher; private SpringSecurityAccessPolicy accessPolicy; @@ -88,7 +93,11 @@ public class SpringSecurityAuthorization } protected Authentication getAuthentication(Message message) { - Authentication answer = message.getHeader(Exchange.AUTHENTICATION, Authentication.class); + Subject subject = message.getHeader(Exchange.AUTHENTICATION, Subject.class); + Authentication answer = null; + if (subject != null) { + answer = getAuthenticationConverter().toAuthentication(subject); + } // try to get it from thread context as a fallback if (answer == null && useThreadSecurityContext) { answer = SecurityContextHolder.getContext().getAuthentication(); @@ -130,6 +139,8 @@ public class SpringSecurityAuthorization authentication = authenticationManager.authenticate(authentication); + System.out.println("The authenitcation is " + authentication); + if (LOG.isDebugEnabled()) { LOG.debug("Successfully Authenticated: " + authentication); } @@ -142,6 +153,23 @@ public class SpringSecurityAuthorization this.eventPublisher.publishEvent(event); } } + + public AuthenticationConverter getAuthenticationConverter() { + if (authenticationConverter == null) { + synchronized (this) { + if (authenticationConverter != null) { + return authenticationConverter; + } else { + authenticationConverter = new DefaultAuthenticationConverter(); + } + } + } + return authenticationConverter; + } + + public void setAuthenticationConverter(AuthenticationConverter converter) { + this.authenticationConverter = converter; + } public AccessDecisionManager getAccessDecisionManager() { return accessDecisionManager; Modified: camel/trunk/components/camel-spring-security/src/main/java/org/apache/camel/component/spring/security/config/SpringSecurityAuthorizationPolicyParser.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring-security/src/main/java/org/apache/camel/component/spring/security/config/SpringSecurityAuthorizationPolicyParser.java?rev=948653&r1=948652&r2=948653&view=diff ============================================================================== --- camel/trunk/components/camel-spring-security/src/main/java/org/apache/camel/component/spring/security/config/SpringSecurityAuthorizationPolicyParser.java (original) +++ camel/trunk/components/camel-spring-security/src/main/java/org/apache/camel/component/spring/security/config/SpringSecurityAuthorizationPolicyParser.java Thu May 27 02:10:51 2010 @@ -45,6 +45,7 @@ public class SpringSecurityAuthorization setReferenceIfAttributeDefine(builder, element, "accessDecisionManager"); setReferenceIfAttributeDefine(builder, element, "authenticationManager"); + setReferenceIfAttributeDefine(builder, element, "authenticationConverter"); BeanDefinitionBuilder accessPolicyBuilder = BeanDefinitionBuilder.genericBeanDefinition( SpringSecurityAccessPolicy.class.getCanonicalName()); Added: camel/trunk/components/camel-spring-security/src/main/java/org/apache/camel/component/spring/security/converter/AuthenticationConverter.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring-security/src/main/java/org/apache/camel/component/spring/security/converter/AuthenticationConverter.java?rev=948653&view=auto ============================================================================== --- camel/trunk/components/camel-spring-security/src/main/java/org/apache/camel/component/spring/security/converter/AuthenticationConverter.java (added) +++ camel/trunk/components/camel-spring-security/src/main/java/org/apache/camel/component/spring/security/converter/AuthenticationConverter.java Thu May 27 02:10:51 2010 @@ -0,0 +1,27 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.camel.component.spring.security.converter; + +import javax.security.auth.Subject; + +import org.springframework.security.Authentication; + +public interface AuthenticationConverter { + + Authentication toAuthentication(Subject subject); + +} Propchange: camel/trunk/components/camel-spring-security/src/main/java/org/apache/camel/component/spring/security/converter/AuthenticationConverter.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/components/camel-spring-security/src/main/java/org/apache/camel/component/spring/security/converter/AuthenticationConverter.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Added: camel/trunk/components/camel-spring-security/src/main/java/org/apache/camel/component/spring/security/converter/DefaultAuthenticationConverter.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring-security/src/main/java/org/apache/camel/component/spring/security/converter/DefaultAuthenticationConverter.java?rev=948653&view=auto ============================================================================== --- camel/trunk/components/camel-spring-security/src/main/java/org/apache/camel/component/spring/security/converter/DefaultAuthenticationConverter.java (added) +++ camel/trunk/components/camel-spring-security/src/main/java/org/apache/camel/component/spring/security/converter/DefaultAuthenticationConverter.java Thu May 27 02:10:51 2010 @@ -0,0 +1,47 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.camel.component.spring.security.converter; + +import java.util.Set; + +import javax.security.auth.Subject; + +import org.springframework.security.Authentication; + +public class DefaultAuthenticationConverter implements AuthenticationConverter { + + public Authentication toAuthentication(Subject subject) { + if (subject == null || subject.getPrincipals().size() == 0) { + return null; + } + Set<Authentication> authentications = subject.getPrincipals(Authentication.class); + if (authentications.size() > 0) { + // just return the first one + return authentications.iterator().next(); + } else { + return convertToAuthentication(subject); + } + } + + /** + * You can add the customer convert code here + */ + protected Authentication convertToAuthentication(Subject subject) { + return null; + } + +} Propchange: camel/trunk/components/camel-spring-security/src/main/java/org/apache/camel/component/spring/security/converter/DefaultAuthenticationConverter.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/components/camel-spring-security/src/main/java/org/apache/camel/component/spring/security/converter/DefaultAuthenticationConverter.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Modified: camel/trunk/components/camel-spring-security/src/main/resources/schema/camel-spring-security.xsd URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring-security/src/main/resources/schema/camel-spring-security.xsd?rev=948653&r1=948652&r2=948653&view=diff ============================================================================== --- camel/trunk/components/camel-spring-security/src/main/resources/schema/camel-spring-security.xsd (original) +++ camel/trunk/components/camel-spring-security/src/main/resources/schema/camel-spring-security.xsd Thu May 27 02:10:51 2010 @@ -32,9 +32,10 @@ <xsd:element name="authorizationPolicy"> <xsd:complexType> <xsd:attribute name="id" type="xsd:ID" use="required" /> - <xsd:attribute name="access" type="xsd:string" /> - <xsd:attribute name="authenticationManager" type="xsd:string" /> + <xsd:attribute name="access" type="xsd:string" /> <xsd:attribute name="accessDecisionManager" type="xsd:string" /> + <xsd:attribute name="authenticationConverter" type="xsd:string" /> + <xsd:attribute name="authenticationManager" type="xsd:string" /> <xsd:attribute name="useThreadSecurityContext" type="xsd:boolean" default="true"/> <xsd:attribute name="alwaysReauthenticate" type="xsd:boolean" default="false"/> </xsd:complexType>