Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions web/src/main/java/org/apache/shiro/web/env/IniWebEnvironment.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
* <p>
* 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.
* <p>
* <strong>NOTE:</strong> 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:
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Loading