Azure-Samples/azure-spring-boot-samples

Azure AD Spring cloud Starter Resource Server

diawara24 opened this issue · 2 comments

Hello guys,
I have a spring rest application back-end and Angular on the front. I am using JWT auth0 to secure my application.

Now I want to have a possibility to connect with azure AD.

Through this git repo aad/spring-cloud-azure-starter-active-directory/web-client-access-resource-server/aad-resource-server , I created my application on https://portal.azure.com/, added the dependencies but my problem is that I can't configure the security part with spring security.

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private AadAuthenticationFilter aadAuthFilter;
    private JwtAuthorizationFilter jwtAuthorizationFilter;
    private JwtAccessDeniedHandler jwtAccessDeniedHandler;
    private JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint;
    private UserDetailsService userDetailsService;
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    private CustomAuthenticationProvider customAuthenticationProvider;

    @Autowired
    public SecurityConfig(JwtAuthorizationFilter jwtAuthorizationFilter,
                          JwtAccessDeniedHandler jwtAccessDeniedHandler,
                          JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint,
                          @Qualifier("userDetailsService")UserDetailsService userDetailsService,
                          BCryptPasswordEncoder bCryptPasswordEncoder,
                          CustomAuthenticationProvider customAuthenticationProvider
                          ) {
        this.jwtAuthorizationFilter = jwtAuthorizationFilter;
        this.jwtAccessDeniedHandler = jwtAccessDeniedHandler;
        this.jwtAuthenticationEntryPoint = jwtAuthenticationEntryPoint;
        this.userDetailsService = userDetailsService;
        this.bCryptPasswordEncoder = bCryptPasswordEncoder;
        this.customAuthenticationProvider = customAuthenticationProvider;

    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable().cors().and()
                .sessionManagement().sessionCreationPolicy(STATELESS)
                .and().authorizeRequests().antMatchers(PUBLIC_URLS).permitAll()
                .anyRequest().authenticated()
                .and()
                .exceptionHandling().accessDeniedHandler(jwtAccessDeniedHandler)
                .authenticationEntryPoint(jwtAuthenticationEntryPoint)
                .and()
                .addFilterBefore(jwtAuthorizationFilter, UsernamePasswordAuthenticationFilter.class)
                .addFilterBefore(aadAuthFilter, UsernamePasswordAuthenticationFilter.class);
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

application.yml

spring:
  cloud:
    azure:
      active-directory:
        enabled: true
        credential:
          client-id: client-id
        app-id-uri: app-id-uri

when I compile I get this error

***************************
APPLICATION FAILED TO START
***************************

Description:

Field aadAuthFilter in SecurityConfig required a bean of type 'com.azure.spring.cloud.autoconfigure.aad.filter.AadAuthenticationFilter' that could not be found.

The injection point has the following annotations:
	- @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.azure.spring.cloud.autoconfigure.aad.filter.AadAuthenticationFilter' in your configuration.


Process finished with exit code 0

Hi, @diawara24
Thanks for reaching out.

Please try these steps:

  1. Make your SecurityConfig extend AadWebSecurityConfigurerAdapter instead of WebSecurityConfigurerAdapter . Refs: https://learn.microsoft.com/en-us/azure/developer/java/spring-framework/spring-security-support?tabs=SpringCloudAzure4x#add-extra-security-configurations
  2. Remove JwtAuthorizationFilter related content.

Closing this issue now because there's no response from the OP. Please reopen it if the issue still persists.