'azure ad config problems

Sorry, I can't speak English. But I had some problems

Currently logging in with Microsoft Azure AD But encounter the following problem


Method springSecurityFilterChain in org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration required a bean of type 'org.springframework.security.oauth2.client.registration.ClientRegistrationRepository' that could not be found.


Action:

Consider defining a bean of type 'org.springframework.security.oauth2.client.registration.ClientRegistrationRepository' in your configuration.

application.yml

server:
  port: 8080
  forward-headers-strategy: native
spring: 
  security: 
    oauth2: 
      client: 
        provider: 
          azure-ad: 
            authorization-uri: https://login.microsoftonline.com/common/oauth2/v2.0/authorize
            token-uri: https://login.microsoftonline.com/common/oauth2/v2.0/token
            jwk-set-uri: https://login.microsoftonline.com/common/discovery/v2.0/keys
          registration: 
            azure-client:
              provider: azure-ad
              client-id: 'xxxxxxxxxx'
              client-secret: 'xxxxxxxxxxxxxxxx'
              authorization-grant-type: authorization_code
              redirect-uri: '{baseUrl}/login/oauth2/code/'
              scope: openid,profile

SecurityConfig.java

import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class AzureADSecurityConfig extends WebSecurityConfigurerAdapter {   
    @Override
    protected void configure( HttpSecurity http ) throws Exception {
        http.authorizeRequests()
            .antMatchers("/","/login").permitAll()
            .anyRequest().authenticated()
            .and()
            .oauth2Login();
            //.defaultSuccessUrl("/monitor");
    }
}

Controller.java

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AzureADController {
    @GetMapping("/Admin")
    public String Admin() {
        System.out.println("================== Azure ad ==================");
        return "Admin message";
    }
}

Any one has any ideas, please? Thanks very much.



Solution 1:[1]

There seems to be an extra indent for client registration. Registration and provider should be on the same level under spring.security.oauth2.client

spring.security.oauth2.client.provider.azure-ad
spring.security.oauth2.client.registration.azure-client

See: https://docs.spring.io/spring-security/site/docs/5.2.12.RELEASE/reference/html/oauth2.html

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Delta George