首页 > 解决方案 > 允许 URL 中的语言参数(Spring Boot / Security)

问题描述

你好亲爱的社区,


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


实际结果:


然后我添加了安全性:

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

标签: javaspring-bootauthenticationspring-securityinternationalization

解决方案


好吧,终于它工作了。

上面我告诉过我对提到的 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();

}


推荐阅读