简体   繁体   English

Angular 和 Spring 启动 -> POST 请求响应 403

[英]Angular and Spring boot -> POST Request response 403

I have a problem with Spring boot and Angular. I make a POST request which works on POSTMAN and locally but which gives me a 403 in production on tomcat with apache as reverse proxy.我的 Spring 启动和 Angular 有问题。我发出了一个 POST 请求,它在 POSTMAN 和本地工作,但它在 tomcat 上给了我一个 403 的生产状态,apache 作为反向代理。 But it's working when I am with the embedded tomcat.但是当我使用嵌入式 tomcat 时它可以工作。

I have to try everything soon.我必须尽快尝试一切。

All the solutions I've seen say to disable CSFR but I have no authentication to access my webservice and therefore no spring-security dependency.我见过的所有解决方案都说要禁用 CSFR,但我没有身份验证来访问我的网络服务,因此没有 spring-security 依赖项。

I tried anyway but the problem is still there.无论如何我都试过了,但问题仍然存在。 And in some cases it required me to log in which I don't want to do在某些情况下,它要求我登录,而我不想这样做


import ch.megahertz.swissqrbillsgeneratorapi.properties.FileStorageProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication(scanBasePackages = {"ch.megahertz.swissqrbillsgeneratorapi.*"})
@EnableConfigurationProperties({
        FileStorageProperties.class
})

public class SwissQrBillsGeneratorApiApplication extends SpringBootServletInitializer {

    static Logger logger = LoggerFactory.getLogger(SwissQrBillsGeneratorApiApplication.class);
    public static void main(String[] args) {
        logger.info("Run application");
        SpringApplication.run(SwissQrBillsGeneratorApiApplication.class, args);
    }
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {

        return application.sources(SwissQrBillsGeneratorApiApplication.class);
    }



}


import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedMethods("*")
                .allowedHeaders("*")
                .allowedOrigins("*")
                .allowCredentials(false)
                .maxAge(-1);
    }

}


import ch.megahertz.swissqrbillsgeneratorapi.payload.Invoice;
import ch.megahertz.swissqrbillsgeneratorapi.service.CRMService;
import ch.megahertz.swissqrbillsgeneratorapi.service.FileStorageService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;


//@CrossOrigin(origins = {"https://swissqrbillsgenerator.megahertz.ch/","http://localhost:4200/"})
@Slf4j
@RestController
public class ApiController {

    private static final Logger logger = LoggerFactory.getLogger(ApiController.class);

    @Autowired
    private FileStorageService fileStorageService;

    @Autowired
    private CRMService crmService;

    @GetMapping
    public String generateQRBills() {
        log.info("Enter in GeT API");
    return "Get ok";
    }

    @PostMapping("/generate")
    public ResponseEntity<Resource> uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
        System.out.println("Enter in generate API");
        logger.info("Enter in generate API");
        log.info("Enter in generate POST API");
        String fileName = fileStorageService.storeFile(file);

        String fileDownloadUri = ServletUriComponentsBuilder.fromCurrentContextPath()
                .path("/downloadFile/")
                .path(fileName)
                .toUriString();

        Invoice facture = crmService.getFactureInfo(fileName);
        File fileWithQR = fileStorageService.addQrToFile(fileName, facture);

        Resource resource = new UrlResource(fileWithQR.toURI());
           return ResponseEntity.ok()
                    .header(HttpHeaders.CONTENT_TYPE,Files.probeContentType(resource.getFile().toPath()))
                    .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName+ "\"")
                    .body(resource);


    }
}

Do you have any idea?你有什么主意吗?

If Postman is returning the query, then the problem is probably in the Angular front end.如果 Postman 正在返回查询,那么问题可能出在 Angular 前端。 I believe I was getting a 403 error when I tried to send a String from my backend, it had to be wrapped in an object and unwrapped by Angular to be a string in Angular.我相信当我试图从我的后端发送一个字符串时我收到了一个 403 错误,它必须被包装在一个 object 中并被 Angular 解包为一个字符串在 Angular 中。

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

相关问题 Spring Boot 403禁止在Tomcat 9中使用POST请求 - Spring Boot 403 forbidden with POST request in Tomcat 9 在 Spring Boot 中,GET 请求返回 200,但 POST 请求返回 403 - In spring boot, GET request is returning 200 but POST request is returning 403 当使用Keyclaok进行POST请求时,Spring Boot返回403被禁止 - spring boot return 403 forbidden when POST request with Keyclaok Spring 启动 -- 使用 CSRF 令牌发布请求产生 403 错误 - Spring Boot -- Post request with CSRF token produce 403 error 使用 angular 和 spring 与 JWT 启动服务的 PUT 请求出现 403 错误 - 403 error with PUT request using angular and spring boot service with JWT Angular 无法在 Spring 上使用 Keycloak 启动时获取请求抛出 403 - Angular cannot GET request on Spring boot with Keycloak throws 403 使用 Angular 2 和 Spring Boot 发送 post 请求 - Send post request with Angular 2 and Spring Boot Angular 和 Spring Boot - POST 请求成为 OPTIONS - Angular and Spring boot - POST Request become OPTIONS 将POST请求发送到服务器,服务器的响应为null,Angular-Spring-boot - Send POST request to server, Server's response is null, Angular - Spring-boot 发布方法 400 错误请求错误角度弹簧引导 - Post method 400 bad request error angular spring boot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM