-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
abdd2c9
commit b65178d
Showing
4 changed files
with
80 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 7 additions & 50 deletions
57
session/src/main/java/io/micronaut/session/http/HttpSessionFilterConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,16 @@ | ||
/* | ||
* Copyright 2017-2024 original authors | ||
* | ||
* Licensed 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 | ||
* | ||
* https://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. | ||
*/ | ||
package io.micronaut.session.http; | ||
|
||
import io.micronaut.context.annotation.ConfigurationProperties; | ||
import io.micronaut.core.annotation.Internal; | ||
|
||
/** | ||
* Configuration properties for the HttpSessionFilter. | ||
* | ||
* This class contains settings for the session filter, including | ||
* a regex pattern to define which paths should have session handling applied. | ||
*/ | ||
@Internal | ||
@ConfigurationProperties("http.session.filter") | ||
public class HttpSessionFilterConfiguration { | ||
@ConfigurationProperties("micronaut.session.filter") | ||
public class HttpSessionFilterConfiguration { | ||
private String excludePattern = "/**"; | ||
|
||
/** | ||
* The regex pattern for filtering paths that should have session handling. | ||
*/ | ||
private String regexPattern = "/.*"; | ||
|
||
/** | ||
* Gets the regex pattern for filtering paths that should have session handling. | ||
* | ||
* Subclasses may override this method to provide a different regex pattern. | ||
* However, care should be taken to ensure that the new pattern remains compatible | ||
* with the intended usage of session handling. | ||
* | ||
* @return The regex pattern for filtering paths. | ||
*/ | ||
public String getRegexPattern() { | ||
return regexPattern; | ||
public String getExcludePattern() { | ||
return excludePattern; | ||
} | ||
|
||
/** | ||
* Sets the regex pattern for filtering paths that should have session handling. | ||
* | ||
* @param regexPattern The regex pattern to apply for filtering paths. | ||
* Default is "/.*", which matches all paths. | ||
*/ | ||
public void setRegexPattern(String regexPattern) { | ||
this.regexPattern = (regexPattern != null) ? regexPattern : "/.*"; | ||
|
||
public void setExcludePattern(String excludePattern) { | ||
this.excludePattern = excludePattern; | ||
} | ||
} |
76 changes: 59 additions & 17 deletions
76
session/src/test/java/io/micronaut/session/HttpSessionFilterConfigurationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,77 @@ | ||
|
||
package io.micronaut.session; | ||
import io.micronaut.session.http.HttpSessionFilterConfiguration; | ||
|
||
|
||
import io.micronaut.context.annotation.Property; | ||
import io.micronaut.session.http.HttpSessionFilterConfiguration; | ||
import io.micronaut.test.extensions.junit5.annotation.MicronautTest; | ||
import jakarta.inject.Inject; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
@MicronautTest | ||
class HttpSessionFilterConfigurationTest { | ||
|
||
@Inject | ||
HttpSessionFilterConfiguration configuration; | ||
|
||
@Test | ||
void testRegexPatternDefault() { | ||
HttpSessionFilterConfiguration config = new HttpSessionFilterConfiguration(); | ||
assertEquals("/.*", config.getRegexPattern(), "Default regex pattern should match all paths."); | ||
void testDefaultConfiguration() { | ||
assertEquals("/**", configuration.getExcludePattern(), | ||
"Default exclude pattern should be '/**'"); | ||
} | ||
|
||
@Test | ||
void testSetRegexPattern() { | ||
HttpSessionFilterConfiguration config = new HttpSessionFilterConfiguration(); | ||
config.setRegexPattern("/assets/.*"); | ||
assertEquals("/assets/.*", config.getRegexPattern(), "Regex pattern should be updated correctly."); | ||
@Property(name = "micronaut.session.filter.exclude-pattern", value = "/static/**,/public/**") | ||
void testMultipleExclusionPatterns() { | ||
assertEquals("/static/**,/public/**", configuration.getExcludePattern()); | ||
assertTrue(matchesExcludePattern("/static/image.jpg"), "Static path should be excluded"); | ||
assertTrue(matchesExcludePattern("/public/script.js"), "Public path should be excluded"); | ||
assertFalse(matchesExcludePattern("/api/users"), "API path shouldn't be excluded"); | ||
assertFalse(matchesExcludePattern("/login"), "Login path shouldn't be excluded"); | ||
} | ||
|
||
@Test | ||
void testSetRegexPatternToNull() { | ||
HttpSessionFilterConfiguration config = new HttpSessionFilterConfiguration(); | ||
config.setRegexPattern(null); | ||
assertEquals("/.*", config.getRegexPattern(), "If set to null, regex pattern should revert to the default."); | ||
@Property(name = "micronaut.session.filter.exclude-pattern", value = "/admin/**") | ||
void testSingleExclusionPattern() { | ||
assertEquals("/admin/**", configuration.getExcludePattern()); | ||
|
||
assertTrue(matchesExcludePattern("/admin/users"), "Admin path should be excluded"); | ||
assertTrue(matchesExcludePattern("/admin/settings"), "Admin path should be excluded"); | ||
assertFalse(matchesExcludePattern("/user/profile"), "User path shouldn't be excluded"); | ||
} | ||
|
||
@Test | ||
void testSetRegexPatternToEmptyString() { | ||
HttpSessionFilterConfiguration config = new HttpSessionFilterConfiguration(); | ||
config.setRegexPattern(""); | ||
assertEquals("", config.getRegexPattern(), "Regex pattern should accept an empty string if explicitly set."); | ||
@Property(name = "micronaut.session.filter.exclude-pattern", value = "") | ||
void testEmptyExclusionPattern() { | ||
assertEquals("", configuration.getExcludePattern()); | ||
assertFalse(matchesExcludePattern("/any/path"), "No path should match empty pattern"); | ||
} | ||
|
||
|
||
private boolean matchesExcludePattern(String path) { | ||
if (configuration.getExcludePattern() == null || configuration.getExcludePattern().isEmpty()) { | ||
return false; | ||
} | ||
|
||
String[] patterns = configuration.getExcludePattern().split(","); | ||
for (String pattern : patterns) { | ||
if (pathMatches(pattern.trim(), path)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
private boolean pathMatches(String pattern, String path) { | ||
if (pattern.isEmpty()) { | ||
return false; | ||
} | ||
|
||
String regex = "^" + pattern | ||
.replace(".", "\\.") | ||
.replace("**", ".*") | ||
.replace("*", "[^/]*") + "$"; | ||
return path.matches(regex); | ||
} | ||
} |
47 changes: 0 additions & 47 deletions
47
session/src/test/java/io/micronaut/session/MalformedSessionCookieValueTest.java
This file was deleted.
Oops, something went wrong.