Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
dc9ff738
Commit
dc9ff738
authored
Feb 12, 2017
by
Eddú Meléndez
Committed by
Andy Wilkinson
Mar 03, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Enable customization of RestTemplate that retrieves JwtAccessTokenConverter's key
Closes gh-8268 See gh-5859
parent
1d9520b3
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
83 additions
and
4 deletions
+83
-4
JwtAccessTokenConverterRestTemplateCustomizer.java
...source/JwtAccessTokenConverterRestTemplateCustomizer.java
+35
-0
ResourceServerTokenServicesConfiguration.java
...h2/resource/ResourceServerTokenServicesConfiguration.java
+11
-4
ResourceServerTokenServicesConfigurationTests.java
...source/ResourceServerTokenServicesConfigurationTests.java
+37
-0
No files found.
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/resource/JwtAccessTokenConverterRestTemplateCustomizer.java
0 → 100644
View file @
dc9ff738
/*
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org
.
springframework
.
boot
.
autoconfigure
.
security
.
oauth2
.
resource
;
import
org.springframework.web.client.RestTemplate
;
/**
* Callback for customizing the rest template used to fetch the token key.
*
* @author Eddú Meléndez
* @since 1.5.2
*/
public
interface
JwtAccessTokenConverterRestTemplateCustomizer
{
/**
* Customize the rest template before it is initialized.
* @param template the rest template
*/
void
customize
(
RestTemplate
template
);
}
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/resource/ResourceServerTokenServicesConfiguration.java
View file @
dc9ff738
...
@@ -76,6 +76,7 @@ import org.springframework.web.client.RestTemplate;
...
@@ -76,6 +76,7 @@ import org.springframework.web.client.RestTemplate;
*
*
* @author Dave Syer
* @author Dave Syer
* @author Madhura Bhave
* @author Madhura Bhave
* @author Eddú Meléndez
* @since 1.3.0
* @since 1.3.0
*/
*/
@Configuration
@Configuration
...
@@ -245,16 +246,18 @@ public class ResourceServerTokenServicesConfiguration {
...
@@ -245,16 +246,18 @@ public class ResourceServerTokenServicesConfiguration {
@Conditional
(
JwtTokenCondition
.
class
)
@Conditional
(
JwtTokenCondition
.
class
)
protected
static
class
JwtTokenServicesConfiguration
{
protected
static
class
JwtTokenServicesConfiguration
{
private
RestTemplate
keyUriRestTemplate
=
new
RestTemplate
();
private
final
ResourceServerProperties
resource
;
private
final
ResourceServerProperties
resource
;
private
final
List
<
JwtAccessTokenConverterConfigurer
>
configurers
;
private
final
List
<
JwtAccessTokenConverterConfigurer
>
configurers
;
private
final
List
<
JwtAccessTokenConverterRestTemplateCustomizer
>
customizers
;
public
JwtTokenServicesConfiguration
(
ResourceServerProperties
resource
,
public
JwtTokenServicesConfiguration
(
ResourceServerProperties
resource
,
ObjectProvider
<
List
<
JwtAccessTokenConverterConfigurer
>>
configurers
)
{
ObjectProvider
<
List
<
JwtAccessTokenConverterConfigurer
>>
configurers
,
ObjectProvider
<
List
<
JwtAccessTokenConverterRestTemplateCustomizer
>>
customizers
)
{
this
.
resource
=
resource
;
this
.
resource
=
resource
;
this
.
configurers
=
configurers
.
getIfAvailable
();
this
.
configurers
=
configurers
.
getIfAvailable
();
this
.
customizers
=
customizers
.
getIfAvailable
();
}
}
@Bean
@Bean
...
@@ -299,6 +302,10 @@ public class ResourceServerTokenServicesConfiguration {
...
@@ -299,6 +302,10 @@ public class ResourceServerTokenServicesConfiguration {
}
}
private
String
getKeyFromServer
()
{
private
String
getKeyFromServer
()
{
RestTemplate
keyUriRestTemplate
=
new
RestTemplate
();
for
(
JwtAccessTokenConverterRestTemplateCustomizer
customizer
:
this
.
customizers
)
{
customizer
.
customize
(
keyUriRestTemplate
);
}
HttpHeaders
headers
=
new
HttpHeaders
();
HttpHeaders
headers
=
new
HttpHeaders
();
String
username
=
this
.
resource
.
getClientId
();
String
username
=
this
.
resource
.
getClientId
();
String
password
=
this
.
resource
.
getClientSecret
();
String
password
=
this
.
resource
.
getClientSecret
();
...
@@ -308,7 +315,7 @@ public class ResourceServerTokenServicesConfiguration {
...
@@ -308,7 +315,7 @@ public class ResourceServerTokenServicesConfiguration {
}
}
HttpEntity
<
Void
>
request
=
new
HttpEntity
<
Void
>(
headers
);
HttpEntity
<
Void
>
request
=
new
HttpEntity
<
Void
>(
headers
);
String
url
=
this
.
resource
.
getJwt
().
getKeyUri
();
String
url
=
this
.
resource
.
getJwt
().
getKeyUri
();
return
(
String
)
this
.
keyUriRestTemplate
return
(
String
)
keyUriRestTemplate
.
exchange
(
url
,
HttpMethod
.
GET
,
request
,
Map
.
class
).
getBody
()
.
exchange
(
url
,
HttpMethod
.
GET
,
request
,
Map
.
class
).
getBody
()
.
get
(
"value"
);
.
get
(
"value"
);
}
}
...
...
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/oauth2/resource/ResourceServerTokenServicesConfigurationTests.java
View file @
dc9ff738
...
@@ -56,6 +56,7 @@ import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
...
@@ -56,6 +56,7 @@ import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
import
org.springframework.security.oauth2.provider.token.RemoteTokenServices
;
import
org.springframework.security.oauth2.provider.token.RemoteTokenServices
;
import
org.springframework.social.connect.ConnectionFactoryLocator
;
import
org.springframework.social.connect.ConnectionFactoryLocator
;
import
org.springframework.stereotype.Component
;
import
org.springframework.stereotype.Component
;
import
org.springframework.web.client.RestTemplate
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
static
org
.
mockito
.
Mockito
.
mock
;
...
@@ -65,6 +66,7 @@ import static org.mockito.Mockito.mock;
...
@@ -65,6 +66,7 @@ import static org.mockito.Mockito.mock;
*
*
* @author Dave Syer
* @author Dave Syer
* @author Madhura Bhave
* @author Madhura Bhave
* @author Eddú Meléndez
*/
*/
public
class
ResourceServerTokenServicesConfigurationTests
{
public
class
ResourceServerTokenServicesConfigurationTests
{
...
@@ -240,6 +242,23 @@ public class ResourceServerTokenServicesConfigurationTests {
...
@@ -240,6 +242,23 @@ public class ResourceServerTokenServicesConfigurationTests {
.
isInstanceOf
(
CustomUserInfoRestTemplateFactory
.
class
);
.
isInstanceOf
(
CustomUserInfoRestTemplateFactory
.
class
);
}
}
@Test
public
void
customRestTemplate
()
{
EnvironmentTestUtils
.
addEnvironment
(
this
.
environment
,
"security.oauth2.resource.userInfoUri:http://example.com"
,
"security.oauth2.resource.tokenInfoUri:http://example.com"
,
"security.oauth2.resource.preferTokenInfo:false"
);
this
.
context
=
new
SpringApplicationBuilder
(
ResourceConfiguration
.
class
,
RestTemplateCustomizer
.
class
).
environment
(
this
.
environment
).
web
(
false
)
.
run
();
String
[]
restTemplateCustomizers
=
this
.
context
.
getBeanNamesForType
(
JwtAccessTokenConverterRestTemplateCustomizer
.
class
);
UserInfoTokenServices
services
=
this
.
context
.
getBean
(
UserInfoTokenServices
.
class
);
assertThat
(
restTemplateCustomizers
).
hasSize
(
1
);
assertThat
(
services
).
isNotNull
();
}
@Configuration
@Configuration
@Import
({
ResourceServerTokenServicesConfiguration
.
class
,
@Import
({
ResourceServerTokenServicesConfiguration
.
class
,
ResourceServerPropertiesConfiguration
.
class
,
ResourceServerPropertiesConfiguration
.
class
,
...
@@ -354,4 +373,22 @@ public class ResourceServerTokenServicesConfigurationTests {
...
@@ -354,4 +373,22 @@ public class ResourceServerTokenServicesConfigurationTests {
}
}
@Component
protected
static
class
RestTemplateCustomizer
implements
JwtAccessTokenConverterRestTemplateCustomizer
{
@Override
public
void
customize
(
RestTemplate
template
)
{
template
.
getInterceptors
().
add
(
new
ClientHttpRequestInterceptor
()
{
@Override
public
ClientHttpResponse
intercept
(
HttpRequest
request
,
byte
[]
body
,
ClientHttpRequestExecution
execution
)
throws
IOException
{
return
execution
.
execute
(
request
,
body
);
}
});
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment