Add HandlerMappingIntrospector Caching
Closes gh-14128
This commit is contained in:
+34
-2
@@ -40,6 +40,8 @@ public final class AuthorizationManagerWebInvocationPrivilegeEvaluator
|
||||
|
||||
private ServletContext servletContext;
|
||||
|
||||
private HttpServletRequestTransformer requestTransformer = HttpServletRequestTransformer.IDENTITY;
|
||||
|
||||
public AuthorizationManagerWebInvocationPrivilegeEvaluator(
|
||||
AuthorizationManager<HttpServletRequest> authorizationManager) {
|
||||
Assert.notNull(authorizationManager, "authorizationManager cannot be null");
|
||||
@@ -54,8 +56,8 @@ public final class AuthorizationManagerWebInvocationPrivilegeEvaluator
|
||||
@Override
|
||||
public boolean isAllowed(String contextPath, String uri, String method, Authentication authentication) {
|
||||
FilterInvocation filterInvocation = new FilterInvocation(contextPath, uri, method, this.servletContext);
|
||||
AuthorizationDecision decision = this.authorizationManager.check(() -> authentication,
|
||||
filterInvocation.getHttpRequest());
|
||||
HttpServletRequest httpRequest = this.requestTransformer.transform(filterInvocation.getHttpRequest());
|
||||
AuthorizationDecision decision = this.authorizationManager.check(() -> authentication, httpRequest);
|
||||
return decision == null || decision.isGranted();
|
||||
}
|
||||
|
||||
@@ -64,4 +66,34 @@ public final class AuthorizationManagerWebInvocationPrivilegeEvaluator
|
||||
this.servletContext = servletContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a {@link HttpServletRequestTransformer} to be used prior to passing to the
|
||||
* {@link AuthorizationManager}.
|
||||
* @param requestTransformer the {@link HttpServletRequestTransformer} to use.
|
||||
*/
|
||||
public void setRequestTransformer(HttpServletRequestTransformer requestTransformer) {
|
||||
Assert.notNull(requestTransformer, "requestTransformer cannot be null");
|
||||
this.requestTransformer = requestTransformer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to transform the {@link HttpServletRequest} prior to passing it into the
|
||||
* {@link AuthorizationManager}.
|
||||
*/
|
||||
public interface HttpServletRequestTransformer {
|
||||
|
||||
HttpServletRequestTransformer IDENTITY = (request) -> request;
|
||||
|
||||
/**
|
||||
* Return the {@link HttpServletRequest} that is passed into the
|
||||
* {@link AuthorizationManager}
|
||||
* @param request the {@link HttpServletRequest} created by the
|
||||
* {@link WebInvocationPrivilegeEvaluator}
|
||||
* @return the {@link HttpServletRequest} that is passed into the
|
||||
* {@link AuthorizationManager}
|
||||
*/
|
||||
HttpServletRequest transform(HttpServletRequest request);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+96
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
* Copyright 2002-2023 the original author or 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 org.springframework.security.web.access;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import jakarta.servlet.DispatcherType;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletRequestWrapper;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.web.servlet.handler.HandlerMappingIntrospector;
|
||||
|
||||
/**
|
||||
* Transforms by passing it into
|
||||
* {@link HandlerMappingIntrospector#setCache(HttpServletRequest)}. Before, it wraps the
|
||||
* {@link HttpServletRequest} to ensure that the methods needed work since some methods by
|
||||
* default throw {@link UnsupportedOperationException}.
|
||||
*
|
||||
* @author Rob Winch
|
||||
*/
|
||||
public class HandlerMappingIntrospectorRequestTransformer
|
||||
implements AuthorizationManagerWebInvocationPrivilegeEvaluator.HttpServletRequestTransformer {
|
||||
|
||||
private final HandlerMappingIntrospector introspector;
|
||||
|
||||
public HandlerMappingIntrospectorRequestTransformer(HandlerMappingIntrospector introspector) {
|
||||
Assert.notNull(introspector, "introspector canot be null");
|
||||
this.introspector = introspector;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpServletRequest transform(HttpServletRequest request) {
|
||||
CacheableRequestWrapper cacheableRequest = new CacheableRequestWrapper(request);
|
||||
this.introspector.setCache(cacheableRequest);
|
||||
return cacheableRequest;
|
||||
}
|
||||
|
||||
static final class CacheableRequestWrapper extends HttpServletRequestWrapper {
|
||||
|
||||
private final Map<String, Object> attributes = new HashMap<>();
|
||||
|
||||
/**
|
||||
* Constructs a request object wrapping the given request.
|
||||
* @param request the {@link HttpServletRequest} to be wrapped.
|
||||
* @throws IllegalArgumentException if the request is null
|
||||
*/
|
||||
CacheableRequestWrapper(HttpServletRequest request) {
|
||||
super(request);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DispatcherType getDispatcherType() {
|
||||
return DispatcherType.REQUEST;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Enumeration<String> getAttributeNames() {
|
||||
return Collections.enumeration(this.attributes.keySet());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getAttribute(String name) {
|
||||
return this.attributes.get(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAttribute(String name, Object o) {
|
||||
this.attributes.put(name, o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeAttribute(String name) {
|
||||
this.attributes.remove(name);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user