View Javadoc
1   /*
2    * Copyright 2006-2011 the original author or authors.
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  
15  package org.springframework.security.oauth2.config.xml;
16  
17  import org.springframework.beans.factory.FactoryBean;
18  import org.springframework.security.oauth2.client.OAuth2ClientContext;
19  import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails;
20  import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails;
21  
22  /**
23   * Convenience factory for OAuth2ClientContext that is aware of the need for a different context if the resource is for a
24   * client credentials grant. Client credentials grants will always have the same credentials for all requests, so
25   * there's no point protecting the context with session and request scopes.
26   * 
27   * @author Dave Syer
28   * 
29   */
30  public class OAuth2ClientContextFactoryBean implements FactoryBean<OAuth2ClientContext> {
31  
32  	private OAuth2ProtectedResourceDetails resource;
33  
34  	private OAuth2ClientContext bareContext;
35  
36  	private OAuth2ClientContext scopedContext;
37  	
38  	/**
39  	 * @param resource the resource to set
40  	 */
41  	public void setResource(OAuth2ProtectedResourceDetails resource) {
42  		this.resource = resource;
43  	}
44  
45  	/**
46  	 * @param scopedContext the scopedContext to set
47  	 */
48  	public void setScopedContext(OAuth2ClientContext scopedContext) {
49  		this.scopedContext = scopedContext;
50  	}
51  
52  	/**
53  	 * @param bareContext the bareContext to set
54  	 */
55  	public void setBareContext(OAuth2ClientContext bareContext) {
56  		this.bareContext = bareContext;
57  	}
58  
59  	public OAuth2ClientContext getObject() throws Exception {
60  		if (resource instanceof ClientCredentialsResourceDetails) {
61  			return bareContext;
62  		}
63  		return scopedContext;
64  	}
65  
66  	public Class<?> getObjectType() {
67  		return OAuth2ClientContext.class;
68  	}
69  
70  	public boolean isSingleton() {
71  		return true;
72  	}
73  
74  }