繁体   English   中英

POST 请求 org.springframework.security.authentication.InsufficientAuthenticationException: 访问此资源需要完全认证

[英]POST request org.springframework.security.authentication.InsufficientAuthenticationException: Full authentication is required to access this resource

使用 spring 安全与 jwt。

我的 spring 安全配置:

@RequiredArgsConstructor
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    private final JwtFilter jwtFilter;

    private final exceptionHandler exceptionHandler;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .httpBasic().disable()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                .antMatchers("/api/v1/users/**").hasAnyRole("USER")
                .antMatchers("/api/v1/admin/**").hasRole("ADMIN")
                .anyRequest()
                .authenticated()
                .and()
                .addFilterBefore(jwtFilter, FilterSecurityInterceptor.class)
                .exceptionHandling().authenticationEntryPoint(exceptionHandler);
    }
}

当我发送 GET 请求时,一切都很好。 但是当我发送 POST 时:

org.springframework.security.authentication.InsufficientAuthenticationException: Full authentication is required to access this resource

如果我添加.csrf().disable()它会有所帮助,但我不明白它是如何工作的。 为什么 Get 请求正常,但没有.csrf().disable()抛出异常?

我可以在没有.csrf().disable()部分的情况下传递 POST 请求吗?


为了保护 MVC 应用程序,Spring 为每个生成的视图添加了一个 CSRF 令牌。 此令牌必须在每个修改 state 的 HTTP 请求(PATCH、POST、PUT 和 DELETE — 不是 GET)上提交给服务器。
这可以保护我们的应用程序免受 CSRF 攻击,因为攻击者无法从他们自己的页面获取此令牌。
更多信息:https://www.baeldung.com/spring-security-csrf

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM