【发布时间】:2017-01-27 11:19:42
【问题描述】:
我的 Web 应用程序受 Oauth2 保护。对于 ajax 调用,必须在请求文件夹中提供 access_token。例如,这是 factory.js 中的方法之一:
service.getAll = function () {
var url = SERVER + "/ristore/foundation/";
return $http({
headers: {'Authorization': 'Bearer ' + $window.localStorage.getItem("access_token")},
url: url,
method: 'GET',
crossOrigin: true
})
}
现在我想从网页下载一个文件。该文件通过流式传输从服务器传递给客户端:
@RequestMapping(
value = "/ristore/foundation/xml/{filename}",
method = RequestMethod.GET,
produces = "application/xml")
public ResponseEntity<byte[]> downloadXMLFile(@PathVariable String filename) throws IOException {
FileSystemResource xmlFile = new FileSystemResource("/rsrch1/rists/moonshot/data/prod/foundation/xml/" + filename + ".xml");
byte [] content = new byte[(int)xmlFile.contentLength()];
IOUtils.read(xmlFile.getInputStream(), content);
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType("application/octet-stream"))
.contentLength(xmlFile.contentLength())
.body(content);
}
在html中,我在将被点击开始下载的按钮上指定了ng-click():
<td data-title="'File'" class="text-center"><span class="glyphicon glyphicon-download-alt" ng-click="download(report.filename)"></span></a>
</td>
根据这篇帖子Download files in Javascript with OAuth2的回答,$window.open用于处理我的控制器中的url:
$scope.download = function(filename) {
var url = "http://rcdrljboss01a:9880/ristoreService/ristore/foundation/xml/" + filename + "?access_token=" + $window.localStorage.getItem("access_token");
$window.open(url);
}
我可以通过这种方式下载文件,但是 access_token 显示在下载 url 中。有没有办法在 url 中隐藏 access_token?
【问题讨论】:
标签: angularjs spring oauth access-token