无法删除;部署到Tomcat 8.5时Sping Boot / Web Flow应用程序URL中的jsessionid

cld4siwp  于 2024-01-08  发布在  其他
关注(0)|答案(1)|浏览(229)

我正在开发一个Java应用程序,其中用户为他/她的帐户注册密码。下面是正在使用的:

  • Spring Boot
  • Spring MVC
  • Spring Web Flow
  • Spring Security
  • Thymeleaf
  • 拦截器(用于检查preHandle方法中的会话)

对于Spring Security部分,真的不需要身份验证。我只是用它来处理CSRF,配置如下:

  1. @Configuration
  2. @EnableWebSecurity
  3. @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
  4. public class SecurityConfig extends WebSecurityConfigurerAdapter {
  5. @Override
  6. protected void configure(HttpSecurity http) throws Exception {
  7. // CSRF feature only
  8. http.authorizeRequests().anyRequest().permitAll();
  9. }
  10. }

字符串
现在,这就是事情变得混乱的地方。当我在Unix环境中将其部署到Tomcat时,;jsessionid会被附加到URL中,Spring Security并不高兴。我在互联网上搜索并找到了以下解决方案来删除它(以及我的结果)。

**application.properties**中的server.servlet.session.tracking-modes=cookie不执行任何操作。
web.xml

  1. <session-config>
  2. <tracking-mode>COOKIE</tracking-mode>
  3. </session-config>

  1. @Configuration
  2. public class WebConfig implements WebApplicationInitializer {
  3. @Override
  4. public void onStartup(ServletContext servletContext) {
  5. HashSet<SessionTrackingMode> set = new HashSet<>();
  6. set.add(SessionTrackingMode.COOKIE);
  7. servletContext.setSessionTrackingModes(set);
  8. }
  9. }


生成IllegalArgumentException: The session tracking mode [COOKIE] requested for context [/<context-name>] is not supported by that context
我正要把剩下的头发拔下来,所以我恢复了任何与cookie相关的更改,并想到在同一个SecurityConfig类中使用下面的代码片段,只允许URL中的后缀(我知道,我知道,不安全)。

  1. @Bean
  2. public HttpFirewall allowUrlSemicolonHttpFirewall() {
  3. StrictHttpFirewall firewall = new StrictHttpFirewall();
  4. firewall.setAllowSemicolon(true);
  5. return firewall;
  6. }
  7. @Override
  8. public void configure(WebSecurity web) throws Exception {
  9. super.configure(web);
  10. web.httpFirewall(allowUrlSemicolonHttpFirewall());
  11. }


瞧!网络流在无限重定向上运行。

问题:

  • 有人见过IllegalArgumentException: The session tracking mode [COOKIE] requested for context [/<context-name>] is not supported by that context吗?我到处找过,最接近的是this
  • server.servlet.session.tracking-modes=cookie不工作的原因可能和上面的一样吗?
  • 无限重定向是否是由http.authorizeRequests().anyRequest().permitAll()引起的?我尝试使用anonymous(),但结果是一样的。
  • 有没有可能知道到底是哪一部分导致了无限重定向?

请注意,在我的localhost中,允许URL中的重定向可以正常工作,所以我有一种预感,导致重定向的原因是SSL相关的。同样,本地;jsessionid没有被附加到URL中。
我的下一步是尝试在本地配置SSL,试图复制这个问题。与此同时,任何帮助都将受到高度赞赏。如果这里有太多的信息,我很抱歉;如果有必要,我愿意将其作为多个问题重新发布。

fafcakar

fafcakar1#

今年,我将其作为进一步的开发,实际上导致了我的一个队友的解决方案,当时他将基础代码扩展到一个需要Spring Security的新应用程序。原来我们案例中的持久性jsessionid是由于我们的Tomcat context.xml中下面的cookies属性。

  1. <Context useHttpOnly="true" cookies="false" reloadable="false">

字符串
在Java方面,下面的通用安全配置(用于原始应用程序)甚至可以与上面的Tomcat配置一起工作。

  1. import org.slf4j.Logger;
  2. import org.slf4j.LoggerFactory;
  3. import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  7. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  8. import org.springframework.security.config.http.SessionCreationPolicy;
  9. import org.springframework.security.web.SecurityFilterChain;
  10. @Configuration
  11. @EnableWebSecurity(debug = true)
  12. @ConditionalOnExpression("${permit.all:true}")
  13. public class SecurityConfig {
  14. private static final Logger LOGGER = LoggerFactory.getLogger(SecurityConfig.class);
  15. @Bean
  16. public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
  17. http.authorizeHttpRequests(authorize -> authorize.antMatchers("/**").permitAll())
  18. .csrf(httpSecurityCsrfConfigurer -> httpSecurityCsrfConfigurer.ignoringAntMatchers("/**"))
  19. .sessionManagement().enableSessionUrlRewriting(true)
  20. .sessionCreationPolicy(SessionCreationPolicy.ALWAYS);
  21. LOGGER.debug("Default {} loaded", SecurityConfig.class.getSimpleName());
  22. return http.build();
  23. }
  24. @Bean
  25. public WebSecurityCustomizer webSecurityCustomizer() {
  26. DefaultHttpFirewall firewall = new DefaultHttpFirewall();
  27. return web -> web.httpFirewall(firewall);
  28. }
  29. }

展开查看全部

相关问题