java - 使用 Spring Security 后 $http 请求给出 403?
问题描述
下面是我正在使用的 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。
有人可以帮忙吗?
解决方案
错误 403表示您被禁止访问所有资源。
如果您检查错误详细信息,您很可能会收到一条消息,例如Expected CSRF token not found
.
从 v4 开始,spring security
默认启用csrf
保护。这是一种很好的做法,因为csrf
攻击会迫使最终用户在当前经过身份验证的 Web 应用程序上执行不需要的操作。
因此,在开发环境中,添加http.csrf().disable();
将解决您的问题。csrf
但是当你想移动到 prod 环境时, 你应该考虑添加一个令牌。
推荐阅读
- swift - 将 UITableView 放入内部后 UITableViewCell 损坏
- javascript - 如何在 sachinchoolur/lightGallery 中集成延迟加载图像
- excel - Excel 保护:如何通过 MS Office 许可证检查仅在 Office 前提下控制 Excel 访问
- r - 当向量有几个 NaN 时,如何在 R 中插值/外推?
- python - 如何解决“执行结果可视化时出现关键错误”?
- google-chrome - Chrome(特别是表格)突然无法正确打印嵌入的图像
- excel - 根据列数据中的唯一ID覆盖单行excel信息
- python - Python - 在不为实例分配名称的情况下实例化一个类
- python - 如何在python中附加名称和整数变量
- jquery - 如何在输入 Bootstrap Datepicker 中显示年份范围?