Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Making the HttpSessionFilter pattern configurable #206

Open
wants to merge 12 commits into
base: 4.6.x
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import io.micronaut.session.annotation.SessionValue;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;

import io.micronaut.http.filter.FilterPatternStyle;
import java.util.List;
import java.util.Optional;

Expand All @@ -45,7 +45,8 @@
* @author Graeme Rocher
* @since 1.0
*/
@Filter("/**")

@Filter(patternStyle = FilterPatternStyle.REGEX, value = "${http.session.filter.regex-pattern:/.*}")
public class HttpSessionFilter implements HttpServerFilter {

/**
Expand All @@ -57,7 +58,7 @@ public class HttpSessionFilter implements HttpServerFilter {
* Constant for Micronaut SESSION attribute.
*/
public static final CharSequence SESSION_ATTRIBUTE = "micronaut.SESSION";

private final HttpSessionFilterConfiguration config;
private final SessionStore<Session> sessionStore;
private final HttpSessionIdResolver[] resolvers;
private final HttpSessionIdEncoder[] encoders;
Expand All @@ -68,11 +69,13 @@ public class HttpSessionFilter implements HttpServerFilter {
* @param sessionStore The session store
* @param resolvers The HTTP session id resolvers
* @param encoders The HTTP session id encoders
* @param config the configuration for the HttpSessionFilter
*/
public HttpSessionFilter(SessionStore<Session> sessionStore, HttpSessionIdResolver[] resolvers, HttpSessionIdEncoder[] encoders) {
public HttpSessionFilter(SessionStore<Session> sessionStore, HttpSessionIdResolver[] resolvers, HttpSessionIdEncoder[] encoders, HttpSessionFilterConfiguration config) {
this.sessionStore = sessionStore;
this.resolvers = resolvers;
this.encoders = encoders;
this.config = config;
}

@Override
Expand All @@ -82,6 +85,9 @@ public int getOrder() {

@Override
public Publisher<MutableHttpResponse<?>> doFilter(HttpRequest<?> request, ServerFilterChain chain) {
if (!request.getUri().getPath().matches(config.getRegexPattern())) {
return chain.proceed(request);
}
request.setAttribute(HttpSessionFilter.class.getName(), true);
try {
for (HttpSessionIdResolver resolver : resolvers) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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 {

/**
* 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;
}

/**
* 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;
}
}
Loading