1

我遇到了这个 url,它表明可以添加一个 Http Handler(示例在 Spring 1.x 中)。 https://lists.jboss.org/pipermail/undertow-dev/2017-March/001938.html

我尝试添加以下代码 - 除非我添加侦听器,否则它似乎不会被调用。不幸的是,Spring 似乎已经添加了一个监听器。想要做的是用我的 Http 处理程序更新 Spring 的侦听器。我只是不知道该怎么做。

很感谢任何形式的帮助。

@Component
@Slf4j
public class LibCoreEmbeddedServletCustomerizer implements WebServerFactoryCustomizer<UndertowServletWebServerFactory> {


    @Value("${same.site.string}")
    private String sameSiteString;


    @Value("${server.port}")
    private int serverPort;

    @Value("${server.address}")
    private String serverAddress;

    @Override
    public void customize(UndertowServletWebServerFactory factory) {

        factory.addBuilderCustomizers(new UndertowBuilderCustomizer() {
            @Override
            public void customize(Undertow.Builder builder) {
                log.debug("LibCoreEmbeddedServletCustomerizer::customize");
                UndertowBuilderCustomizer customizer = new UndertowBuilderCustomizer() {
                    @Override
                    public void customize(Undertow.Builder builder) {
                        builder.
                        //addHttpListener(serverPort, serverAddress)
                        setHandler(new HttpHandler() {
                            @Override
                            public void handleRequest(HttpServerExchange httpServerExchange) throws Exception {
                                Map<String, Cookie> cookies = httpServerExchange.getResponseCookies();

                                log.debug(Encode.log(String.format("UndertowServletWebServerFactory handleRequest sameSiteString=%s", sameSiteString)));
                                for (Cookie cookie:cookies.values()) {
                                    log.debug(Encode.log(String.format("UndertowServletWebServerFactory handleRequest cookie=%s", cookie)));
                                    cookie.setSameSiteMode(sameSiteString);
                                }
                            }
                        });
                    }
                };
                factory.addBuilderCustomizers(customizer);

            }
        });

}

    }
4

1 回答 1

0

尝试这个:

SameSiteHandler 遍历所有响应 cookie 并将 SameSite 信息从 Comment 移动到 SameSiteMode 属性。

import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;
import io.undertow.server.handlers.Cookie;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class SameSiteHandler implements HttpHandler {

    private final HttpHandler nextHttpHandler;

    @Override
    public void handleRequest(HttpServerExchange httpHandlerExchange) throws Exception {

        httpHandlerExchange.addResponseCommitListener(exchange -> {
            var cookies = exchange.getResponseCookies();
            if (cookies != null) {
                cookies.forEach((name, cookie) -> fix(cookie)));
            }
        });
        nextHttpHandler.handleRequest(httpHandlerExchange);
    }

    /** Moves SameSite value from Comment to SameSiteMode */
    private void fix(Cookie cookie) {
        if (cookie == null) {
            return;
        }
        var comment = cookie.getComment();
        cookie.setComment(null);
        cookie.setSameSiteMode(comment);
    }

}

注册 SameSiteHandler

import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SameSiteHandlerConfig implements WebServerFactoryCustomizer<UndertowServletWebServerFactory> {

    @Override
    public void customize(UndertowServletWebServerFactory factory) {
        factory.addDeploymentInfoCustomizers(deploymentInfo ->
                deploymentInfo.addInitialHandlerChainWrapper(SameSiteHandler::new));
    }
}
于 2021-02-17T16:12:55.617 回答