Kus*_*mal 6 spring spring-security spring-boot
我在我的项目中使用 spring security。我有一个条件,匿名用户应该能够从数据库中读取,而只有授权用户才能添加/更新/删除。我们如何在安全配置中提及这种情况?
.antMatchers("/user/**").permitAll()
Run Code Online (Sandbox Code Playgroud)
permit all 需要经过身份验证,但我什至希望没有经过身份验证的用户通过 GET 方法访问。
@RequestMapping("/user")
@PreAuthorize("hasAuthority('USER')")
public List<UserAll> getAll() {
return userService.getAll();
}
Run Code Online (Sandbox Code Playgroud)
在这里我如何提到匿名用户也应该访问此功能?
小智 6
在我的 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();
Run Code Online (Sandbox Code Playgroud)
它的作用是只允许未经身份验证的用户使用注册和登录端点。它只允许经过身份验证的用户访问其他端点(以 /secure/rest 开头的端点。它还允许经过身份验证和未经身份验证的用户使用我的 Swagger 端点。
permitAll 不需要对用户进行身份验证。它允许所有请求通过。
作为旁注,我建议每个环境使用不同的安全配置。我不建议在生产环境中向所有人公开 Swagger 端点。以上配置适用于我的较低开发环境。
| 归档时间: |
|
| 查看次数: |
5751 次 |
| 最近记录: |