小编jok*_*r21的帖子

如何允许跨源请求

我是后端开发人员,我提供了具有JWT安全性的Spring Boot Rest API,供前端开发人员使用,该前端开发人员从本地主机调用api。因此,当他调用POST请求时,他说他收到了CORS错误。那个部分

 @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {


        response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With, remember-me");


        chain.doFilter(request, response);
    }
Run Code Online (Sandbox Code Playgroud)

但是他仍然得到了错误,可能是什么原因。

OPTIONS https:my.domain/url 401 (Unauthorized)
Run Code Online (Sandbox Code Playgroud)

当它是POST请求时。

控制器代码:

@RestController
public class RegistrationController {



    @Autowired
    @Qualifier("restTemplateUserRegitration")
    private RestTemplate restTemplateUserRegitration;
@RequestMapping(value="${user.endpoint}",produces={MediaType.APPLICATION_JSON_VALUE},method=RequestMethod.POST)
    public ResponseEntity<?> registerUser(@RequestBody Model ModelRequest){

                Map<String, Object> Status=new HashMap<String, Object>();
                FeedBackStatus = restTemplateUserRegitration.postForObject("http:serviceurl",registration/single",Model.class,Map.class );
                return ResponseEntity.ok(Status); 
    }
}
Run Code Online (Sandbox Code Playgroud)

java rest spring spring-boot

5
推荐指数
1
解决办法
452
查看次数

如何将多部分文件从另一个服务发送到一个服务

我有两个端点 api,分别是/upload/redirect

/upload是我直接上传文件的地方。 /redirect是我接收文件并将其传递到上传并从/upload获取 JSON 响应的位置。下面是我的代码:

package com.example;

import java.io.BufferedOutputStream;

import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

@RestController
public class UserLogsController {

    @Autowired
    @Qualifier("restTemplateUserRegitration")
    private RestTemplate restTemplateUserRegitration;

    @Bean
    public RestTemplate restTemplateUserRegitration() {

        RestTemplateBuilder builderUserRegitration = new …
Run Code Online (Sandbox Code Playgroud)

java multipartform-data resttemplate spring-boot

4
推荐指数
1
解决办法
1万
查看次数

如何为CORS指定响应头?

我正在构建一个后端REST API,我的朋友正在构建一个Angular JS前端应用程序来调用我的API.我有一个带有键的标记头Authorization和一个可以访问服务的值,否则它会拒绝.从Postman和REST客户端我能够收到API但是在测试时他说他在preflight上获得了401 Unauthorized Error.Below是我的doFilterInternal方法.

protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers","Content-Type, Accept, X-Requested-With, Authorization");
}
Run Code Online (Sandbox Code Playgroud)

但当他用Angular JS中的令牌调用API时,他得到了

所以我在这里回答这个问题并添加了属性

spring.mvc.dispatch-options-request=true

在application.properties.But仍然他错误似乎是

预检的响应具有无效的https状态代码401

任何帮助表示赞赏.

java jwt angularjs spring-boot

1
推荐指数
1
解决办法
4280
查看次数