# authrizeRequest

**URL:** <https://devforum.okta.com/t/authrizerequest/16210>\
**Category:** OAuth/OIDC\
**Created:** [September 17, 2021, 8:12pm UTC](https://devforum.okta.com/t/authrizerequest/16210 "2021-09-17T20:12:22Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![shreekucherla](https://avatars.discourse-cdn.com/v4/letter/s/ccd318/32.png) [@shreekucherla](https://devforum.okta.com/u/shreekucherla)\
**Post date:** [September 17, 2021, 8:12pm UTC](https://devforum.okta.com/t/authrizerequest/16210/1 "2021-09-17T20:12:22Z")

</div>

```auto
// Build an OAuth2 request for the Okta provider
		OAuth2AuthorizeRequest authorizeRequest = OAuth2AuthorizeRequest.withClientRegistrationId("okta")
				.principal("Demo Service")
				.build();

```

above line from blog [How to Use Client Credentials Flow with Spring Security | Okta Developer](https://developer.okta.com/blog/2021/05/05/client-credentials-spring-security#comment-5538395893) is giving compilation error. .prinicipal needs authentication,but example shows string

---

<div class="post-metadata">

**Author:** ![mraible](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/mraible/32/37_2.png) [@mraible](https://devforum.okta.com/u/mraible)\
**Post date:** [September 17, 2021, 8:27pm UTC](https://devforum.okta.com/t/authrizerequest/16210/2 "2021-09-17T20:27:47Z")

</div>

Are you using the same version of Spring Boot (v2.4.5) that this blog post is using?

---

<div class="post-metadata">

**Author:** ![shreekucherla](https://avatars.discourse-cdn.com/v4/letter/s/ccd318/32.png) [@shreekucherla](https://devforum.okta.com/u/shreekucherla)\
**Post date:** [September 17, 2021, 8:37pm UTC](https://devforum.okta.com/t/authrizerequest/16210/3 "2021-09-17T20:37:42Z")

</div>

no I am using   
org.springframework.boot  
spring-boot-starter-parent  
2.2.7.RELEASE

---

<div class="post-metadata">

**Author:** ![shreekucherla](https://avatars.discourse-cdn.com/v4/letter/s/ccd318/32.png) [@shreekucherla](https://devforum.okta.com/u/shreekucherla)\
**Post date:** [September 17, 2021, 8:51pm UTC](https://devforum.okta.com/t/authrizerequest/16210/4 "2021-09-17T20:51:00Z")

</div>

Controller:

```auto
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.oauth2.client.AuthorizedClientServiceOAuth2AuthorizedClientManager;
import org.springframework.security.oauth2.client.OAuth2AuthorizeRequest;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
import org.springframework.security.oauth2.core.OAuth2AccessToken;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.reactive.function.client.WebClient;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Objects;

/**
 * Basic Login controller.
 */
@CrossOrigin // Allow used HttpMethods on the endpoints, from all origins
@RestController()
public class LoginController
{
    private static final Logger LOG = LoggerFactory.getLogger(LoginController.class);

    private String resourceServerUrl = "http://localhost:11033/";
    @Autowired
    private AuthorizedClientServiceOAuth2AuthorizedClientManager authorizedClientServiceAndManager;

    /**
     * Show me.
     *
     * @param principal the principal
     */
    @GetMapping(value="/index")
    public void showMe(final Authentication principal){
        System.out.println(principal.getName());
    }

    /**
     * .
     * Redirect to home.
     *
     * @param authentication the authentication
     * @param servletRequest the servlet request
     * @param servletResponse the servlet response
     * @return string of page
     * @throws IOException the io exception
     */
    @RequestMapping(value = "/oauth2/callback", method = RequestMethod.GET)
    @DependsOn("authorizedClientServiceAndManager")
    public String authorizeClient(final Authentication authentication,
                                final HttpServletRequest servletRequest,
                                final HttpServletResponse servletResponse) throws IOException {

        final OAuth2AuthorizeRequest authorizeRequest = OAuth2AuthorizeRequest.withClientRegistrationId("DemoApp")
                .principal("Demo Service")
                .attributes(attrs -> {
                    attrs.put(HttpServletRequest.class.getName(), servletRequest);
                    attrs.put(HttpServletResponse.class.getName(), servletResponse);
                })
                .build();
        System.out.println(authorizeRequest.toString());
        final OAuth2AuthorizedClient authorizedClient = authorizedClientServiceAndManager
                .authorize(authorizeRequest);

        // Get the token from the authorized client object
        final OAuth2AccessToken accessToken = Objects.requireNonNull(authorizedClient).getAccessToken();

        System.out.println(accessToken.getTokenValue());
        LOG.info("Issued: " + accessToken.getIssuedAt().toString() + ", "
                + "Expires:" + accessToken.getExpiresAt().toString());
        LOG.info("Scopes: " + accessToken.getScopes().toString());
        LOG.info("Token: " + accessToken.getTokenValue());
        return "index";
    }

    

}

```

---

<div class="post-metadata">

**Author:** ![shreekucherla](https://avatars.discourse-cdn.com/v4/letter/s/ccd318/32.png) [@shreekucherla](https://devforum.okta.com/u/shreekucherla)\
**Post date:** [September 17, 2021, 8:53pm UTC](https://devforum.okta.com/t/authrizerequest/16210/5 "2021-09-17T20:53:48Z")

</div>

OauthClient Config ::

```auto
package com.veriscape.sso.webapp.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService;
import org.springframework.security.oauth2.client.InMemoryOAuth2AuthorizedClientService;
import org.springframework.security.oauth2.client.AuthorizedClientServiceOAuth2AuthorizedClientManager;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientProvider;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientProviderBuilder;
import org.springframework.security.oauth2.client.registration.ClientRegistration;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
import org.springframework.security.oauth2.client.registration.InMemoryClientRegistrationRepository;
import org.springframework.security.oauth2.client.web.AuthenticatedPrincipalOAuth2AuthorizedClientRepository;
import org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository;
import org.springframework.security.oauth2.client.web.reactive.function.client
        .ServletOAuth2AuthorizedClientExchangeFilterFunction;
import org.springframework.security.oauth2.core.AuthorizationGrantType;
import org.springframework.security.oauth2.core.ClientAuthenticationMethod;
import org.springframework.web.reactive.function.client.WebClient;

/**
 * The type Oauth client config.
 */
@Configuration
public class OauthClientConfig {

    /**
     * Oauth client config client registration.
     *
     * @param issuerUrl the issuer url
     * @param clientId the client id
     * @param clientSecret the client secret
     * @param scope the scope
     * @param authorizationGrantType the authorization grant type
     * @param redirectUri the redirect uri
     * @param tokenUri the token uri
     * @return the client registration
     */
    @Bean
    ClientRegistration oauthClientRegistration(
            @Value("${spring.cloud.sso.authIssuer}")
            final String issuerUrl,
            @Value("${spring.cloud.sso.authClientId}")
            final String clientId,
            @Value("${spring.cloud.sso.authSecret}")
            final String clientSecret,
            @Value("${spring.cloud.sso.authScope}")
            final String scope,
            @Value("${spring.cloud.sso.authGranttype}")
            final String authorizationGrantType,
            @Value("${spring.cloud.sso.authRedirectUri}")
            final String redirectUri,
            @Value("${spring.cloud.sso.tokenUri}")
            final String tokenUri

    ) {
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + issuerUrl);
        return ClientRegistration
                .withRegistrationId("DemoApp")
                .clientAuthenticationMethod(ClientAuthenticationMethod.BASIC)
                //.authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
                .authorizationUri(issuerUrl)
                .clientId(clientId)
                .clientSecret(clientSecret)
                .scope(scope)
                .tokenUri(tokenUri)
                .clientName("DemoApp")
                .redirectUriTemplate(redirectUri)
                .authorizationGrantType(new AuthorizationGrantType(authorizationGrantType))
                .build();
    }   

    /**
     * Client registration repository client registration repository.
     *
     * @param oauthClientRegistration the oauth client registration
     * @return the client registration repository
     */
// Create the client registration repository
    @Bean
    public ClientRegistrationRepository clientRegistrationRepository(final ClientRegistration oauthClientRegistration) {
        System.out.println(oauthClientRegistration.getClientId()+"--"+oauthClientRegistration.getClientName());
        return new InMemoryClientRegistrationRepository(oauthClientRegistration);
    }

    /**
     * Auth 2 authorized client service oauth2 authorized client service.
     *
     * @param clientRegistrationRepository the client registration repository
     * @return the o auth 2 authorized client service
     */
// Create the authorized client service
    @Bean
    public OAuth2AuthorizedClientService auth2AuthorizedClientService(
            final ClientRegistrationRepository clientRegistrationRepository) {
        return new InMemoryOAuth2AuthorizedClientService(clientRegistrationRepository);
    }

    /**
     * Authorized client service and manager authorized client service o auth 2 authorized client manager.
     *
     * @param clientRegistrationRepository the client registration repository
     * @param authorizedClientService the authorized client service
     * @return the authorized client service o auth2 authorized client manager
     */
// Create the authorized client manager and service manager using the
    // beans created and configured above
    @Bean
    public AuthorizedClientServiceOAuth2AuthorizedClientManager authorizedClientServiceAndManager(
            final ClientRegistrationRepository clientRegistrationRepository,
            final OAuth2AuthorizedClientService authorizedClientService) {

        final OAuth2AuthorizedClientProvider authorizedClientProvider =
                OAuth2AuthorizedClientProviderBuilder.builder()
                        .clientCredentials()
                        .build();

        final AuthorizedClientServiceOAuth2AuthorizedClientManager authorizedClientManager =
                new AuthorizedClientServiceOAuth2AuthorizedClientManager(
                        clientRegistrationRepository, authorizedClientService);
        authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);

        return authorizedClientManager;
    }

}

```

---

<div class="post-metadata">

**Author:** ![shreekucherla](https://avatars.discourse-cdn.com/v4/letter/s/ccd318/32.png) [@shreekucherla](https://devforum.okta.com/u/shreekucherla)\
**Post date:** [September 17, 2021, 8:56pm UTC](https://devforum.okta.com/t/authrizerequest/16210/6 "2021-09-17T20:56:01Z")

</div>

OauthClient Config:

```auto
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService;
import org.springframework.security.oauth2.client.InMemoryOAuth2AuthorizedClientService;
import org.springframework.security.oauth2.client.AuthorizedClientServiceOAuth2AuthorizedClientManager;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientProvider;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientProviderBuilder;
import org.springframework.security.oauth2.client.registration.ClientRegistration;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
import org.springframework.security.oauth2.client.registration.InMemoryClientRegistrationRepository;
import org.springframework.security.oauth2.client.web.AuthenticatedPrincipalOAuth2AuthorizedClientRepository;
import org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository;
import org.springframework.security.oauth2.client.web.reactive.function.client
        .ServletOAuth2AuthorizedClientExchangeFilterFunction;
import org.springframework.security.oauth2.core.AuthorizationGrantType;
import org.springframework.security.oauth2.core.ClientAuthenticationMethod;
import org.springframework.web.reactive.function.client.WebClient;

/**
 * The type Oauth client config.
 */
@Configuration
public class OauthClientConfig {

    /**
     * Oauth client config client registration.
     *
     * @param issuerUrl the issuer url
     * @param clientId the client id
     * @param clientSecret the client secret
     * @param scope the scope
     * @param authorizationGrantType the authorization grant type
     * @param redirectUri the redirect uri
     * @param tokenUri the token uri
     * @return the client registration
     */
    @Bean
    ClientRegistration oauthClientRegistration(
            @Value("${spring.cloud.sso.authIssuer}")
            final String issuerUrl,
            @Value("${spring.cloud.sso.authClientId}")
            final String clientId,
            @Value("${spring.cloud.sso.authSecret}")
            final String clientSecret,
            @Value("${spring.cloud.sso.authScope}")
            final String scope,
            @Value("${spring.cloud.sso.authGranttype}")
            final String authorizationGrantType,
            @Value("${spring.cloud.sso.authRedirectUri}")
            final String redirectUri,
            @Value("${spring.cloud.sso.tokenUri}")
            final String tokenUri

    ) {
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + issuerUrl);
        return ClientRegistration
                .withRegistrationId("DemoApp")
                .clientAuthenticationMethod(ClientAuthenticationMethod.BASIC)
                //.authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
                .authorizationUri(issuerUrl)
                .clientId(clientId)
                .clientSecret(clientSecret)
                .scope(scope)
                .tokenUri(tokenUri)
                .clientName("DemoApp")
                .redirectUriTemplate(redirectUri)
                .authorizationGrantType(new AuthorizationGrantType(authorizationGrantType))
                .build();
    }   

    /**
     * Client registration repository client registration repository.
     *
     * @param oauthClientRegistration the oauth client registration
     * @return the client registration repository
     */
// Create the client registration repository
    @Bean
    public ClientRegistrationRepository clientRegistrationRepository(final ClientRegistration oauthClientRegistration) {
        System.out.println(oauthClientRegistration.getClientId()+"--"+oauthClientRegistration.getClientName());
        return new InMemoryClientRegistrationRepository(oauthClientRegistration);
    }

    /**
     * Auth 2 authorized client service oauth2 authorized client service.
     *
     * @param clientRegistrationRepository the client registration repository
     * @return the o auth 2 authorized client service
     */
// Create the authorized client service
    @Bean
    public OAuth2AuthorizedClientService auth2AuthorizedClientService(
            final ClientRegistrationRepository clientRegistrationRepository) {
        return new InMemoryOAuth2AuthorizedClientService(clientRegistrationRepository);
    }

    /**
     * Authorized client service and manager authorized client service o auth 2 authorized client manager.
     *
     * @param clientRegistrationRepository the client registration repository
     * @param authorizedClientService the authorized client service
     * @return the authorized client service o auth2 authorized client manager
     */
// Create the authorized client manager and service manager using the
    // beans created and configured above
    @Bean
    public AuthorizedClientServiceOAuth2AuthorizedClientManager authorizedClientServiceAndManager(
            final ClientRegistrationRepository clientRegistrationRepository,
            final OAuth2AuthorizedClientService authorizedClientService) {

        final OAuth2AuthorizedClientProvider authorizedClientProvider =
                OAuth2AuthorizedClientProviderBuilder.builder()
                        .clientCredentials()
                        .build();

        final AuthorizedClientServiceOAuth2AuthorizedClientManager authorizedClientManager =
                new AuthorizedClientServiceOAuth2AuthorizedClientManager(
                        clientRegistrationRepository, authorizedClientService);
        authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);

        return authorizedClientManager;
    }

}

```

---

<div class="post-metadata">

**Author:** ![shreekucherla](https://avatars.discourse-cdn.com/v4/letter/s/ccd318/32.png) [@shreekucherla](https://devforum.okta.com/u/shreekucherla)\
**Post date:** [September 17, 2021, 8:56pm UTC](https://devforum.okta.com/t/authrizerequest/16210/7 "2021-09-17T20:56:56Z")

</div>

application Yam,

```auto
spring:
  profiles: default
  cloud:
    consul:
      enabled: false
      config:
        enabled: false
      discovery:
        enabled: false
    kubernetes:
      enabled: false
      config:
        enabled: false
      discovery:
        enabled: false
    sso:
      enabled: true
      authIssuer: https://dev-993218.okta.com/oauth2/v1/authorize
      authClientId: xxxxx
      authSecret: xxxxx
      authRedirectUri: http://localhost:11033/oauth2/callback
      authGranttype: authorization_code
      authScope: openid
      tokenUri: https://dev-993218.okta.com/oauth2/v1/token

```

---

<div class="post-metadata">

**Author:** ![bdemers](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/bdemers/32/23_2.png) [@bdemers](https://devforum.okta.com/u/bdemers)\
**Post date:** [September 18, 2021, 2:16am UTC](https://devforum.okta.com/t/authrizerequest/16210/8 "2021-09-18T02:16:45Z")

</div>

I’m guessing part of the problem is the custom `/oauth2/callback` route, The OAuth callback is handled by Spring Security, and should NOT be implemented in a custom controller.

Taking a step back, can you tell us a little more about what you are trying to do, maybe we can point you to an example that better fits your needs?

---

<div class="post-metadata">

**Author:** ![shreekucherla](https://avatars.discourse-cdn.com/v4/letter/s/ccd318/32.png) [@shreekucherla](https://devforum.okta.com/u/shreekucherla)\
**Post date:** [September 18, 2021, 5:46am UTC](https://devforum.okta.com/t/authrizerequest/16210/9 "2021-09-18T05:46:50Z")

</div>

Hi , Thank you very much for your response. All I was trying is when user hits an api end point on my server , I want to direct him to auth server , have him login and come back to me with authentication. So that I can extract his user name and may be policies.

Thank you  
Shree

---

<div class="post-metadata">

**Author:** ![andrea](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/andrea/32/2172_2.png) [@andrea](https://devforum.okta.com/u/andrea)\
**Post date:** [February 10, 2024, 12:58am UTC](https://devforum.okta.com/t/authrizerequest/16210/10 "2024-02-10T00:58:42Z")

</div>


