简体   繁体   中英

Spring Boot2 Undertow Same Site - HttpHandler Customization - Help wanted

I ran across this url which suggests that an Http Handler can be added(Example is in Spring 1.x). https://lists.jboss.org/pipermail/undertow-dev/2017-March/001938.html

I have tried adding the following code - it does not appear to be called unless I add a listener. Unfortunately, Spring appears to have already added a listener. What would like to do is updates Spring's listener with my Http Handler. I am just not sure how to do it.

Any help is very much appreciated.

@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);

            }
        });

}

    }

Try this:

SameSiteHandler goes through all of response cookies and moves SameSite information from Comment To SameSiteMode property.

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);
    }

}

Register 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));
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM