View Javadoc
1   /*
2    * Copyright 2013-2014 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  package org.springframework.security.oauth2.config.annotation.web.configuration;
15  
16  import java.util.Map;
17  
18  import javax.annotation.Resource;
19  
20  import org.springframework.beans.factory.annotation.Qualifier;
21  import org.springframework.beans.factory.annotation.Value;
22  import org.springframework.context.annotation.Bean;
23  import org.springframework.context.annotation.Configuration;
24  import org.springframework.context.annotation.Scope;
25  import org.springframework.context.annotation.ScopedProxyMode;
26  import org.springframework.security.oauth2.client.DefaultOAuth2ClientContext;
27  import org.springframework.security.oauth2.client.OAuth2ClientContext;
28  import org.springframework.security.oauth2.client.filter.OAuth2ClientContextFilter;
29  import org.springframework.security.oauth2.client.token.AccessTokenRequest;
30  import org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest;
31  
32  /**
33   * @author Dave Syer
34   * 
35   */
36  @Configuration
37  public class OAuth2ClientConfiguration {
38  
39  	@Bean
40  	public OAuth2ClientContextFilter oauth2ClientContextFilter() {
41  		OAuth2ClientContextFilter filter = new OAuth2ClientContextFilter();
42  		return filter;
43  	}
44  
45  	@Bean
46  	@Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
47  	protected AccessTokenRequest accessTokenRequest(@Value("#{request.parameterMap}")
48  	Map<String, String[]> parameters, @Value("#{request.getAttribute('currentUri')}")
49  	String currentUri) {
50  		DefaultAccessTokenRequest request = new DefaultAccessTokenRequest(parameters);
51  		request.setCurrentUri(currentUri);
52  		return request;
53  	}
54  	
55  	@Configuration
56  	protected static class OAuth2ClientContextConfiguration {
57  		
58  		@Resource
59  		@Qualifier("accessTokenRequest")
60  		private AccessTokenRequest accessTokenRequest;
61  		
62  		@Bean
63  		@Scope(value = "session", proxyMode = ScopedProxyMode.INTERFACES)
64  		public OAuth2ClientContext oauth2ClientContext() {
65  			return new DefaultOAuth2ClientContext(accessTokenRequest);
66  		}
67  		
68  	}
69  
70  }