question

NareshPawar-1854 avatar image
0 Votes"
NareshPawar-1854 asked sikumars commented

[invalid_token_response] An error occurred while attempting to retrieve the OAuth 2.0 Access Token Response: 401 Unauthorized: [no body]

I have added Azure AD as an authentication server but it is showing me the following error "[invalid_token_response] An error occurred while attempting to retrieve the OAuth 2.0 Access Token Response: 401 Unauthorized: [no body]"
Springboot: 2.5.8
Azure version: 3.10.0

my application.properties as follow:

     spring.security.oauth2.client.registration.azure-client.provider=azure-ad
     spring.security.oauth2.client.registration.azure-client.client-id=<Client_id>
     spring.security.oauth2.client.registration.azure-client.client-secret=<Client_secret>
     spring.security.oauth2.client.registration.azure-client.authorization-grant-type=authorization_code
     spring.security.oauth2.client.registration.azure-client.redirect-uri={baseUrl}/login/oauth2/code/{registrationId}
     spring.security.oauth2.client.registration.azure-client.scope=openid, profile
     spring.security.oauth2.client.registration.azure-client.client-authentication-method=post
     spring.security.oauth2.client.provider.azure-ad.authorization-uri=https://login.microsoftonline.com/common/oauth2/v2.0/authorize
     spring.security.oauth2.client.provider.azure-ad.token-uri=https://login.microsoftonline.com/common/oauth2/v2.0/token
     spring.security.oauth2.client.provider.azure-ad.jwk-set-uri=https://login.microsoftonline.com/common/discovery/v2.0/keys
     spring.security.oauth2.client.provider.azure-ad.user-name-attribute=name
     server.forward-headers-strategy=native
     logging.level.org.springframework.security=DEBUG

And SecurityConfig class as follow:

 @Configuration
 @EnableWebSecurity
 public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
     @Override
     protected void configure( HttpSecurity http ) throws Exception {
    
         http.authorizeRequests()
                 .antMatchers( "/oauth2/**", "/login/**" ).permitAll()
                 .anyRequest().authenticated()
                 .and()
                 .oauth2Login()
                 .defaultSuccessUrl( "/home" );
    
     }
 }

And Controller as follows:

 @RestController
 public class HomeController {
    
     @GetMapping( "home" )
     public String home( @AuthenticationPrincipal(expression = "claims['name']") String name ) {
         return String.format( "Hello %s!  welcome to the Security app", name);
     }
 }

Kindly help, thanks!


azure-ad-authenticationazure-security-centerazure-ad-openid-connectazure-webapps-authenticationazure-spring-cloud
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@NareshPawar-1854,

Thanks for reaching out.

Do you see any correlation ID and timestamp for above error? if so can you share that with us? Thanks.

0 Votes 0 ·

@sikumars-msft thanks for the reply. I had mistakenly added the wrong client secret, after adding the correct one it is working fine.

1 Vote 1 ·

1 Answer

NareshPawar-1854 avatar image
1 Vote"
NareshPawar-1854 answered sikumars commented

I had mistakenly added the wrong client secret, after adding the correct one it is working fine.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks for the update. Kindy "accept your answer" so that this beneficial to other community members reading this thread and experiencing similar issue. Thanks.

0 Votes 0 ·