【问题标题】:Allow language parameter in URL (Spring Boot / Security)允许 URL 中的语言参数(Spring Boot / Security)
【发布时间】:2021-10-23 16:30:11
【问题描述】:

亲爱的社区,您好,


关于我的目标的详细信息:


实际结果:

  • 国际化效果很好

然后我添加了安全性:

http.authorizeRequests()

    // Restrict Endpoints
    .antMatchers("/login/**").hasAnyRole("admin", "member")

    // Allow Forms
    .antMatchers("/member/**").permitAll()

    // Allow Resources
    .antMatchers("/js/**").permitAll()
    .antMatchers("/css/**").permitAll()

    // Deny All
    .anyRequest().authenticated();
}

由于.anyRequest().authenticated(),像/?lang=de 这样的根路径上的请求将触发身份验证。


我尝试了什么:

http.authorizeRequests()

    // Restrict Endpoints
    .antMatchers("/login/**").hasAnyRole("admin", "member")

    // Allow Forms
    .antMatchers("/member/**").permitAll()

    // Allow Resources
    .antMatchers("/js/**").permitAll()
    .antMatchers("/css/**").permitAll()

    // Trick to allow Internationalization
    .antMatchers("/*").permitAll()

    // Deny All
    .anyRequest().authenticated();
}

我添加了.antMatchers("/*").permitAll(),它可以工作,但它允许在根路径上使用很多资源。我的目标是只允许/?lang=de 无需身份验证。

有机会吗?


我研究过但不满意的资源:


亲切的问候
OtenMoten

【问题讨论】:

    标签: java spring-boot authentication spring-security internationalization


    【解决方案1】:

    嗯,终于成功了。

    上面我告诉过我对提到的 stackoverflow 链接不满意,但它正在工作。

    我已添加:

    .regexMatchers("/.*lang=.*").permitAll()

    http.authorizeRequests()

    // Restrict Endpoints
    .antMatchers("/login/**").hasAnyRole("admin", "member")
    
    // Allow Forms
    .antMatchers("/member/**").permitAll()
    
    // Allow Resources
    .antMatchers("/js/**").permitAll()
    .antMatchers("/css/**").permitAll()
    
    .regexMatchers("/.*lang=.*").permitAll()
    
    // Deny All
    .anyRequest().authenticated();
    

    }

    【讨论】:

      猜你喜欢
      • 2015-07-22
      • 1970-01-01
      • 1970-01-01
      • 2017-11-06
      • 2019-08-10
      • 2022-10-24
      • 2019-01-13
      • 2018-11-20
      • 1970-01-01
      相关资源
      最近更新 更多