我正在尝试运行Spring Boot Web应用程序。
应用程序类如下:
@SpringBootApplication
public class MyWebApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
System.setProperty("spring.profiles.active", "dev");
SpringApplication.run(MyWebApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(MyWebApplication.class);
}
@Configuration
@ConditionalOnWebApplication
public static class WebConfiguration {
@Bean
public ServletListenerRegistrationBean<ServletContextListener> registerClientCookieConfigListener () {
ServletListenerRegistrationBean<ServletContextListener> srb =
new ServletListenerRegistrationBean<>();
srb.setListener(new MyCookieConfigListener());
return srb;
}
// ... other servlets and filters registered by ServletRegistrationBean or FilterRegistrationBean
}
}
和MyCookieConfigListener
内部:
@WebListener
public class MyCookieConfigListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
final SessionCookieConfig cookieConfig = sce.getServletContext().getSessionCookieConfig();
cookieConfig.setHttpOnly(true);
cookieConfig.setMaxAge(-1);
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
}
}
MyCookieListener
也在web.xml
中定义
启动应用程序时:
java.lang.UnsupportedOperationException: Section 4.4 of the Servlet 3.0 specification does not permit this method to be called from a ServletContextListener that was not defined in web.xml, a web-fragment.xml file nor annotated with @WebListener
at org.apache.catalina.core.StandardContext$NoPluggabilityServletContext.getSessionCookieConfig(StandardContext.java:6665)
at MyCookieConfigListener.contextInitialized(MyCookieConfigListener.java:20)
这发生在这一行:
final SessionCookieConfig scc=sce.getServletContext().getSessionCookieConfig();
在web.xml中声明了侦听器,它也被注释为@Listener
。
有没有人遇到这个问题并知道解决方案?