How to enable samesite for jsessionid cookie

后端 未结 4 1765
时光说笑
时光说笑 2020-12-15 21:32

How can I enable samesite for my web application which runs on wildfly as. Checked standalone.xml however could not find an appropriate tag within



        
4条回答
  •  青春惊慌失措
    2020-12-15 21:57

    My workaround, which works in JBoss EAP 7.2, is a custom handler. I use it as a global handler. But you can also use it in the jboss-web.xml. You need to play with the cookie implementation because undertow only allows Strict or Lax for samesite (it throws '"UT000162: Same-site attribute None is invalid. It must be Strict or Lax"' if you use cookie.setSameSiteMode("None"))

    import io.undertow.server.HttpHandler;
    import io.undertow.server.HttpServerExchange;
    import io.undertow.server.handlers.Cookie;
    import java.lang.reflect.Proxy;
    import java.util.Map;
    
    public class CookieSameSiteHandler implements HttpHandler
    {
       private  HttpHandler next;
    
       public CookieSameSiteHandler(HttpHandler next){
          this.next = next;
       }
    
       @Override
       public void handleRequest(final HttpServerExchange exchange)
          throws Exception
       {
          exchange.addResponseCommitListener(serverExchange -> {
             for (Map.Entry responcecookie : serverExchange.getResponseCookies().entrySet()){
                serverExchange.getResponseCookies().replace(responcecookie.getKey(), proxyCookie(responcecookie.getValue()));
             }
          });
          next.handleRequest(exchange);
       }
    
       private Cookie proxyCookie(Cookie cookie)
       {
          return (Cookie)Proxy.newProxyInstance(
             cookie.getClass().getClassLoader(),
             cookie.getClass().getInterfaces(),
             (proxy, method, args) -> {
                if ("isSameSite".equals(method.getName())){
                   return true;
                }
                if ("getSameSiteMode".equals(method.getName()) && cookie.getSameSiteMode() == null){
                   return "None";
                }
                if ("isSecure".equals(method.getName()) && cookie.getSameSiteMode() == null){
                   return true;
                }
                return method.invoke(cookie, args);
             });
       }
    }
    

    handler configuration:

    
        
        
            ...
            
                ...
                
                ...
            
        
        ...
        
            
        
    
    

提交回复
热议问题