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

[Feature Request] Spring boot azure active directory multiple client-id #21254

Closed
2 tasks done
ObakeFilter opened this issue May 9, 2021 · 5 comments
Closed
2 tasks done
Assignees
Labels
azure-spring All azure-spring related issues azure-spring-aad Spring active directory related issues. Client This issue points to a problem in the data-plane of the library. customer-reported Issues that are reported by GitHub users external to the Azure organization. feature-request This issue requires a new behavior in the product in order be resolved.

Comments

@ObakeFilter
Copy link

ObakeFilter commented May 9, 2021

Query/Question
I have a Spring Boot app that use azure active directory with front-end authentication (using AADAppRoleStatelessAuthenticationFilter, similar to this w/o using a login page (it's merly a REST api with two Post endpoints).

I would like to extend it so I could be able to authenticate using multiple sets of client ids, client secrets & app uris, meaning, that each request sent to a specific host will be mapped to it's own AADAppRoleStatelessAuthenticationFilter instance for authentication purposes.
Problem I am having is that trying to autowire multiple instances of AADAppRoleStatelessAuthenticationFilter will end up creating using only 1 set of configurations, even when I used 2 configuration files with @propertysource, every autowired class ended up being created from the same configurations (app-id-uri, secret etc....) and obviously failed to authenticate requests for all other applications.

How can I achieve multi-app authentication?

This is my attempt for breaking the security configuration to different classes:

    public static final String HTTPS_SUFFIX = ":443";
    public static final String REPLACEMENT_REGEX = "^http[s]?://";

    @Configuration
    @PropertySource(factory = YamlPropertySourceFactory.class, value = "classpath:default-${profile.name}.yml")
    @Order(1)
    public static class SecConfig1 extends WebSecurityConfigurerAdapter {
        @Value("${azure.activedirectory.app-id-uri}")
        String appIdUri;
        @Autowired
        AADAppRoleStatelessAuthenticationFilter aadAppRoleStatelessAuthenticationFilter;

        @Override
        protected void configure(HttpSecurity http) {
            http.requestMatcher(new RequestHeaderRequestMatcher("Host", appIdUri.replaceFirst(REPLACEMENT_REGEX, "") + HTTPS_SUFFIX))
                    .addFilterBefore(aadAppRoleStatelessAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
        }
    }
    @Configuration
    @PropertySource(factory = YamlPropertySourceFactory.class, value = "classpath:gcc-${profile.name}.yml")
    @Order(2)
    public static class SecConfig2 extends WebSecurityConfigurerAdapter {
        @Value("${azure.activedirectory.app-id-uri}")
        String appIdUri;
        @Autowired
        AADAppRoleStatelessAuthenticationFilter aadAppRoleStatelessAuthenticationFilterGcc;

        @Override
        protected void configure(HttpSecurity http) {
            http.requestMatcher(new RequestHeaderRequestMatcher("Host", appIdUri.replaceFirst(REPLACEMENT_REGEX, "") + HTTPS_SUFFIX))
                    .addFilterBefore(aadAppRoleStatelessAuthenticationFilterGcc, UsernamePasswordAuthenticationFilter.class);
        }
    }

public class YamlPropertySourceFactory implements PropertySourceFactory {

    @Override
    public PropertySource<?> createPropertySource(@Nullable String name, EncodedResource resource) throws IOException {
        Properties propertiesFromYaml = loadYamlIntoProperties(resource);
        String sourceName = name != null ? name : resource.getResource().getFilename();
        return new PropertiesPropertySource(sourceName, propertiesFromYaml);
    }

    private Properties loadYamlIntoProperties(EncodedResource resource) throws FileNotFoundException {
        try {
            YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
            factory.setResources(resource.getResource());
            factory.afterPropertiesSet();
            return factory.getObject();
        } catch (IllegalStateException e) {
            // for ignoreResourceNotFound
            Throwable cause = e.getCause();
            if (cause instanceof FileNotFoundException)
                throw (FileNotFoundException) e.getCause();
            throw e;
        }
    }
}

And my yaml configuration files both look like this:

spring:
  security:
    oauth2:
      client:
        registration:
          azure:
            client-id: XXX
            client-secret: YYYY

azure:
  activedirectory:
    tenant-id: ZZZ
    client-id: XXX
    client-secret: YYY
    session-stateless: true
    app-id-uri: https://example.com
    user-group:
      allowed-groups: Users

Setup (please complete the following information if applicable):

  • OS: Alpine
  • IDE : IntelliJ Idea Ultimate
  • azure-active-directory-spring-boot-starter 2.3.5

Information Checklist
Kindly make sure that you have added all the following information above and checkoff the required fields otherwise we will treat the issuer as an incomplete report

  • Query Added
  • Setup information Added
@ghost ghost added needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. customer-reported Issues that are reported by GitHub users external to the Azure organization. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that labels May 9, 2021
@joshfree joshfree added azure-spring All azure-spring related issues azure-spring-aad Spring active directory related issues. Client This issue points to a problem in the data-plane of the library. labels May 10, 2021
@ghost ghost removed the needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. label May 10, 2021
@joshfree
Copy link
Member

@stliu could you please take a look?

@backwind1233 backwind1233 self-assigned this May 12, 2021
@chenrujun chenrujun added this to the Backlog milestone May 12, 2021
@chenrujun chenrujun changed the title [QUERY] Spring boot azure active directory multiple client-id [Feature Request] Spring boot azure active directory multiple client-id May 17, 2021
@chenrujun chenrujun added feature-request This issue requires a new behavior in the product in order be resolved. and removed question The issue doesn't require a change to the product in order to be resolved. Most issues start as that labels May 17, 2021
@chenrujun
Copy link

Hi, @ObakeFilter

Thank you for reaching out.
Now multi client-id is not supported yet.
We will investigate whether it's necessary to implement this feature and how to implement.

We will update the process in this issue.

@ObakeFilter
Copy link
Author

Hi, @ObakeFilter

Thank you for reaching out.
Now multi client-id is not supported yet.
We will investigate whether it's necessary to implement this feature and how to implement.

We will update the process in this issue.

Thank you, are you familiar of any possible workaround that can be used to instantiate multiple AADAppRoleStatelessAuthenticationFilter as of today?

@chenrujun
Copy link

Hi, @ObakeFilter

  1. AADAppRoleStatelessAuthenticationFilter is deprecated. Could you please use resource-server instead?
  2. Now resource-server still not support multi client-id.
  3. Sorry that I can not get work around in a short time. And now I'm busy with other tasks. This issue is in out roadmap. Could you please investigate by yourself? And PR is welcome.

@chenrujun
Copy link

Closing this issue, because we decide to deprecate azure-spring-boot-starter-active-directory. Just write sample to demonstrate how to use Azure Active Directory in Spring Boot application in this repo: https://github.com/Azure-Samples/spring-boot-application-with-azure-active-directory

Repository owner moved this from Todo to Done in Spring Cloud Azure Nov 5, 2021
@chenrujun chenrujun modified the milestones: Backlog, [2021] November Nov 5, 2021
@github-actions github-actions bot locked and limited conversation to collaborators Apr 12, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
azure-spring All azure-spring related issues azure-spring-aad Spring active directory related issues. Client This issue points to a problem in the data-plane of the library. customer-reported Issues that are reported by GitHub users external to the Azure organization. feature-request This issue requires a new behavior in the product in order be resolved.
Projects
Archived in project
Development

No branches or pull requests

5 participants