【问题标题】:Spring security - Specific session creation policy per matchersSpring security - 每个匹配器的特定会话创建策略
【发布时间】:2022-01-19 19:19:03
【问题描述】:

我正在尝试为 /testMVCController/** 端点实现 SessionCreationPolicy.ALWAYS,为其余端点 (/**) 实现 SessionCreationPolicy.STATELESS。

预期场景:

当访问 /testMVCController/displayUsers 时,用户登录一次,我在 UserDetailsS​​ervice 中实现的日志记录了与该用户关联的权限。 之后,所有对 /testMVCController/displayUsers 或 /testMVCController/** 下的其他 URL 的请求将不会再次登录权限,因为会话创建策略始终是并且用户已经登录。

这在我没有指定第二个安全配置 (X509ClientSessionCreationPolicyStateless) 时有效,但是当我添加它时,所有请求都变为无状态会话。

它不适用于当前的安全配置,因为在我使用客户端证书登录后,在 /testMVCController/** 端点下执行的任何请求(例如 /testMVCController/displayUsers)下,都会查询 authenticationUserDetailsS​​ervice 和权限列表记录浏览器发出的每个文件请求(.js 文件、.css 文件、...),即使在初始登录之后也是如此。

因此,如果有 3 个请求(/testMVCController/displayUsers、displayUsers.js、displayUsers.css),authenticationUserDetailsS​​ervice 中的权限日志列表将被记录 3 次。

我如下所示配置了 SecurityConfiguration 但它不起作用:

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableWebSecurity
public class SecurityConfiguration {

 @Configuration
 @Order(1)
 public static class X509ClientSessionCreationPolicyAlways extends WebSecurityConfigurerAdapter {

      @Autowired
      private X509CUDService x509CUDService;

      @Override
      protected void configure(AuthenticationManagerBuilder auth) throws Exception {
      }

      @Override
      protected void configure(HttpSecurity http) throws Exception {
           http
                .antMatcher("/testMVCController/**")
                .csrf().disable()
                .authorizeRequests()
                .anyRequest().authenticated()
                .and()
                .x509()
                .authenticationUserDetailsService(x509CUDService)
                .and()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.ALWAYS);
      }

 }

 @Configuration
 @Order(2)
 public static class X509ClientSessionCreationPolicyStateless extends WebSecurityConfigurerAdapter {

      @Autowired
      private X509CUDService X509CUDService ;

      @Override
      protected void configure(AuthenticationManagerBuilder auth) throws Exception {
      }

      @Override
      protected void configure(HttpSecurity http) throws Exception {
           http
                .antMatcher("/**")
                .csrf().disable()
                .authorizeRequests()
                .anyRequest().authenticated()
                .and()
                .x509()
                .authenticationUserDetailsService(X509CUDService);
                .and()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
      }

      @Override
      @Bean
      public AuthenticationManager authenticationManagerBean() throws Exception {
           return super.authenticationManagerBean();
      }

 }

}

我搜索了这个问题,发现了各种链接(例如 Spring session creation policy per-request?Spring Session: How to create separate session management policies for different URLsMultiple HttpSecurity),但没有一个有效。

提前致谢!

【问题讨论】:

  • 你的意思是什么'Spring总是访问主要权限(显示在日志中)你的日志在哪里?
  • 每次发出请求时都会咨询 authenticationUserDetailsS​​ervice(我正在那里记录权限列表)。在日志中,我每次点击端点时都会看到此列表。但是使用 Always 值的会话创建策略,这个列表应该只记录一次(我第一次选择客户端证书并执行登录操作)。
  • Enable DEBUG log for AbstractPreAuthenticatedProcessingFilter 然后你会看到原因。

标签: spring spring-boot spring-mvc spring-security spring-session


【解决方案1】:

我遗漏了一些关于我的配置的细节。我正在捕获对/testMVCController/** 的所有请求,这很有效,但是除了捕获对/testMVCController/** 类型的任何端点的请求(例如:/testMVCController/usersList)之外,我还必须捕获这些页面的请求make 获取他们的脚本(.js 文件、.css 文件、.png 文件)。 发生的情况是:对/testMVCController/usersList) 的请求被配置为SessionCreationPolicy.ALWAYS,但随后的请求如usersList.jsusersList.css 等被配置为SessionCreationPolicy.STATELESS,在这些情况下X509CustomUserDetailsService总是被咨询。

示例: 对/testMVCController/usersList 的GET 请求有效,但在此usersList 页面中也有对usersList.jsusersList.css 等的请求。

所以,一旦我将这些资源路径包含在 antMatchers 中,一切都会完美运行。

【讨论】:

    猜你喜欢
    • 2017-09-17
    • 2021-05-28
    • 2020-02-22
    • 2019-05-08
    • 2015-06-13
    • 2018-08-12
    • 2018-11-21
    • 1970-01-01
    • 2019-06-11
    相关资源
    最近更新 更多