【问题标题】:Springboot Angular security - 403 forbidden on REST callsSpringboot Angular安全-REST调用禁止403
【发布时间】:2019-02-21 19:04:12
【问题描述】:

我们有一个 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) {

【问题讨论】:

    标签: angular spring-boot spring-security oauth-2.0 openid-connect


    【解决方案1】:

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

    谢谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-27
      • 1970-01-01
      • 2020-04-20
      • 2018-03-11
      • 1970-01-01
      • 2019-05-11
      • 2017-12-01
      相关资源
      最近更新 更多