gpt4 book ai didi

spring - 如何允许匿名用户仅使用 spring security 访问某个功能

转载 作者:行者123 更新时间:2023-12-03 16:24:40 24 4
gpt4 key购买 nike

我在我的项目中使用 spring security。
我有一个条件,匿名用户应该能够从数据库中读取,而只有授权用户才能添加/更新/删除。
我们如何在安全配置中提及这种情况?

.antMatchers("/user/**").permitAll()

permit all 需要经过身份验证,但我什至希望没有经过身份验证的用户通过 GET 方法访问。
@RequestMapping("/user")
@PreAuthorize("hasAuthority('USER')")
public List<UserAll> getAll() {

return userService.getAll();
}

在这里我如何提到匿名用户也应该访问此功能?

最佳答案

在我的 WebSecurityConfig 类中,我使用了这个:

    .authorizeRequests()
.antMatchers("/v2/api-docs", "/configuration/ui", "/swagger-resources", "/configuration/security", "/swagger-ui.html", "/webjars/**")
.permitAll()
.antMatchers("/secure/rest/**")
.authenticated()
.antMatchers("/register**")
.anonymous()
.antMatchers("/login**")
.anonymous()
.and();

它的作用是只允许未经身份验证的用户使用注册和登录端点。它只允许经过身份验证的用户访问其他端点(以/secure/rest 开头的端点)。
它还允许经过身份验证和未经身份验证的用户使用我的 Swagger 端点。

permitAll 不需要对用户进行身份验证。它允许所有请求通过。

作为旁注,我建议每个环境使用不同的安全配置。我不建议在生产环境中向所有人公开 Swagger 端点。以上配置适用于我的较低开发环境。

关于spring - 如何允许匿名用户仅使用 spring security 访问某个功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51129045/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com