View Javadoc
1   /*
2    * Copyright 2008-2009 Web Cohesion
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5    * the License. You may obtain a copy of the License at
6    * 
7    * https://www.apache.org/licenses/LICENSE-2.0
8    * 
9    * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10   * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11   * specific language governing permissions and limitations under the License.
12   */
13  
14  package org.springframework.security.oauth2.config.xml;
15  
16  import org.springframework.beans.factory.support.AbstractBeanDefinition;
17  import org.springframework.beans.factory.support.BeanDefinitionBuilder;
18  import org.springframework.beans.factory.xml.ParserContext;
19  import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationManager;
20  import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationProcessingFilter;
21  import org.springframework.util.StringUtils;
22  import org.w3c.dom.Element;
23  
24  /**
25   * Parser for the OAuth "resource-server" element. Creates a filter that can be added to the standard Spring Security
26   * filter chain.
27   * 
28   * @author Ryan Heaton
29   * @author Dave Syer
30   */
31  public class ResourceServerBeanDefinitionParser extends ProviderBeanDefinitionParser {
32  
33  	@Override
34  	protected AbstractBeanDefinition parseEndpointAndReturnFilter(Element element, ParserContext parserContext,
35  			String tokenServicesRef, String serializerRef) {
36  
37  		String resourceId = element.getAttribute("resource-id");
38  		String entryPointRef = element.getAttribute("entry-point-ref");
39  		String authenticationManagerRef = element.getAttribute("authentication-manager-ref");
40  		String tokenExtractorRef = element.getAttribute("token-extractor-ref");
41  		String entryAuthDetailsSource = element.getAttribute("auth-details-source-ref");
42  		String stateless = element.getAttribute("stateless");
43  
44  		// configure the protected resource filter
45  		BeanDefinitionBuilder protectedResourceFilterBean = BeanDefinitionBuilder
46  				.rootBeanDefinition(OAuth2AuthenticationProcessingFilter.class);
47  
48  		if (StringUtils.hasText(authenticationManagerRef)) {
49  			protectedResourceFilterBean.addPropertyReference("authenticationManager", authenticationManagerRef);
50  		}
51  		else {
52  
53  			BeanDefinitionBuilder authenticationManagerBean = BeanDefinitionBuilder
54  					.rootBeanDefinition(OAuth2AuthenticationManager.class);
55  			
56  			authenticationManagerBean.addPropertyReference("tokenServices", tokenServicesRef);
57  
58  			if (StringUtils.hasText(resourceId)) {
59  				authenticationManagerBean.addPropertyValue("resourceId", resourceId);
60  			}
61  
62  			protectedResourceFilterBean.addPropertyValue("authenticationManager",
63  					authenticationManagerBean.getBeanDefinition());
64  
65  		}
66  
67  		if (StringUtils.hasText(entryPointRef)) {
68  			protectedResourceFilterBean.addPropertyReference("authenticationEntryPoint", entryPointRef);
69  		}
70  
71  		if (StringUtils.hasText(entryAuthDetailsSource)) {
72  			protectedResourceFilterBean.addPropertyReference("authenticationDetailsSource", entryAuthDetailsSource);
73  		}
74  
75  		if (StringUtils.hasText(tokenExtractorRef)) {
76  			protectedResourceFilterBean.addPropertyReference("tokenExtractor", tokenExtractorRef);
77  		}
78  
79  		if (StringUtils.hasText(stateless)) {
80  			protectedResourceFilterBean.addPropertyValue("stateless", stateless);
81  		}
82  
83  		return protectedResourceFilterBean.getBeanDefinition();
84  
85  	}
86  
87  }