Java 即使csrf被禁用,Spring boot也会为移动请求抛出403

Java 即使csrf被禁用,Spring boot也会为移动请求抛出403,java,spring,spring-boot,spring-security,csrf,Java,Spring,Spring Boot,Spring Security,Csrf,我有一个简单的Spring启动应用程序,它有一个POST rest api方法来注册用户。当我通过邮递员测试它时,它非常有效。但当我从我的移动应用程序测试它时,它总是抛出403。这在选项级别失败,因为我没有看到后端记录尝试的请求 对于这个问题,通常的解决方案是在spring安全配置中禁用csrf。有趣的是,我有一个残疾人,但仍然得到403分。我已经尽我所能地搜索了,但找不到一个解决方案,说明这是如何仍然失败的。感谢任何人能提供的帮助 这是安全配置 @Configuration @EnableWe

我有一个简单的Spring启动应用程序,它有一个POST rest api方法来注册用户。当我通过邮递员测试它时,它非常有效。但当我从我的移动应用程序测试它时,它总是抛出403。这在选项级别失败,因为我没有看到后端记录尝试的请求

对于这个问题,通常的解决方案是在spring安全配置中禁用
csrf
。有趣的是,我有一个残疾人,但仍然得到403分。我已经尽我所能地搜索了,但找不到一个解决方案,说明这是如何仍然失败的。感谢任何人能提供的帮助

这是安全配置

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    // Some beans that are not relevant

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(authenticationProvider());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .authorizeRequests()
                .antMatchers("/register").permitAll()
                .antMatchers("/login").permitAll()
                .anyRequest().authenticated()
                .and().exceptionHandling().accessDeniedHandler(accessDeniedHandler)
                .and().sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS);
        http.addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class);
    }
}
我认为还值得一提的是,我还尝试添加了许多其他线程中建议的
cors
disable选项,但没有效果

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors().and().csrf().disable()
                .authorizeRequests()
                .antMatchers("/register").permitAll()
                .antMatchers("/login").permitAll()
                .anyRequest().authenticated()
                .and().exceptionHandling().accessDeniedHandler(accessDeniedHandler)
                .and().sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS);
        http.addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class);
    }
这就是我的控制器的样子

@RestController
public class AuthenticationResource {

    // Wiring and other methods

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public ResponseEntity<?> registerNewUser(@Valid @RequestBody UserRegistrationRequest request) {
        if (!request.getConfirmPassword().equals(request.getPassword())) {
            throw new MyException(ErrorUtil.generateErrorFieldsValue("password", "confirmPassword"),
                    "Passwords do not match!", HttpStatus.BAD_REQUEST);
        }

        UserAccountResponse savedUserAccount = userAccountService.save(request);
        return ResponseEntity.ok(savedUserAccount);
    }
}
@RestController
公共类AuthenticationResource{
//接线和其他方法
@RequestMapping(value=“/register”,method=RequestMethod.POST)
公共响应身份注册服务器(@Valid@RequestBody UserRegistrationRequest){
如果(!request.getConfirmPassword().equals(request.getPassword())){
抛出新的MyException(ErrorUtil.generateErrorFieldsValue(“密码”、“确认密码”),
“密码不匹配!”,HttpStatus.BAD_请求);
}
UserAccountResponse savedUserAccount=userAccountService.save(请求);
返回ResponseEntity.ok(savedUserAccount);
}
}

如果需要任何其他详细信息,请告诉我。

据我所知,这与您建议的CORS有关,但是通过使用
http.CORS()
您告诉spring查找名为
corsFilter()
的bean。在我的其他项目中,我就是这样解决的

@Bean
公共公司过滤器{
UrlBasedCorsConfigurationSource configSource=新建
UrlBasedCorsConfigurationSource();
CorsConfiguration配置=新的CorsConfiguration();
//得到、张贴、头
config.applyPermitDefaultValues();
config.addAllowedMethod(HttpMethod.PUT);
config.addAllowedMethod(HttpMethod.DELETE);
config.addAllowedMethod(HttpMethod.OPTIONS);
configSource.registerCorsConfiguration(“/**”,config);
返回新的公司过滤器(配置源);
}

应用程序和邮递员使用的URL是否相同?将
org.springframework.security:DEBUG
添加到日志中,查看Spring security告诉您的消息。