From 5dedba216ff544ffa1439d44437d89cf2434cdb1 Mon Sep 17 00:00:00 2001 From: DOHA Date: Wed, 25 Feb 2015 21:56:03 +0200 Subject: [PATCH] use enhancer --- .../config/CustomRequestEnhancer.java | 22 +++++++++++++++++++ ...yAuthorizationCodeAccessTokenProvider.java | 2 ++ .../java/org/baeldung/config/WebConfig.java | 6 +++-- 3 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 spring-security-oauth/src/main/java/org/baeldung/config/CustomRequestEnhancer.java diff --git a/spring-security-oauth/src/main/java/org/baeldung/config/CustomRequestEnhancer.java b/spring-security-oauth/src/main/java/org/baeldung/config/CustomRequestEnhancer.java new file mode 100644 index 0000000000..cb213719af --- /dev/null +++ b/spring-security-oauth/src/main/java/org/baeldung/config/CustomRequestEnhancer.java @@ -0,0 +1,22 @@ +package org.baeldung.config; + +import java.io.Serializable; + +import org.springframework.http.HttpHeaders; +import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails; +import org.springframework.security.oauth2.client.token.AccessTokenRequest; +import org.springframework.security.oauth2.client.token.RequestEnhancer; +import org.springframework.util.MultiValueMap; + +public class CustomRequestEnhancer implements RequestEnhancer, Serializable { + /** + * + */ + private static final long serialVersionUID = 1L; + + @Override + public void enhance(AccessTokenRequest request, OAuth2ProtectedResourceDetails resource, MultiValueMap form, HttpHeaders headers) { + System.out.println("called==="); + form.set("duration", "permanent"); + } +} diff --git a/spring-security-oauth/src/main/java/org/baeldung/config/MyAuthorizationCodeAccessTokenProvider.java b/spring-security-oauth/src/main/java/org/baeldung/config/MyAuthorizationCodeAccessTokenProvider.java index c5c7226d10..47ff588fc0 100644 --- a/spring-security-oauth/src/main/java/org/baeldung/config/MyAuthorizationCodeAccessTokenProvider.java +++ b/spring-security-oauth/src/main/java/org/baeldung/config/MyAuthorizationCodeAccessTokenProvider.java @@ -56,6 +56,7 @@ public class MyAuthorizationCodeAccessTokenProvider extends AuthorizationCodeAcc form.putAll(getParametersForAuthorizeRequest(resource, request)); } form.set("duration", "permanent"); + System.out.println("===== at enhancer point ==="); authorizationRequestEnhancer.enhance(request, resource, form, headers); final AccessTokenRequest copy = request; @@ -192,6 +193,7 @@ public class MyAuthorizationCodeAccessTokenProvider extends AuthorizationCodeAcc requestParameters.put("response_type", "code"); requestParameters.put("client_id", resource.getClientId()); requestParameters.put("duration", "permanent"); + System.out.println("===== at private message redirect ==="); String redirectUri = resource.getRedirectUri(request); if (redirectUri != null) { diff --git a/spring-security-oauth/src/main/java/org/baeldung/config/WebConfig.java b/spring-security-oauth/src/main/java/org/baeldung/config/WebConfig.java index 06f5915c0a..f7167eaee6 100644 --- a/spring-security-oauth/src/main/java/org/baeldung/config/WebConfig.java +++ b/spring-security-oauth/src/main/java/org/baeldung/config/WebConfig.java @@ -15,6 +15,7 @@ import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResour import org.springframework.security.oauth2.client.token.AccessTokenProvider; import org.springframework.security.oauth2.client.token.AccessTokenProviderChain; import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsAccessTokenProvider; +import org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeAccessTokenProvider; import org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails; import org.springframework.security.oauth2.client.token.grant.implicit.ImplicitAccessTokenProvider; import org.springframework.security.oauth2.client.token.grant.password.ResourceOwnerPasswordAccessTokenProvider; @@ -93,8 +94,9 @@ public class WebConfig extends WebMvcConfigurerAdapter { @Bean public OAuth2RestTemplate redditRestTemplate(OAuth2ClientContext clientContext) { OAuth2RestTemplate template = new OAuth2RestTemplate(reddit(), clientContext); - AccessTokenProvider accessTokenProvider = new AccessTokenProviderChain(Arrays. asList(new MyAuthorizationCodeAccessTokenProvider(), new ImplicitAccessTokenProvider(), new ResourceOwnerPasswordAccessTokenProvider(), - new ClientCredentialsAccessTokenProvider())); + AuthorizationCodeAccessTokenProvider authProvider = new AuthorizationCodeAccessTokenProvider(); + authProvider.setAuthorizationRequestEnhancer(new CustomRequestEnhancer()); + AccessTokenProvider accessTokenProvider = new AccessTokenProviderChain(Arrays. asList(authProvider, new ImplicitAccessTokenProvider(), new ResourceOwnerPasswordAccessTokenProvider(), new ClientCredentialsAccessTokenProvider())); template.setAccessTokenProvider(accessTokenProvider); return template; }