【问题标题】:Spring Boot HttpSecurity always 403 forbiddenSpring Boot HttpSecurity 总是 403 被禁止
【发布时间】:2020-02-11 23:59:42
【问题描述】:

我总是得到 http 状态 403。我有这个安全配置:

@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
    httpSecurity
        .cors().and().csrf().disable()
        .authorizeRequests()
        .antMatchers("/api/users/login/").permitAll()
        .anyRequest().authenticated();
}


@Bean
CorsConfigurationSource corsConfigurationSource() {
    CorsConfiguration configuration = new CorsConfiguration();
    configuration.setAllowedOrigins(Arrays.asList("*"));
    configuration.setAllowedMethods(Arrays.asList("*"));
    configuration.setAllowedHeaders(Arrays.asList("*"));
    configuration.setAllowCredentials(true);
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    source.registerCorsConfiguration("/**", configuration);
    return source;
}

我无法发布到 /api/users/login

2019-10-15 12:25:49.567[0;39m [32mDEBUG[0;39m [35m7423[0;39m [2m---[0;39m [2m[nio-8080-exec-1][0;39m [36mo.s.web.servlet.DispatcherServlet [0;39m [2m:[0;39m POST“/error”的“ERROR”调度,参数={} [2m2019-10-15 12:25:49.576[0;39m [32mDEBUG[0;39m [35m7423[0;39m [2m---[0;39m [2m[nio-8080-exec-1][0;39m [36ms.w.s.m.m.a.RequestMappingHandlerMapping[0;39m [2m:[0;39m 映射到公共 org.springframework.http.ResponseEntity> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest) [2m2019-10-15 12:25:49.605[0;39m [32mDEBUG[0;39m [35m7423[0;39m [2m---[0;39m [2m[nio-8080-exec-1][0;39m [36mo.s.w.s.m.m.a.HttpEntityMethodProcessor [0;39m [2m:[0;39m 使用 'application/json',给定 [/] 并支持 [application/json, 应用程序/+json、应用程序/json、应用程序/+json] [2m2019-10-15 12:25:49.608[0;39m [32mDEBUG[0;39m [35m7423[0;39m [2m---[0;39m [2m[nio-8080-exec-1][0;39m [36mo.s.w.s.m.m.a.HttpEntityMethodProcessor [0;39m [2m:[0;39m 写作 [{timestamp=Tue Oct 15 12:25:49 CEST 2019, status=403, 错误=禁止,消息=拒绝访问,路径=/(截断)...] [2m2019-10-15 12:25:49.661[0;39m [32mDEBUG[0;39m [35m7423[0;39m [2m---[0;39m [2m[nio-8080-exec-1][0;39m [36mo.s.web.servlet.DispatcherServlet [0;39m [2m:[0;39m 退出“错误”调度,状态 403

【问题讨论】:

  • 也许尝试删除.authorizeRequests() 电话。无论如何,403 错误不是 CORS 问题,也不是由您的 CORS 配置引起的。
  • 在您的帖子中,我没有看到任何关于身份验证的代码。
  • .antMatchers("/api/users/login/").permitAll() ?
  • @PatelRomil 请写下此评论作为答案。你完全正确!

标签: java spring spring-boot spring-security


【解决方案1】:

试试.antMatchers(HttpMethod.POST,"/api/users/login").permitAll(),还要注意你有.antMatchers("/api/users/login/"),并且你正在向/api/users/login发出请求,注意额外的/在你的antMatchers中。

您也可以使用configure(WebSecurity web),这将绕过here所述的Spring Security过滤器链

【讨论】:

    猜你喜欢
    • 2019-04-01
    • 2018-04-23
    • 1970-01-01
    • 2017-11-28
    • 2021-04-08
    • 1970-01-01
    • 2021-09-04
    • 2021-03-05
    • 2016-03-06
    相关资源
    最近更新 更多