自定义响应标头AngularJS的访问控制 - 公开 - 标头配置

时间:2015-04-29 04:57:10

标签: angularjs spring cors

我在reading over a similar question之后在资源上实施自定义响应标头,我添加了一个“访问控制 - 公开 - 标题”'到我的CORS标题,但我不能正确配置。

以下是我在资源的GET请求中收到的标题:我收到了一个'响应标题' "史蒂夫",应该被' Access-Control-Expose-Headers'

所允许
Access-Control-Allow-Headers:x-requested-with, Request-Header, Response-Header
Access-Control-Allow-Methods:POST, GET, OPTIONS, DELETE
Access-Control-Allow-Origin:*
Access-Control-Expose-Headers:Response-Header
Response-Header:Steve

但是,Angular仍然无法访问该自定义标头。我收回了我的自定义Request-Header,但不是我自定义的Response-Header:

{
  "data": {
    "id": 2,
    "content": "Hello, frank!"
  },
  "status": 200,
  "config": {
    "method": "GET",
    "transformRequest": [
      null
    ],
    "transformResponse": [
      null
    ],
    "url": "http:\/\/localhost:8080\/greeting",
    "headers": {
      "Accept": "application\/json, text\/plain, *\/*",
      "Request-Header": "frank"
    }
  },
  "statusText": "OK"
}

Angular 1.3 $ httpProvider.interceptor:

'use strict';
angular
  .module('headerDemoUiApp')
  .factory('AuthInterceptor', function AuthInterceptor(nameService) {

    return {
      request: handleRequest,
      response: handleResponse
    };

    function handleRequest(config) {
      if (angular.isDefined(config.headers)) {
        config.headers['Request-Header'] = "frank";
      }
      return config;
    }

    function handleResponse(response) {
      console.log('Response: ' + JSON.stringify(response));

      if (angular.isDefined(response.config.headers['Response-Header'])) {
        var respHead = response.config.headers['Response-Header'];
        console.log('Response-Header: ' + respHead);
        nameService.responsename=respHead;
      }
      return response;
    }

  });

而且,Spring版本4.1.6 @RestController添加了标题。

@RestController
public class GreetingController {

    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    @RequestMapping("/greeting")
    public Greeting greeting(
            @RequestParam(value = "name", defaultValue = "World") String name,
            HttpServletResponse response,
            @RequestHeader("Request-Header") String headerName) {

        // Sets our custom response header
        response.setHeader("Response-Header","Steve");

        return new Greeting(counter.incrementAndGet(),
                String.format(template, headerName));
    }
}

API Code Repo

UI Code Repo

谢谢你看看!

1 个答案:

答案 0 :(得分:2)

问题在于您的API GreetingController.java

更改greeting方法以返回ResponseEntity并使用HttpHeaders,如下所示

@RequestMapping("/greeting")
    public ResponseEntity<Greeting> greeting(@RequestParam(value = "name", defaultValue = "World") String name, @RequestHeader("Request-Header") String headerName) {

        MultiValueMap<String, String> headers = new HttpHeaders();
        // Sets our custom response header
        headers.add("Response-Header","Steve");
        return new ResponseEntity<Greeting>(new Greeting(counter.incrementAndGet(),String.format(template, headerName)), headers, HttpStatus.OK); 
    }

以下是我与Postman客户端的标题回复

Content-Length → 2
Content-Type → text/plain;charset=UTF-8
Date → Wed, 27 May 2015 15:53:47 GMT
Response-Header → Steve
Server → Apache-Coyote/1.1