ST-*_*DDT 10 java cookies spring nginx spring-mvc
我有一个在nginx后面的tomcat应用程序服务器.SSL终止于nginx.部署在tomcat上的Spring web-mvc应用程序应该在JSESSIONID上设置安全标志.如果spring有一些自动检测功能会很酷,所以我不会在开发过程中受到打扰,因为我没有SSL.
有没有办法告诉spring自动设置标志?
我使用JavaConfig来设置应用程序并使用Maven来创建可部署的war文件.
我已经检查了这个,但这看起来有些丑陋和静态: 将'secure'标志设置为JSESSION id cookie
小智 12
如果您使用的是 Spring Boot,有一个简单的解决方案。只需在您的 中设置以下属性application.properties
:
server.servlet.session.cookie.secure=true
Run Code Online (Sandbox Code Playgroud)
如果您有一些使用 HTTPS 的环境而一些没有使用它,则需要在没有 HTTPS 的配置文件中将其设置为 false。否则,安全 cookie 将被忽略。
Ste*_*com 11
当你使用spring-session时,例如在reddis中保存你的会话,这确实是自动完成的.该cookie比创建通过org.springframework.session.web.http.CookieHttpSessionStrategy
在其中CookieHttpSessionStrategy#createSessionCookie
检查是否请求来自通过HTTPS和集相应地确保:
sessionCookie.setSecure(request.isSecure());
Run Code Online (Sandbox Code Playgroud)
如果您不使用spring-session,则可以使用a配置安全cookie ServletContextInitializer
.使用应用程序属性,根据配置文件将其设置为true/false.
@Bean
public ServletContextInitializer servletContextInitializer(@Value("${secure.cookie}") boolean secure) {
return new ServletContextInitializer() {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
servletContext.getSessionCookieConfig().setSecure(secure);
}
};
}
Run Code Online (Sandbox Code Playgroud)
application.properties(在配置文件'prod'未激活时用于dev):
secure.cookie=false
Run Code Online (Sandbox Code Playgroud)
application-prod.properties(仅在配置文件'prod'处于活动状态时使用,覆盖application.properties中的值):
secure.cookie=false
Run Code Online (Sandbox Code Playgroud)
使用以下命令在prod服务器上启动应用程序:
--spring.profiles.active=prod
Run Code Online (Sandbox Code Playgroud)
听起来有些努力,如果你到目前为止还没有使用过配置文件,但无论如何你很可能需要一个prod环境的配置文件,所以它真的很值得.
归档时间: |
|
查看次数: |
22501 次 |
最近记录: |