I need to get the access token from oauth2.0 Authorization code grant. I have clientid, client secret, redirect uri, token uri etc. Using spring boot I hit an api so that it'll redirect into Authorization uri where I'll enter user credentials and it'll redirect into another url which is specified and there I can see the authorization code For example :- (After redirect I can see the authorization code in the browser) https://domain.xyz?code=xyzyejsusjxyzxy

Here I need this Authorization code to hit the token url to get the access token. But, since that redirect url is server one I cannot call back to local api (if I do this I'm getting 400 invalid redirect uri and even I cannot change this redirect uri in auth server). So, how can I capture this Authorization code to hit the token url? Kindly anyone help here.

I'm using the below api in spring boot where here it is redirected into Authorization url

@GetMapping("/initiate-auth")
    public ResponseEntity<Void> initiateOAuth2Authentication() {
String redirectUri= "https://domain.xyz.oidcclient/sub";
//(for example)
        String authorizationRequestUri = "/oauth2/authorization?response_type=code&scope=opening&redirecturi" + redirectUri;

        // Perform redirection to the OAuth2 provider's authorization page
        return ResponseEntity.status(302)
                .header("Location", authorizationRequestUri)
                .build();

I even tried with security config in spring boot

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/initiate-auth").permitAll()
                .anyRequest().authenticated()
                .and()
            .oauth2Login()
                .userInfoEndpoint()
                    .oidcUserService(oidcUserService());
    }

    @Bean
    public ClientRegistrationRepository clientRegistrationRepository() {
        return new InMemoryClientRegistrationRepository(clientRegistration());
    }

    private ClientRegistration clientRegistration() {
        return ClientRegistration.withRegistrationId("your-client-id")
                .clientId("your-client-id")
                .clientSecret("your-client-secret")
                .redirectUriTemplate("{baseUrl}/oauth2/callback")
                .authorizationUri("your-authorization-uri")
                .tokenUri("your-token-uri")
                .userInfoUri("your-user-info-uri")
                .userNameAttributeName("your-username-attribute")
                .clientName("Your Client Name")
                .build();
    }
}

But I'm unable to capture authorization code in local console and hit the token uri.

But same way I can do in postman and can get the access token. I checked in postman console and I assure that the same Authorization uri with redirect uri is mentioned in that but it can able to capture the token and hit the token url to get the access token.

If there is any config to do this automatic process handled by spring boot please share and it'll help me a lot

The requirement is to get the access token so that I can consume the rest api which is secured by oauth2

I even tried to get the access token through client credentials grant type but unfortunately I'm getting 403 Forbidden. But when I try to hit with the access token which is generated through Authorization code, I can hit the api successfully through post man. So, similar way I need to get the token through java spring boot.

I would like to request you to please help me to get the access token from oauth2 authorization code grant type.

Thank you.

0

There are 0 best solutions below