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

cld4siwp  于 5个月前  发布在  其他
关注(0)|答案(1)|浏览(46)

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

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

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

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

   @Override
   protected void configure(HttpSecurity http) throws Exception {
      // CSRF feature only
      http.authorizeRequests().anyRequest().permitAll();
   }

}

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

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

<session-config> 
   <tracking-mode>COOKIE</tracking-mode> 
</session-config>

@Configuration
public class WebConfig implements WebApplicationInitializer {

   @Override
   public void onStartup(ServletContext servletContext) {
      HashSet<SessionTrackingMode> set = new HashSet<>();
      set.add(SessionTrackingMode.COOKIE);
      servletContext.setSessionTrackingModes(set);
   }

}


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

@Bean
   public HttpFirewall allowUrlSemicolonHttpFirewall() {
      StrictHttpFirewall firewall = new StrictHttpFirewall();
      firewall.setAllowSemicolon(true);
      return firewall;
   }

   @Override
   public void configure(WebSecurity web) throws Exception {
      super.configure(web);
      web.httpFirewall(allowUrlSemicolonHttpFirewall());
   }


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

问题:

  • 有人见过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属性。

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

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
@EnableWebSecurity(debug = true)
@ConditionalOnExpression("${permit.all:true}")
public class SecurityConfig {

    private static final Logger LOGGER = LoggerFactory.getLogger(SecurityConfig.class);

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http.authorizeHttpRequests(authorize -> authorize.antMatchers("/**").permitAll())
                .csrf(httpSecurityCsrfConfigurer -> httpSecurityCsrfConfigurer.ignoringAntMatchers("/**"))
                .sessionManagement().enableSessionUrlRewriting(true)
                .sessionCreationPolicy(SessionCreationPolicy.ALWAYS);

        LOGGER.debug("Default {} loaded", SecurityConfig.class.getSimpleName());
        return http.build();
    }

    @Bean
    public WebSecurityCustomizer webSecurityCustomizer() {
        DefaultHttpFirewall firewall = new DefaultHttpFirewall();
        return web -> web.httpFirewall(firewall);
    }

}

相关问题