Angularjs Post 没有发送 headers 到 Spring JWT
Angularjs Post not sending headers to Spring JWT
我有一个由 AngularJs 构建的网络应用程序和一个由 Spring 构建的后端应用程序,我正在使用 JWT 来保护我的应用程序。
使用 Get 方法一切正常,在后端级别,我获得了我期望的不记名令牌,因此我可以 return 私人信息。但是使用 POST 方法不发送不记名令牌。
我不知道这是后端还是前端层的问题。
这里有我的代码:
AngularJS
$http({
method: 'POST',
url: SessionService.apiUrl + '/category/create',
headers: {
'Accept': 'application/json','Content-Type': 'application/json;
charset=UTF-8;', 'Authorization': 'Bearer ' + SessionService.getToken()
},
data: params
})
对于 GET 方法,我有完全相同的方法(没有参数和方法 GET)并且它正在工作。
在后端:
@RequestMapping(value = "/category/create", method = RequestMethod.POST)
public @ResponseBody
Response add(@RequestBody CategoryBO request) {
...
}
为了获得授权 header,我正在按以下方式使用 io.jsonwebtoken 库:
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
FilterChain filterChain) throws ServletException, IOException {
String authorizationHeader = request.getHeader("authorization");
使用 Postman 后端运行良好,但使用 angularjs 则不然。
GET - 请求 Header:
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:es-ES,es;q=0.8,en;q=0.6,pt;q=0.4
Authorization:Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJmYWJpYW4uYW5nZWxvbmkiLCJyb2xlcyI6IkFETUlOIiwiaWF0IjoxNDU5MDE5MTg0fQ.QAPZDbyavambfdK9LJUQWyzSRAuELvg_IGTjFdsm6cc
Connection:keep-alive
Host:localhost:8080
Origin:http://localhost:3000
Referer:http://localhost:3000/
User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36
POST - 请求 Header
Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:es-ES,es;q=0.8,en;q=0.6,pt;q=0.4
Access-Control-Request-Headers:accept, authorization, content-type
Access-Control-Request-Method:POST
Connection:keep-alive
Host:localhost:8080
Origin:http://localhost:3000
Referer:http://localhost:3000/
User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36
我遇到了类似的问题,然后我意识到前端在实际执行 HTTP.OPTIONS 请求之前触发了预检请求。
More info here
这样的请求将没有授权令牌,因此对控制器的访问将是fail.The最自然的解决方案,因此,将通过让所有 OPTIONS 请求通过来设置应用程序的安全性。
这是可以做到的
- 正在设置配置,如you can see here
- 将您的应用程序肯定会拥有的 CorsFilter 设置为
希望对您有所帮助
感谢@user2858970 的解释。我发现它很清晰。
要解决问题,您基本上需要处理两个问题:
- 在您的服务器中启用 CORS,因为您的服务器必须知道它正在从不同域中的客户端获取请求。
- 同时在 Spring 安全级别启用 CORS,以允许它利用在 Spring MVC 级别定义的配置。
我已经将解释和代码放在一起来回答 CORS with spring-boot and angularjs not working
.
我有一个由 AngularJs 构建的网络应用程序和一个由 Spring 构建的后端应用程序,我正在使用 JWT 来保护我的应用程序。 使用 Get 方法一切正常,在后端级别,我获得了我期望的不记名令牌,因此我可以 return 私人信息。但是使用 POST 方法不发送不记名令牌。 我不知道这是后端还是前端层的问题。 这里有我的代码:
AngularJS
$http({
method: 'POST',
url: SessionService.apiUrl + '/category/create',
headers: {
'Accept': 'application/json','Content-Type': 'application/json;
charset=UTF-8;', 'Authorization': 'Bearer ' + SessionService.getToken()
},
data: params
})
对于 GET 方法,我有完全相同的方法(没有参数和方法 GET)并且它正在工作。
在后端:
@RequestMapping(value = "/category/create", method = RequestMethod.POST)
public @ResponseBody
Response add(@RequestBody CategoryBO request) {
...
}
为了获得授权 header,我正在按以下方式使用 io.jsonwebtoken 库:
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
FilterChain filterChain) throws ServletException, IOException {
String authorizationHeader = request.getHeader("authorization");
使用 Postman 后端运行良好,但使用 angularjs 则不然。
GET - 请求 Header:
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:es-ES,es;q=0.8,en;q=0.6,pt;q=0.4
Authorization:Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJmYWJpYW4uYW5nZWxvbmkiLCJyb2xlcyI6IkFETUlOIiwiaWF0IjoxNDU5MDE5MTg0fQ.QAPZDbyavambfdK9LJUQWyzSRAuELvg_IGTjFdsm6cc
Connection:keep-alive
Host:localhost:8080
Origin:http://localhost:3000
Referer:http://localhost:3000/
User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36
POST - 请求 Header
Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:es-ES,es;q=0.8,en;q=0.6,pt;q=0.4
Access-Control-Request-Headers:accept, authorization, content-type
Access-Control-Request-Method:POST
Connection:keep-alive
Host:localhost:8080
Origin:http://localhost:3000
Referer:http://localhost:3000/
User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36
我遇到了类似的问题,然后我意识到前端在实际执行 HTTP.OPTIONS 请求之前触发了预检请求。 More info here
这样的请求将没有授权令牌,因此对控制器的访问将是fail.The最自然的解决方案,因此,将通过让所有 OPTIONS 请求通过来设置应用程序的安全性。
这是可以做到的
- 正在设置配置,如you can see here
- 将您的应用程序肯定会拥有的 CorsFilter 设置为
希望对您有所帮助
感谢@user2858970 的解释。我发现它很清晰。
要解决问题,您基本上需要处理两个问题:
- 在您的服务器中启用 CORS,因为您的服务器必须知道它正在从不同域中的客户端获取请求。
- 同时在 Spring 安全级别启用 CORS,以允许它利用在 Spring MVC 级别定义的配置。
我已经将解释和代码放在一起来回答 CORS with spring-boot and angularjs not working .