允许 URL 中的语言参数(Spring Boot / Security)

Posted

技术标签:

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

亲爱的社区,您好,


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

Spring Boot 应用程序中使用 国际化 以及 Spring Security 的身份验证机制 Using Baeldung's Guide to internationalization

实际结果:

国际化效果很好

然后我添加了安全性:

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 无需身份验证。

有机会吗?


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

antMatchers Spring Security pattern with changeable URL user ID Regex doesn't match antMatcher URL pattern

亲切的问候 OtenMoten

【问题讨论】:

【参考方案1】:

嗯,终于成功了。

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

我已添加:

.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();

【讨论】:

以上是关于允许 URL 中的语言参数(Spring Boot / Security)的主要内容,如果未能解决你的问题,请参考以下文章