1
2
3
4
5
6
7
8
9
10
11
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
26
27
28
29
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
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 }