GH-1034 Add implementation of ProxyHttpSession, fix Spring Security interaction

This commit is contained in:
Oleg Zhurakousky
2023-05-10 07:47:46 +02:00
parent 74aaadea98
commit 05685c647c
7 changed files with 222 additions and 7 deletions

View File

@@ -446,7 +446,7 @@ public class ProxyHttpServletRequest implements HttpServletRequest {
@Override
public String getScheme() {
throw new UnsupportedOperationException();
return "https";
}
public void setServerName(String serverName) {
@@ -455,7 +455,7 @@ public class ProxyHttpServletRequest implements HttpServletRequest {
@Override
public String getServerName() {
throw new UnsupportedOperationException();
return "spring-serverless-web-proxy";
}
public void setServerPort(int serverPort) {
@@ -464,7 +464,7 @@ public class ProxyHttpServletRequest implements HttpServletRequest {
@Override
public int getServerPort() {
throw new UnsupportedOperationException();
return 0;
}
@Override
@@ -582,7 +582,7 @@ public class ProxyHttpServletRequest implements HttpServletRequest {
*/
@Override
public boolean isSecure() {
throw new UnsupportedOperationException();
return false;
}
@Override
@@ -883,7 +883,7 @@ public class ProxyHttpServletRequest implements HttpServletRequest {
@Override
public StringBuffer getRequestURL() {
throw new UnsupportedOperationException();
return new StringBuffer(this.requestURI);
}
public void setServletPath(String servletPath) {
@@ -902,6 +902,9 @@ public class ProxyHttpServletRequest implements HttpServletRequest {
@Override
@Nullable
public HttpSession getSession(boolean create) {
if (this.session == null) {
this.session = new ProxyHttpSession(this.servletContext);
}
return this.session;
}

View File

@@ -0,0 +1,112 @@
/*
* Copyright 2023-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.cloud.function.serverless.web;
import java.util.Collections;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import jakarta.servlet.ServletContext;
import jakarta.servlet.http.HttpSession;
/**
*
* @author Oleg Zhurakousky
* @since 4.x
*
*
*/
public class ProxyHttpSession implements HttpSession {
private final long creationTime;
private final String sessionId;
private final ServletContext servletContext;
private final Map<String, Object> attributes = new HashMap<>();
public ProxyHttpSession(ServletContext servletContext) {
this.creationTime = new Date().getTime();
this.sessionId = UUID.randomUUID().toString();
this.servletContext = servletContext;
}
@Override
public long getCreationTime() {
return this.creationTime;
}
@Override
public String getId() {
return this.sessionId;
}
@Override
public long getLastAccessedTime() {
return 0;
}
@Override
public ServletContext getServletContext() {
return this.servletContext;
}
@Override
public void setMaxInactiveInterval(int interval) {
}
@Override
public int getMaxInactiveInterval() {
return 0;
}
@Override
public Object getAttribute(String name) {
return this.attributes.get(name);
}
@Override
public Enumeration<String> getAttributeNames() {
return Collections.enumeration(this.attributes.keySet());
}
@Override
public void setAttribute(String name, Object value) {
this.attributes.put(name, value);
}
@Override
public void removeAttribute(String name) {
this.attributes.remove(name);
}
@Override
public void invalidate() {
}
@Override
public boolean isNew() {
return false;
}
}

View File

@@ -170,6 +170,7 @@ public class ProxyMvc {
ProxyFilterChain(DispatcherServlet servlet) {
List<Filter> filters = new ArrayList<>();
servlet.getServletContext().getFilterRegistrations().values().forEach(fr -> filters.add(((ProxyFilterRegistration) fr).getFilter()));
servlet.getWebApplicationContext().getBeansOfType(Filter.class).values().forEach(f -> filters.add(f));
Assert.notNull(filters, "filters cannot be null");
Assert.noNullElements(filters, "filters cannot contain null values");
this.filters = initFilterList(servlet, filters.toArray(new Filter[] {}));

View File

@@ -42,6 +42,8 @@ import jakarta.servlet.descriptor.JspConfigDescriptor;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.ClassUtils;
/**
* Empty no-op representation of {@link ServletContext} to satisfy required dependencies to
* successfully proxy incoming web requests to target web application.
@@ -143,7 +145,9 @@ public class ProxyServletContext implements ServletContext {
@Override
public String getServerInfo() {
return "serverless-web-proxy";
return ClassUtils.isPresent("com.amazonaws.serverless.proxy.spring.SpringLambdaContainerHandler", ClassUtils.getDefaultClassLoader())
? "aws-serverless-java-container/6.0"
: "serverless-web-proxy";
}
@Override