如何解决Spring引导Post请求中的403错误

如何解决Spring引导Post请求中的403错误,spring,rest,maven,spring-boot,Spring,Rest,Maven,Spring Boot,我是spring boot rest服务的新手。我使用maven项目在spring boot中开发了一些RESTAPI 我已经成功开发了Get和PostApi。我的GET方法在邮递员和手机中正常工作当我试图从邮递员那里点击post方法时,它工作正常,但从手机中,它给出了403禁止的错误。 这是我的配置: spring.datasource.url = jdbc:mysql://localhost/sampledb?useSSL=false spring.datasource.username =

我是spring boot rest服务的新手。我使用maven项目在spring boot中开发了一些RESTAPI

我已经成功开发了GetPostApi。我的GET方法在邮递员和手机中正常工作当我试图从邮递员那里点击post方法时,它工作正常,但从手机中,它给出了403禁止的错误。

这是我的配置:

spring.datasource.url = jdbc:mysql://localhost/sampledb?useSSL=false
spring.datasource.username = te
spring.datasource.password = test
spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.MySQL5InnoDBDialect
Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = update
请告诉我如何解决这个错误

可能的原因:

  • 邮递员发出的请求与手机发出的请求不同(uri、方法、标题)
  • 无效令牌
  • CORS(阅读一些关于它的文章,谷歌有很多文章)将@CrossOrigin注释添加到你的控制器中
  • 移动应用程序在执行POST之前正在执行选项请求,您将阻止选项请求。如果来自postman的选项请求也被阻止,则添加属性spring.mvc.dispatch options request=true。此外,在使用spring安全性的情况下,还必须显式地允许对它的选项请求

  • 您必须禁用csrf保护,因为它在spring security中是默认启用的:在这里您可以看到允许cors来源的代码

    import org.springframework.context.annotation.Bean;
    import org.springframework.security.config.annotation.web.builders.HttpSecurity;
    import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
    import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
    import org.springframework.web.cors.CorsConfiguration;
    import org.springframework.web.cors.CorsConfigurationSource;
    import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
    
    @EnableWebSecurity
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
    
        @Override
        protected void configure(HttpSecurity http) throws Exception{
            http.cors().and().csrf().disable();
        }
    
        @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;
        }
    
    }
    

    以公认的答案为基础

    许多HTTP客户端库(例如Axios)隐式地为POST请求设置
    内容类型:JSON
    头。在我的例子中,我忘了允许那个标题,这只会导致帖子失败

    @Bean
    CorsConfigurationSource corsConfigurationSource() {
        ...
        configuration.addAllowedHeader("Content-Type"); // <- ALLOW THIS HEADER 
        ...
    }
    
    @Bean
    CorsConfiguration源CorsConfiguration源(){
    ...
    
    configuration.addAllowedHeader(“内容类型”);//您是否与postman一起发送任何标题?我认为您发送令牌是为了验证请求。您是否从移动设备发送此令牌?请添加spring引导配置的详细信息。我仅发送postman和mobile@Deoss的内容类型请附合日志,spring已意外读取日志中的ble错误消息;)这是我的配置:#spring.datasource.url=jdbc:mysql://192.168.4.2/maha?useSSL=false #spring.datasource.username=test#spring.datasource.password=test@123#########Hibernate属性###SQL方言使Hibernate为所选数据库生成更好的SQL#spring.jpa.Properties.Hibernate.dial=org.hibernate.dialogue.mysql5innodbdialogue####hibernate ddl自动(创建、创建、删除、验证、更新)#spring.jpa.hibernate.ddl-auto=update@jannikweichertca你能告诉我如何添加选项请求吗?@desossI已经更新了第4点…让我知道spring.mvc.dispatch options request=true是否有效。是的,我正在尝试。我会让你知道是否有效是的,你是对的-邮递员的POST请求被认为是CORS,但刚刚赢了为什么即使CORS被禁用,邮递员的GET请求也会通过?禁用csrf使我的POST请求成功!真是松了一口气。谢谢你,我的朋友!你节省了我的时间!我也遇到过同样的情况,再加上基于JWT的身份验证。这个解决方案对我来说很有效,还有一点:我必须允许“选项”在授权头中没有JWT的情况下请求通过。为什么禁用csrf允许post方法?@Ezzat Eissa-禁用
    csrf
    当然有效。但这也会使我们的API易受攻击,不是吗?如果是这样,如何确保从服务器端和客户端配置csrf令牌?