Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request for Developer Support: Security Filter with Spring Boot 3.+ #695

Open
2 tasks done
BillyBolton opened this issue May 24, 2023 · 3 comments
Open
2 tasks done
Labels
azure-spring All azure-spring related issues

Comments

@BillyBolton
Copy link

BillyBolton commented May 24, 2023

Query/Question
Note: I have also asked this question on StackOverflow here

I would like to use SecurityFilters instead of fine grain annotations for protecting a resource server's endpoints. However, it does not seem easily possible to add a whitelist to some of these endpoints doing so. Either both will be blocked or both will be permitted.

I'm wondering what I may be doing wrong with the following example:

Gradle dependencies:

implementation 'com.azure.spring:spring-cloud-azure-starter-active-directory:5.1.0'
implementation 'org.springframework.boot:spring-boot-starter-oauth2-resource-server:3.1.0'

application.properties

# ...
spring.cloud.azure.active-directory.enabled=true
spring.cloud.azure.active-directory.credential.client-id=${client-id}
spring.cloud.azure.active-directory.app-id-uri=${app-id-uri}

Example AadOAuth2ResourceServerSecurityConfig

@Configuration(proxyBeanMethods = false)
@EnableWebSecurity
@EnableMethodSecurity
public class AadOAuth2ResourceServerSecurityConfig {

    @Bean
    public SecurityFilterChain apiFilterChain(HttpSecurity http) throws Exception {
        http.apply(AadResourceServerHttpSecurityConfigurer.aadResourceServer())
                .and()
                .authorizeHttpRequests()
                .requestMatchers("/api/docs/**").permitAll()
                .requestMatchers("/api/**").hasRole("ADMIN") // any other than "/api/docs/**"
        ;
        return http.build();
    }
}

Why is this not a Bug or a feature Request?
This is a request for support from Azure developers.

Setup (please complete the following information if applicable):

  • OS: MacOS 13.4
  • IDE: IntelliJ
  • Sample Path:

Note: this seems old for Spring Boot 3.+
https://github.com/Azure-Samples/azure-spring-boot-samples/blob/main/aad/spring-cloud-azure-starter-active-directory/aad-resource-server-by-filter/src/main/java/com/azure/spring/sample/aad/security/WebSecurityConfig.java

However, I've followed the documentation here:
https://learn.microsoft.com/en-us/azure/developer/java/spring-framework/secure-your-restful-api-using-spring-cloud-azure

  • Library/Libraries:
implementation 'com.azure.spring:spring-cloud-azure-starter-active-directory:5.1.0'
implementation 'org.springframework.boot:spring-boot-starter-oauth2-resource-server:3.1.0'

Information Checklist

  • Query Added
  • Setup information Added
@BillyBolton BillyBolton added the azure-spring All azure-spring related issues label May 24, 2023
@cheikh-diop
Copy link

Hello @BillyBolton it s working for me with the following configuration : http.apply(AadResourceServerHttpSecurityConfigurer.aadResourceServer())
.and()
.authorizeHttpRequests((requests) -> requests
.requestMatchers( "url to allow").permitAll()
.anyRequest().authenticated());
// @Formatter:on
return http.build();

@BillyBolton
Copy link
Author

Hi @cheikh-diop , thanks for the response.

Your response is similar to the example that is not working for me, but excluding the other endpoint that I need have a specific Role/Permission/Scope for.

In my example, either both endpoints will be permitted or both will be blocked (when attempting different things).

@moarychan
Copy link
Contributor

Hi @BillyBolton thanks for using Spring Cloud Azure!

You can try to config a bean WebSecurityCustomizer for path /api/docs/**, see more from https://docs.spring.io/spring-security/reference/5.8/migration/servlet/config.html#_publish_a_websecuritycustomizer_bean

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
azure-spring All azure-spring related issues
Projects
None yet
Development

No branches or pull requests

3 participants