首页 > 解决方案 > 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) {

标签: angularspring-bootspring-securityoauth-2.0openid-connect

解决方案


找到了罪魁祸首,是因为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 的情况下为应用程序工作。

谢谢


推荐阅读