【问题标题】:$http request giving 403 after using Spring Security?使用 Spring Security 后 $http 请求给出 403?
【发布时间】:2018-12-13 15:10:39
【问题描述】:

下面是我正在使用的 SecurityConfiguration 类。

@SpringBootApplication
@RestController
public class WebMvcConfig {
@Configuration
protected static class SecurityConfiguration extends 
WebSecurityConfigurerAdapter {

  @Override
  protected void configure(HttpSecurity http) throws Exception {


  http.authorizeRequests()
  .antMatchers("/login").permitAll()
  .antMatchers("/**").authenticated()
  .anyRequest().authenticated()
  .and()
  .formLogin().permitAll();
  }
}

启动后,只要我点击我的 URL (http://localhost:8080/TestApp/),它就会带我到默认登录页面,当我输入默认用户 ID(用户)和密码(打印在控制台上)时,它会带我通过我的 AngularJS 路由到由“/” URL 映射的 index.html 页面。我可以浏览 UI,但是只要我提交任何 $http 请求(我正在尝试使用 POST 请求),它就会在浏览器控制台上使用我的 $http 请求 URL 给我 403。

有人可以帮忙吗?

【问题讨论】:

  • 尝试添加http.csrf().disable();

标签: java angularjs spring-security http-headers http-post


【解决方案1】:

错误 403 表示您被禁止访问所有资源。

如果您检查错误详细信息,您很可能会收到诸如Expected CSRF token not found 之类的消息。

从 v4 开始,spring security 默认启用csrf 保护。这是一个很好的做法,因为csrf 攻击会强制最终用户在当前已通过身份验证的 Web 应用程序上执行不需要的操作。

所以在开发环境中,添加http.csrf().disable(); 将解决您的问题。但是,当您想迁移到 prod 环境时,您应该考虑添加 csrf 令牌。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-17
    • 2021-09-26
    • 1970-01-01
    • 1970-01-01
    • 2015-10-23
    • 2011-05-10
    • 2017-10-03
    • 2022-01-06
    相关资源
    最近更新 更多