|
15 | 15 | */
|
16 | 16 | package org.springframework.boot.autoconfigure.security.oauth2.client;
|
17 | 17 |
|
18 |
| -import java.io.IOException; |
19 |
| -import java.util.Arrays; |
20 |
| - |
21 | 18 | import javax.annotation.PostConstruct;
|
22 | 19 | import javax.annotation.Resource;
|
23 | 20 |
|
|
27 | 24 | import org.springframework.beans.factory.annotation.Qualifier;
|
28 | 25 | import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
29 | 26 | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
| 27 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; |
| 28 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
| 29 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnNotWebApplication; |
| 30 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; |
30 | 31 | import org.springframework.boot.autoconfigure.security.oauth2.ClientCredentialsProperties;
|
31 | 32 | import org.springframework.boot.context.embedded.FilterRegistrationBean;
|
32 | 33 | import org.springframework.boot.context.properties.ConfigurationProperties;
|
|
35 | 36 | import org.springframework.context.annotation.Primary;
|
36 | 37 | import org.springframework.context.annotation.Scope;
|
37 | 38 | import org.springframework.context.annotation.ScopedProxyMode;
|
38 |
| -import org.springframework.http.HttpHeaders; |
39 |
| -import org.springframework.http.HttpRequest; |
40 |
| -import org.springframework.http.MediaType; |
41 |
| -import org.springframework.http.client.ClientHttpRequestExecution; |
42 |
| -import org.springframework.http.client.ClientHttpRequestInterceptor; |
43 |
| -import org.springframework.http.client.ClientHttpResponse; |
| 39 | +import org.springframework.security.core.Authentication; |
| 40 | +import org.springframework.security.core.context.SecurityContextHolder; |
44 | 41 | import org.springframework.security.oauth2.client.DefaultOAuth2ClientContext;
|
45 | 42 | import org.springframework.security.oauth2.client.OAuth2ClientContext;
|
46 |
| -import org.springframework.security.oauth2.client.OAuth2RestOperations; |
47 | 43 | import org.springframework.security.oauth2.client.OAuth2RestTemplate;
|
48 | 44 | import org.springframework.security.oauth2.client.filter.OAuth2ClientContextFilter;
|
49 | 45 | import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails;
|
50 | 46 | import org.springframework.security.oauth2.client.token.AccessTokenRequest;
|
51 |
| -import org.springframework.security.oauth2.client.token.RequestEnhancer; |
52 |
| -import org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeAccessTokenProvider; |
| 47 | +import org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest; |
| 48 | +import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails; |
53 | 49 | import org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails;
|
| 50 | +import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken; |
54 | 51 | import org.springframework.security.oauth2.config.annotation.web.configuration.EnableOAuth2Client;
|
55 | 52 | import org.springframework.security.oauth2.config.annotation.web.configuration.OAuth2ClientConfiguration;
|
56 |
| -import org.springframework.util.MultiValueMap; |
| 53 | +import org.springframework.security.oauth2.provider.OAuth2Authentication; |
| 54 | +import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails; |
57 | 55 |
|
58 | 56 | /**
|
59 | 57 | * @author Dave Syer
|
60 | 58 | *
|
61 | 59 | */
|
62 | 60 | @Configuration
|
63 | 61 | @ConditionalOnClass(EnableOAuth2Client.class)
|
64 |
| -@ConditionalOnBean(OAuth2ClientConfiguration.class) |
| 62 | +@ConditionalOnExpression("'${spring.oauth2.client.clientId:}'!=''") |
65 | 63 | public class SpringSecurityOAuth2ClientConfiguration {
|
66 | 64 |
|
67 | 65 | private static final Log logger = LogFactory
|
68 | 66 | .getLog(SpringSecurityOAuth2ClientConfiguration.class);
|
69 | 67 |
|
| 68 | + @Autowired |
| 69 | + private ClientCredentialsProperties credentials; |
| 70 | + |
| 71 | + @PostConstruct |
| 72 | + public void init() { |
| 73 | + String prefix = "spring.oauth2.client"; |
| 74 | + boolean defaultSecret = this.credentials.isDefaultSecret(); |
| 75 | + logger.info(String.format( |
| 76 | + "Initialized OAuth2 Client\n\n%s.clientId = %s\n%s.secret = %s\n\n", |
| 77 | + prefix, this.credentials.getClientId(), prefix, |
| 78 | + defaultSecret ? this.credentials.getClientSecret() : "****")); |
| 79 | + } |
| 80 | + |
| 81 | + @Bean |
| 82 | + @Primary |
| 83 | + public OAuth2RestTemplate oauth2RestTemplate(OAuth2ClientContext oauth2ClientContext, |
| 84 | + OAuth2ProtectedResourceDetails details) { |
| 85 | + OAuth2RestTemplate template = new OAuth2RestTemplate(details, oauth2ClientContext); |
| 86 | + return template; |
| 87 | + } |
| 88 | + |
70 | 89 | @Configuration
|
71 |
| - public static class ClientAuthenticationFilterConfiguration { |
| 90 | + protected abstract static class BaseConfiguration { |
72 | 91 |
|
73 |
| - @Resource |
74 |
| - @Qualifier("accessTokenRequest") |
75 |
| - private AccessTokenRequest accessTokenRequest; |
76 |
| - |
77 |
| - @Autowired |
78 |
| - private ClientCredentialsProperties credentials; |
79 |
| - |
80 |
| - @PostConstruct |
81 |
| - public void init() { |
82 |
| - String prefix = "spring.oauth2.client"; |
83 |
| - boolean defaultSecret = this.credentials.isDefaultSecret(); |
84 |
| - logger.info(String.format( |
85 |
| - "Initialized OAuth2 Client\n\n%s.clientId = %s\n%s.secret = %s\n\n", |
86 |
| - prefix, this.credentials.getClientId(), prefix, |
87 |
| - defaultSecret ? this.credentials.getClientSecret() : "****")); |
| 92 | + @Bean |
| 93 | + @ConfigurationProperties("spring.oauth2.client") |
| 94 | + @Primary |
| 95 | + public AuthorizationCodeResourceDetails oauth2RemoteResource() { |
| 96 | + AuthorizationCodeResourceDetails details = new AuthorizationCodeResourceDetails(); |
| 97 | + return details; |
88 | 98 | }
|
89 | 99 |
|
| 100 | + } |
| 101 | + |
| 102 | + @Configuration |
| 103 | + @ConditionalOnNotWebApplication |
| 104 | + protected static class SingletonScopedConfiguration { |
| 105 | + |
90 | 106 | @Bean
|
91 | 107 | @ConfigurationProperties("spring.oauth2.client")
|
92 | 108 | @Primary
|
93 |
| - public AuthorizationCodeResourceDetails authorizationCodeResourceDetails() { |
94 |
| - AuthorizationCodeResourceDetails details = new AuthorizationCodeResourceDetails(); |
95 |
| - details.setClientSecret(this.credentials.getClientSecret()); |
96 |
| - details.setClientId(this.credentials.getClientId()); |
| 109 | + public ClientCredentialsResourceDetails oauth2RemoteResource() { |
| 110 | + ClientCredentialsResourceDetails details = new ClientCredentialsResourceDetails(); |
97 | 111 | return details;
|
98 | 112 | }
|
99 | 113 |
|
| 114 | + @Bean |
| 115 | + public OAuth2ClientContext oauth2ClientContext() { |
| 116 | + return new DefaultOAuth2ClientContext(new DefaultAccessTokenRequest()); |
| 117 | + } |
| 118 | + |
| 119 | + } |
| 120 | + |
| 121 | + @Configuration |
| 122 | + @ConditionalOnBean(OAuth2ClientConfiguration.class) |
| 123 | + @ConditionalOnWebApplication |
| 124 | + protected static class SessionScopedConfiguration extends BaseConfiguration { |
| 125 | + |
| 126 | + @Resource |
| 127 | + @Qualifier("accessTokenRequest") |
| 128 | + protected AccessTokenRequest accessTokenRequest; |
| 129 | + |
| 130 | + @Bean |
| 131 | + @Scope(value = "session", proxyMode = ScopedProxyMode.INTERFACES) |
| 132 | + public OAuth2ClientContext oauth2ClientContext() { |
| 133 | + return new DefaultOAuth2ClientContext(accessTokenRequest); |
| 134 | + } |
| 135 | + |
100 | 136 | @Bean
|
101 | 137 | public FilterRegistrationBean oauth2ClientFilterRegistration(
|
102 | 138 | OAuth2ClientContextFilter filter) {
|
103 | 139 | FilterRegistrationBean registration = new FilterRegistrationBean();
|
104 | 140 | registration.setFilter(filter);
|
105 |
| - registration.setOrder(0); |
| 141 | + registration.setOrder(-100); |
106 | 142 | return registration;
|
107 | 143 | }
|
108 | 144 |
|
109 |
| - @Bean |
110 |
| - public OAuth2RestOperations authorizationCodeRestTemplate( |
111 |
| - AuthorizationCodeResourceDetails oauth2RemoteResource) { |
112 |
| - OAuth2RestTemplate template = new OAuth2RestTemplate(oauth2RemoteResource, |
113 |
| - oauth2ClientContext()); |
114 |
| - template.setInterceptors(Arrays |
115 |
| - .<ClientHttpRequestInterceptor> asList(new ClientHttpRequestInterceptor() { |
116 |
| - @Override |
117 |
| - public ClientHttpResponse intercept(HttpRequest request, |
118 |
| - byte[] body, ClientHttpRequestExecution execution) |
119 |
| - throws IOException { |
120 |
| - request.getHeaders().setAccept( |
121 |
| - Arrays.asList(MediaType.APPLICATION_JSON)); |
122 |
| - return execution.execute(request, body); |
123 |
| - } |
124 |
| - })); |
125 |
| - AuthorizationCodeAccessTokenProvider accessTokenProvider = new AuthorizationCodeAccessTokenProvider(); |
126 |
| - accessTokenProvider.setTokenRequestEnhancer(new RequestEnhancer() { |
127 |
| - @Override |
128 |
| - public void enhance(AccessTokenRequest request, |
129 |
| - OAuth2ProtectedResourceDetails resource, |
130 |
| - MultiValueMap<String, String> form, HttpHeaders headers) { |
131 |
| - headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); |
132 |
| - } |
133 |
| - }); |
134 |
| - template.setAccessTokenProvider(accessTokenProvider); |
135 |
| - return template; |
136 |
| - } |
| 145 | + } |
| 146 | + |
| 147 | + /* |
| 148 | + * When the authentication is per cookie but the stored token is an oauth2 one, we can |
| 149 | + * pass that on to a client that wants to call downstream. We don't even need an |
| 150 | + * OAuth2ClientContextFilter until we need to refresh the access token. To handle |
| 151 | + * refresh tokens you need to <code>@EnableOAuth2Client</code> |
| 152 | + */ |
| 153 | + @Configuration |
| 154 | + @ConditionalOnMissingBean(OAuth2ClientConfiguration.class) |
| 155 | + @ConditionalOnWebApplication |
| 156 | + protected static class RequestScopedConfiguration extends BaseConfiguration { |
137 | 157 |
|
138 | 158 | @Bean
|
139 | 159 | @Scope(value = "session", proxyMode = ScopedProxyMode.INTERFACES)
|
140 | 160 | public OAuth2ClientContext oauth2ClientContext() {
|
141 |
| - return new DefaultOAuth2ClientContext(this.accessTokenRequest); |
| 161 | + DefaultOAuth2ClientContext context = new DefaultOAuth2ClientContext( |
| 162 | + new DefaultAccessTokenRequest()); |
| 163 | + Authentication principal = SecurityContextHolder.getContext() |
| 164 | + .getAuthentication(); |
| 165 | + if (principal instanceof OAuth2Authentication) { |
| 166 | + OAuth2Authentication authentication = (OAuth2Authentication) principal; |
| 167 | + Object details = authentication.getDetails(); |
| 168 | + if (details instanceof OAuth2AuthenticationDetails) { |
| 169 | + OAuth2AuthenticationDetails oauthsDetails = (OAuth2AuthenticationDetails) details; |
| 170 | + String token = oauthsDetails.getTokenValue(); |
| 171 | + context.setAccessToken(new DefaultOAuth2AccessToken(token)); |
| 172 | + } |
| 173 | + } |
| 174 | + return context; |
142 | 175 | }
|
143 | 176 |
|
144 | 177 | }
|
|
0 commit comments