如何解决Spring Security和Azure:是否有Active Directory组通配符?
https://docs.microsoft.com/en-us/azure/developer/java/spring-framework/configure-spring-boot-starter-java-app-with-azure-active-directory上的教程说明了如何在Microsoft Azure Active Directory上通过身份验证设置Spring Security。
忽略两个小差异(在此处OpenID Connect log in in with Office 365 and spring security中进行解释),效果很好。
在我的application.properties中,有以下属性:
azure.activedirectory.active-directory-groups=myADUserGroup
(提示:azure.activedirectory.active-directory-groups似乎是较新的azure.activedirectory.user-group.allowed-groups的不建议使用的版本...)
我不想限制特定的群体。对于我的用例,每个具有有效Microsoft帐户的用户都可以。 将属性留空或什至删除属性都会导致此异常:
Caused by: java.lang.IllegalStateException: One of the User Group Properties must be populated. Please populate azure.activedirectory.user-group.allowed-groups
at com.microsoft.azure.spring.autoconfigure.aad.AADAuthenticationProperties.validateUserGroupProperties(AADAuthenticationProperties.java:148) ~[azure-spring-boot-2.3.1.jar:na]
可能的解决方法是在application.properties中为属性输入一些任意的组名:
azure.activedirectory.active-directory-groups=some-arbitrary-group-name-doesnt-matter
并且不要使用@PreAuthorize("hasRole('[group / role name]')")
。
这有效(只要您的应用对角色名称不感兴趣),但感觉不正确。 A)是否有“正确”的方法来设置通配符active-directory-group? B)org.springframework.security.core.Authentication.getAuthorities()似乎只传递那些在该属性中输入的组名/角色名,因此解决方法不传递任何东西(但ROLE_USER)。我想阅读用户的所有组/角色。因此,我提出了第二个问题:如何从org.springframework.security.core.Authentication.getAuthorities()中获得所有角色,而又不知道所有角色,尤其是不将所有角色都输入“ azure.activedirectory.active-directory-组”属性?
解决方法
目前,它不支持为Azure活动目录组设置通配符。
您可以给您voice,以提高广告反馈的准确性,如果其他人有相同的需求,也会投票给您。很多投票将促进该功能的实现。
,这不是组通配符,但是如果stateless processing符合您的需要,
azure.activedirectory.active-directory-groups=...
可以替换为
azure.activedirectory.session-stateless=true
这将激活AADAppRoleStatelessAuthenticationFilter
而不是AADAuthenticationFilter
,这不需要通过azure.activedirectory.active-directory-groups
指定组。
您要使用的角色必须在application manifest
由于目前不支持组的通配符,因此我通过忽略用户组是否有效来构建解决方法。
为此,我制作了com.microsoft.azure.spring.autoconfigure.aad.AzureADGraphClient的副本,并注释了以下代码段:
.filter(this::isValidUserGroupToGrantAuthority)
我用
复制了com.microsoft.azure.spring.autoconfigure.aad.AADOAuth2UserServicegraphClient = new MyAzureADGraphClient(...
代替
graphClient = new AzureADGraphClient(...
然后在SecurityConfiguration中,我注入了AAD属性:
@Autowired(required = false) private AADAuthenticationProperties aadAuthenticationProperties;
@Autowired(required = false) private ServiceEndpointsProperties serviceEndpointsProps;
并在void configure(HttpSecurity http)中调用了我自己的AADOAuth2UserService:
EvaAADOAuth2UserService oidcUserService = new EvaAADOAuth2UserService(aadAuthenticationProperties,serviceEndpointsProps);
httpSecurity.oauth2Login().loginPage(LOGIN_URL).permitAll().userInfoEndpoint().oidcUserService(oidcUserService);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。