spring-boot - 在使用 OAuth2 保护应用程序的同时,我想公开一些可供任何人访问的 URL
问题描述
在我创建了一个小型Spring Boot 2.2.6应用程序并将AWS Cognito配置为身份验证提供程序后,一切正常。访问任何应用程序的 URL 时,我会被重定向到 Cognito,登录后,应用程序运行良好。
我尝试添加一些不需要任何身份验证的公共页面(/api/**)。首先我尝试了这个:
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/api/**").permitAll(); // This should be permitted for anyone
但是,现在,一切都是开放的。一点安全感都没有。哎呀。
我将其更改为:
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/api/**").permitAll() // This should be permitted for anyone
.anyRequest().authenticated(); // Everything else should be protected
现在,列入白名单的 URL (/api/**) 运行良好,无需密码。但是所有其他 URL(例如 /private)不会将我重定向到登录页面,而是会产生 403 错误:
出现意外错误(类型=禁止,状态=403)。拒绝访问
有谁知道如何保持原始行为(密码)但很少有匿名访问的 URL?
解决方案
有WebSecurityConfigurerAdapter
另一种方法可以用来忽略某些 url:
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/api/**");
}
推荐阅读
- android - 如何以编程方式在键盘上设置 imeOptions
- java - Spring Boot Config Server 添加 Vault,无法取回 propertySources
- javascript - 使用ajax在两个php函数之间发送数据
- java - 更改覆盖方法的返回类型?
- python - 在 python3 版本上使用自定义模块导入错误
- java - 单链表询问用户 n 节点的大小并在 java 中的每个节点中放置一个元素
- iis - 3 个文件资源上的 Blazor .net 5 完整性错误被阻止
- laravel - 上传的图像存储在 Laravel Spatie Medialibrary 的单个图像文件夹中
- architecture - 如何设计分布式写入密集型数据存储
- docker - 使用 Docker Compose 运行 Google Pubsub 模拟器会导致随机行为