angular - Springboot Angular安全-REST调用禁止403
问题描述
我们有一个 Springboot 2.0.x 和 Angular 6 多模块应用程序,使用 OAuth2 标准的 OpenID Connect 1.0 实现作为安全性。初始安全工作、身份验证和授权,并登陆主页。但由于某种原因,我们的 POST 和 DELETE REST 调用正在获得 403 Forbidden 状态代码,用于经过身份验证和授权的用户。GET 调用不受影响,仍然有效。
有人对此有任何想法吗?我们没有设置任何角色来过滤任何用户可以执行的操作。只是所有用户,一旦经过身份验证和授权,就可以发布、删除和获取。
这是安全配置:
@Override
public void configure(WebSecurity web) throws Exception {
System.out.println("Error!!/resources/**");
web.ignoring().antMatchers("/resources/**");
}
@Bean
public OpenIdConnectFilter myFilter() {
final OpenIdConnectFilter filter = new OpenIdConnectFilter("/auth/sso/callback");
filter.setRestTemplate(restTemplate);
return filter;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
// @formatter:off
http
.addFilterAfter(new OAuth2ClientContextFilter(), AbstractPreAuthenticatedProcessingFilter.class)
.addFilterAfter(myFilter(), OAuth2ClientContextFilter.class)
.httpBasic().authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/auth/sso/callback"))
// .httpBasic().authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/google-login"))
.and()
.authorizeRequests()
.antMatchers("/errorPage").permitAll()
.anyRequest().authenticated()
;
// @formatter:on
}
POST 签名:
@PostMapping("/spreadsheet/upload/{uploader}/{filename}")
public ResponseEntity<?> uploadSpreadsheet(@RequestBody MultipartFile file, @PathVariable("uploader") String uploader, @PathVariable("filename") String filename) {
删除签名:
@DeleteMapping("/spreadsheet/{uploader}/{filename}")
public ResponseEntity<?> deleteUploadedSpreadsheet(@PathVariable(value = "uploader") String uploader, @PathVariable String filename) {
解决方案
找到了罪魁祸首,是因为CSRF,不知道是默认配置启用的。一旦我们禁用它,通过添加,
.and().csrf().disable()
在
@Override
protected void configure(HttpSecurity http) throws Exception {
// @formatter:off
http
.addFilterAfter(new OAuth2ClientContextFilter(), AbstractPreAuthenticatedProcessingFilter.class)
.addFilterAfter(myFilter(), OAuth2ClientContextFilter.class)
.httpBasic().authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/auth/sso/callback"))
// .httpBasic().authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/google-login"))
.and()
.authorizeRequests()
.antMatchers("/errorPage").permitAll()
.anyRequest().authenticated()
.and().csrf().disable()
;
// @formatter:on
}
POST 和 DELETE 再次起作用。但当然,此解决方案正在禁用这部分安全性。但是,现在我们知道了,我们只需将 csrf 配置为在不禁止 POST 和 DELETE 的情况下为应用程序工作。
谢谢
推荐阅读
- linux - 如何将私钥作为文本传递给 ssh?
- json - 在 json 文件中设置值(使用 karate intuit 框架)
- react-native - SDK 目录 'C:\Users\ABC\AppData\Local\Android\Sdk\platform-tools;C:\Users\ABC\AppData\Local\Android\Sdk' 不存在
- sql - 使用行参数对所有行执行 SQL 函数
- c# - 无法从列表中删除重复项
- ruby-on-rails - 发送补丁请求时,无头模式缺少按钮名称选项
- php - Eclipse Oxygen 中的“生成构造函数”选项在哪里?
- ms-access - Microsoft Access 2016 - 记录数
- angularjs - AngularJs:如何知道 URL 的更改是来自地址栏还是来自组件操作
- java - gson.fromJson 实例化 pojo 时是否创建空值或空列表