From eba48ab4226e1c41a7eb4bf41b5cf542ba641fdd Mon Sep 17 00:00:00 2001 From: Telmo Brugnara Date: Thu, 23 Oct 2025 16:09:13 -0300 Subject: [PATCH] [#2308] activedirectory: added Initialization test --- .../ActiveDirectoryRealmTest.java | 20 ++++++++++++- .../AdRealm.withPrincipalSuffix.ini | 30 +++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 core/src/test/resources/org/apache/shiro/realm/activedirectory/AdRealm.withPrincipalSuffix.ini diff --git a/core/src/test/java/org/apache/shiro/realm/activedirectory/ActiveDirectoryRealmTest.java b/core/src/test/java/org/apache/shiro/realm/activedirectory/ActiveDirectoryRealmTest.java index 92fdd9f054..a4447afbdb 100644 --- a/core/src/test/java/org/apache/shiro/realm/activedirectory/ActiveDirectoryRealmTest.java +++ b/core/src/test/java/org/apache/shiro/realm/activedirectory/ActiveDirectoryRealmTest.java @@ -19,7 +19,7 @@ package org.apache.shiro.realm.activedirectory; import org.apache.shiro.SecurityUtils; - +import org.apache.shiro.UnavailableSecurityManagerException; import org.apache.shiro.authc.AuthenticationException; import org.apache.shiro.authc.AuthenticationInfo; import org.apache.shiro.authc.AuthenticationToken; @@ -28,6 +28,9 @@ import org.apache.shiro.authc.credential.CredentialsMatcher; import org.apache.shiro.authz.AuthorizationInfo; import org.apache.shiro.authz.SimpleAuthorizationInfo; +import org.apache.shiro.ini.IniSecurityManagerFactory; +import org.apache.shiro.lang.util.Factory; +import org.apache.shiro.lang.util.LifecycleUtils; import org.apache.shiro.mgt.DefaultSecurityManager; import org.apache.shiro.mgt.SecurityManager; import org.apache.shiro.realm.AuthorizingRealm; @@ -133,6 +136,21 @@ void testExistingUserSuffix() throws Exception { assertExistingUserSuffix(USERNAME + "@EXAMPLE.com", "testuser@EXAMPLE.com"); } + @Test + void testInitialization() { + try { + // Initialize AD Realm + Factory factory = new IniSecurityManagerFactory( + "classpath:org/apache/shiro/realm/activedirectory/AdRealm.withPrincipalSuffix.ini"); + SecurityUtils.setSecurityManager(factory.getInstance()); + // Destroy Realm + SecurityManager securityManager = SecurityUtils.getSecurityManager(); + LifecycleUtils.destroy(securityManager); + } catch (UnavailableSecurityManagerException e) { + } + SecurityUtils.setSecurityManager(null); + } + public void assertExistingUserSuffix(String username, String expectedPrincipalName) throws Exception { LdapContext ldapContext = createMock(LdapContext.class); diff --git a/core/src/test/resources/org/apache/shiro/realm/activedirectory/AdRealm.withPrincipalSuffix.ini b/core/src/test/resources/org/apache/shiro/realm/activedirectory/AdRealm.withPrincipalSuffix.ini new file mode 100644 index 0000000000..b43919a585 --- /dev/null +++ b/core/src/test/resources/org/apache/shiro/realm/activedirectory/AdRealm.withPrincipalSuffix.ini @@ -0,0 +1,30 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +[main] + +ldapRealm = org.apache.shiro.realm.activedirectory.ActiveDirectoryRealm + +ldapRealm.url = ldap://dc.example.com:389 +ldapRealm.systemUsername = systemuser +ldapRealm.systemPassword = systempwd + +ldapRealm.searchBase = "OU=Users,DC=example,DC=com" +ldapRealm.principalSuffix = @example.com +ldapRealm.searchFilter = (&(objectClass=*)(mail={0}))