春季安全与邮政法冲突

发布于 2025-02-10 07:22:35 字数 724 浏览 0 评论 0原文

我正在尝试将Spring Basic Auth实施到应用程序中。我在应用程序中添加了效率线。专业:

  #Security
  security:
    user:
      name: admin
      password: admin

因此,弹簧将自己创建一个Web Security Config Bean。但是我进行了一些测试,除了发布403状态以外,除了发布时,所有方法都按预期进行了预期。我浏览了网络,发现它是由于CSRF保护而发生的,并将其禁用,创建了其他Web安全配置类:

@EnableWebSecurity
public class WebSecurityConfig {

@Bean
protected SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
    http.cors().and().csrf().disable();
    return http.build();
}
}

但是,它完全禁用身份验证。用户不需要密码和登录才能使用资源。 我的问题是,如何以帖子方法不会投掷403的方式配置身份验证?我是否需要编写完整的Web安全配置并从Application.properties或有另一种方法中删除这4行?提前致谢。

I am trying to implement Spring basic auth into the app. I added the folowing lines to application.properties:

  #Security
  security:
    user:
      name: admin
      password: admin

So the Spring will create a web security config bean by itself. But I ran some tests and everything works as expected for any method besides POST as it throws 403 status. I browsed the web and discovered that it happens due to csrf protection and I disabled it, creating additional web security config class:

@EnableWebSecurity
public class WebSecurityConfig {

@Bean
protected SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
    http.cors().and().csrf().disable();
    return http.build();
}
}

However, it completely disables the authentication. The user does not need a password and a login to use the resources.
My question is, how do I configure the authentication in a way POST method will not throw 403? Do I need to write a full web security config and delete those 4 lines from application.properties or there is an alternative way? Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文