diff --git a/web/src/main/java/org/apache/shiro/web/env/IniWebEnvironment.java b/web/src/main/java/org/apache/shiro/web/env/IniWebEnvironment.java index 8f048767b1..3b77000679 100644 --- a/web/src/main/java/org/apache/shiro/web/env/IniWebEnvironment.java +++ b/web/src/main/java/org/apache/shiro/web/env/IniWebEnvironment.java @@ -41,6 +41,8 @@ import java.io.InputStream; import java.util.HashMap; import java.util.Map; +import java.util.Optional; +import java.util.stream.Stream; /** * {@link WebEnvironment} implementation configured by an {@link Ini} instance or {@code Ini} resource locations. @@ -152,6 +154,10 @@ protected void configure() { * Extension point to allow subclasses to provide an {@link Ini} configuration that will be merged into the * users configuration. The users configuration will override anything set here. *
+ * In {@code [main]}, the framework's definitions come first, so the user's configuration can refer to them. + * In {@code [urls]}, the user's rules come first, because the first matching rule wins. + * A user catch-all rule such as {@code /** = authc} therefore hides the framework's rules. + *
* NOTE: Framework developers should use with caution. It is possible a user could provide * configuration that would conflict with the frameworks configuration. For example: if this method returns an * Ini object with the following configuration: @@ -226,6 +232,13 @@ protected Ini mergeIni(Ini ini1, Ini ini2) { Ini iniResult = new Ini(ini1); iniResult.merge(ini2); + // The first matching [urls] rule wins, so the rules of ini2 go first, followed by the rules only ini1 has. + Optional.ofNullable(ini2.getSection(IniFilterChainResolverFactory.URLS)).ifPresent(userUrls -> { + Ini.Section merged = iniResult.removeSection(IniFilterChainResolverFactory.URLS); + Stream.concat(userUrls.keySet().stream(), merged.keySet().stream()).distinct().forEach(path -> + iniResult.setSectionProperty(IniFilterChainResolverFactory.URLS, path, merged.get(path))); + }); + return iniResult; } diff --git a/web/src/test/groovy/org/apache/shiro/web/env/IniWebEnvironmentTest.groovy b/web/src/test/groovy/org/apache/shiro/web/env/IniWebEnvironmentTest.groovy index 173034e4ac..5fbebb4d3f 100644 --- a/web/src/test/groovy/org/apache/shiro/web/env/IniWebEnvironmentTest.groovy +++ b/web/src/test/groovy/org/apache/shiro/web/env/IniWebEnvironmentTest.groovy @@ -99,6 +99,38 @@ class IniWebEnvironmentTest { assertSame(compositeBean.simpleBean, simpleBean) } + @Test + void testUserUrlsTakePrecedenceOverFrameworkUrls() { + + def ini = new Ini() + ini.load(""" + [urls] + /admin/** = authc + /** = authcBasic + """) + + def env = new IniWebEnvironment() { + @Override + protected Ini getFrameworkIni() { + def frameworkIni = new Ini() + frameworkIni.load(""" + [urls] + /** = anon + /framework/** = anon + """) + return frameworkIni + } + } + env.ini = ini + env.init() + + def urls = env.ini.getSection("urls") + assertEquals(["/admin/**", "/**", "/framework/**"], urls.keySet() as List) + assertEquals("authcBasic", urls.get("/**")) + assertEquals(["/admin/**", "/**", "/framework/**"], + env.filterChainResolver.filterChainManager.chainNames as List) + } + @Test void testDisableGlobalFilters() { Ini ini = new Ini()