2016-12-27 4 views
1

Ich benutze Spring Boot 1.4.2 (mit eingebetteten Tomcat), Spring MVC 4.3.4 und Spring Security 4.1.3, um eine einfache Website zu erstellen. Meine Login-Seite enthält eine Checkbox, mit der Sie festlegen können, ob ein Cookie zum Speichern von Cookies von Spring Security erstellt wird.Add Ablauf zu Spring Security erinnern mich Cookie

Dieser Cookie ist in Chrome und Firefox perfekt eingestellt, aber IE und MS Edge verwenden das Max-Age Attribut nicht, das nur einen Sitzungscookie erstellt. Gibt es eine Möglichkeit, Spring Security (zusätzlich zu Max-Age) das Expire Attribut auf dem Set-Cookie Header für den Remember Me Cookie zu setzen?

Unten ist mein Spring Security-Konfiguration:

http.authorizeRequests() 
      .antMatchers("/admin/**").hasRole("ADMIN") 
      .antMatchers("/private/**").authenticated() 
      .anyRequest().permitAll() 
      .and() 
     .httpBasic() 
      .and() 
     .formLogin() 
      .loginPage("/login").permitAll().and() 
     .rememberMe() 
      .tokenValiditySeconds(365 * 24 * 60 * 60) 
      .and() 
     .sessionManagement() 
      .sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED); 

Antwort

1

Zusätzlich zu Max-Age Sie auch Expires Attribut hinzufügen können.

Der Konfigurationsteil des Behälters ist, siehe Apache Tomcat 8 Configuration Reference:

alwaysAddExpires

If this is true Tomcat will always add an expires parameter to a SetCookie header even for cookies with version greater than zero. This is to work around a known IE6 and IE7 bug that causes I to ignore the Max-Age parameter in a SetCookie header.

If org.apache.catalina.STRICT_SERVLET_COMPLIANCE is set to true, the default of this setting will be false, else the default value will be true.

Aber es gibt keine Common application properties für Expires im Frühjahr Stiefel. So müssen Sie die CookieProcessor-LegacyCookieProcessor ändern und konfigurieren, siehe Spring Boot Reference Guide:

70.10 Use Tomcat’s LegacyCookieProcessor

The embedded Tomcat used by Spring Boot does not support "Version 0" of the Cookie format out of the box, and you may see the following error:

java.lang.IllegalArgumentException: An invalid character [32] was present in the Cookie value 

If at all possible, you should consider updating your code to only store values compliant with later Cookie specifications. If, however, you’re unable to change the way that cookies are written, you can instead configure Tomcat to use a LegacyCookieProcessor . To switch to the LegacyCookieProcessor use an EmbeddedServletContainerCustomizer bean that adds a TomcatContextCustomizer :

@Bean 
public EmbeddedServletContainerCustomizer cookieProcessorCustomizer() { 
    return new EmbeddedServletContainerCustomizer() { 

     @Override 
     public void customize(ConfigurableEmbeddedServletContainer container) { 
      if (container instanceof TomcatEmbeddedServletContainerFactory) { 
       ((TomcatEmbeddedServletContainerFactory) container) 
         .addContextCustomizers(new TomcatContextCustomizer() { 

        @Override 
        public void customize(Context context) { 
         context.setCookieProcessor(new LegacyCookieProcessor()); 
        } 

       }); 
      } 
     } 
    }; 
} 
Verwandte Themen