我的应用程序是带有Spring后端的Angular Front-end。我试图从后端到前端访问一些Restful webservices。这是我对Custom Http服务进行的Angular调用:
stationFilter(stFilt : StationFilter): Observable<Response>
{
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers, method: 'post' });
let currentUser = JSON.parse(sessionStorage.getItem('currentUser'));
// options.headers.append('Access-Control-Allow-Origin','*');
if (currentUser && currentUser.token) {
options.headers.append('Authorization', 'Bearer ' + currentUser.token);
}
this.requestJsonBody = JSON.stringify({'snmpFlagOn': stFilt.snmpFLag, 'noIpStations': stFilt.noIPStation, 'stationDataDateBegin': stFilt.inputStartDate, 'stationDataDateEnd': stFilt.inputEndDate });
console.log(this.requestJsonBody);
return this.stationHttp.postService(this.confPropLoader.stationUrl + '/services/stationCollection/viewCollected', this.requestJsonBody, options);
}
对于上面的授权,我使用的是基于JWT的令牌。 这是我的基于角度类型脚本的Custom StationHttp:
import { Injectable } from '@angular/core';
import { ConnectionBackend, XHRBackend, RequestOptions, Request,
RequestOptionsArgs, Response, Http, Headers } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';
import 'rxjs/add/observable/throw';
@Injectable()
export class StationHttp extends Http {
constructor(backend: XHRBackend, defaultOptions: RequestOptions) {
super(backend, defaultOptions);
}
getService(url: string, options?: RequestOptionsArgs): Observable<Response> {
return super.get( url, options).catch(this.handleError);
}
postService(url: string, body: string, options?: RequestOptionsArgs): Observable<Response> {
return super.post(url, body, options);
}
}
export function customHttpFactory(xhrBackend: XHRBackend, requestOptions: RequestOptions): Http {
return new StationHttp(xhrBackend, requestOptions);
}
以下是在Spring方面编写的CORS过滤器:
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.filter.OncePerRequestFilter;
public class CORSFilter extends OncePerRequestFilter {
private static final Log LOG = LogFactory.getLog(CORSFilter.class);
@Override
protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
LOG.debug(" CORS Filter Running....");
if (response == null) {
LOG.debug(" Response is null");
}
LOG.trace("Sending Header....");
// CORS "pre-flight" request
response.addHeader("Access-Control-Expose-Headers", "Access-Control-*");
response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, OPTIONS");
response.addHeader("Access-Control-Allow-Headers", "Access-Control-*, Origin, X-Requested-With, Content-Type, Accept, authorization");
response.addHeader("Access-Control-Allow-Origin", "*");
response.addHeader("Access-Control-Allow-Headers", "content-Type");
// response.addHeader("Access-Control-Max-Age", "3600");
LOG.trace("Request:"+ request.toString());
LOG.trace("Response:"+ response.toString());
// }
filterChain.doFilter(request, response);
}
}
正在调用Restful服务:
@RestController
@RequestMapping(path = { "/stationCollection" })
public class StationCollectionWS {
@Autowired
StationCollectionWSDBProcessing scWSDBPrcessing;
@Autowired
StationCollectionDAO stationCollectDao;
@Autowired
StationExpertStateBean stationExpertBean;
@Autowired
StationCollectionProperties stCollectProp;
@Autowired
@Qualifier(value = "isClustered")
Boolean isClustered;
@RequestMapping(path = { "/viewCollected" }, method = { RequestMethod.POST }, consumes = {
"application/xml", "application/json", "text/xml" }, produces = { "application/json" })
@ResponseBody
public ResponseEntity<List<StationCollectionData>> viewStationDataCollected(
@RequestBody StationDataFilter filter) {
try {
log.debug(" View on the basis of filter:-> " + filter);
Date startDate;
Date endDate;
List<Object> paramList = new ArrayList<Object>();
StringBuilder sqlStringBuilder = new StringBuilder(
"SELECT * FROM STATION_COLLECTION_DATA");
/*
Do some processing for sqlStringBuilder here
*/
String sql = sqlStringBuilder.toString();
log.debug("Argument List:" + paramList);
Object[] params = paramList.toArray();
// Making DAO layer call
RowMapper<StationCollectionData> stDataRowMapper = StationCollectionRowMappers
.getStationCollectionDataRowMapper();
stData = (List<StationCollectionData>) stationCollectDao
.getRecords(sql, params, stDataRowMapper);
} catch (Exception ex) {
ex.printStackTrace();
}
return new ResponseEntity<List<StationCollectionData>>(stData,
HttpStatus.OK);
}
@RequestMapping(path = { "/submitRequest" }, method = { RequestMethod.POST }, consumes = {
"application/xml", "application/json", "text/xml" }, produces = { "application/json" })
@ResponseBody
public ResponseEntity<String> submitStationCollectionRequest(
@RequestBody Request request) {
try {
if (request.getProductType().trim() == null
|| request.getProductIdentifier().trim() == null)
return new ResponseEntity<>(
"Invalid Incoming Request. Please check the following values: [\' Product Identifier (SEID)\', \'Product Type\']",
HttpStatus.BAD_REQUEST);
String incmgProdType = request.getProductType();
String incmgProdId = request.getProductIdentifier();
if (incmgProdId != null && !incmgProdId.equals("")) {
if (!stationCollectDao.chekRecordExist(
"STATION_COLLECTION_QUEUE", "PRODUCTIDENTIFIER",
incmgProdId, true)) {
if (scWSDBPrcessing.checkPrIdCollected(incmgProdId)) {
log.debug("Within Days Limit");
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set("WithinLimit", "MyValue");
return new ResponseEntity<String>(
"Requested Product Identifier:"
+ incmgProdId
+ " is within "
+ stCollectProp.getNoOfStationCollectionDays()
+ " days", responseHeaders,
HttpStatus.ACCEPTED);
}
}
}
if(! incmgProdId.equals("ABC"){
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set("UnsupportedProductType", "MyValue");
return new ResponseEntity<String>(
"Unsupported Product Type for Request",
responseHeaders, HttpStatus.BAD_REQUEST);
}
} catch (SQLIntegrityConstraintViolationException ex) {
log.debug("Record Already Present In Queue");
} catch (Exception ex) {
ex.printStackTrace();
}
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set("SucessfullyCompleted", "MyValue");
return new ResponseEntity<String>("Successfully Submitted Record",
responseHeaders, HttpStatus.OK);
}
}
问题是有一段时间我确实收到了请求(时间不可预测,有时是8小时,有时是12小时)但在此之后,我开始收到以下错误:
我在Chrome的网络标签中看到成功前传递请求,如下所示:
但原始请求失败如下:
事实上它可以运作一段时间,但之后它会让我感到很困惑。我错过了什么吗?
以下是POST呼叫的请求标头:
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate, br
Accept-Language:en-US,en;q=0.9
Authorization:Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhZG1pbiIsInJvbGVz
Cache-Control:no-cache
Connection:keep-alive
Content-Length:137
Content-Type:application/json
Host:lqtremmt01.gl.avaya.com:9002
Origin:http://******************:8080
Pragma:no-cache
Referer:http://******************:8080/stationUI/home/1001/station_filter
User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36
添加我使用的令牌:
eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhZG1pbiIsInJvbGVzIjp7Il9pZCI6IjEwMDEiLCJ1c2VybmFtZSI6ImFkbWluIiwiZmlyc3ROYW1lIjoiVG9tIiwibGFzdE5hbWUiOiJIYXJkeSIsInBhc3N3b3JkIjoiYWRtaW4iLCJyb2xlIjoiYWRtaW4iLCJ0b2tlbiI6ImV5SmhiR2NpT2lKSVV6STFOaUo5LmV5SnpkV0lpT2lKaFpHMXBiaUlzSW5KdmJHVnpJanA3SWw5cFpDSTZJakV3TURFaUxDSjFjMlZ5Ym1GdFpTSTZJbUZrYldsdUlpd2labWx5YzNST1lXMWxJam9pVkc5dElpd2liR0Z6ZEU1aGJXVWlPaUpJWVhKa2VTSXNJbkJoYzNOM2IzSmtJam9pWVdSdGFXNGlMQ0p5YjJ4bElqb2lZV1J0YVc0aUxDSjBiMnRsYmlJNkltVjVTbWhpUjJOcFQybEtTVlY2U1RGT2FVbzVMbVY1U25wa1YwbHBUMmxLYUZwSE1YQmlhVWx6U1c1S2RtSkhWbnBKYW5BM1NXdzVjRnBEU1RaSmFrVjNUVVJGYVV4RFNqRmpNbFo1WW0xR2RGcFRTVFpKYlVacllsZHNkVWxwZDJsYWJXeDVZek5TVDFsWE1XeEphbTlwVmtjNWRFbHBk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TJzeFJrNVlSbWhTUjNReFdXcEdiMkpGTUhwVmJteFRVMGhDU0ZZeGFGZGhNRGxIWTBaa1ZrMUZOVE5VYkdoeVpFVTVXVnBJWkZWU01IQk1WVzB4VTFGc1ZYbFhiWFJTWVROb1RGZHFTbGRUVmxWNFUydGtVbFV3YnpWVVJVNUxZMFpzV1ZWWGJGQmhhMVY0VkZaU1ZtVkZNVFpTVkVKT1VrVldlbE5YTVZkT1IwNUVVMVJhVGxaR1ZqUlViRkpHWldzeE5sTllaRTVYUkVGMVZqSjBiMkpYWnpKaE1FVXdVMnR6TUdKR1VsUmlNMFp0V1hwYWIxb3djR2hSYTJnMFlqSTFTVm96UVRWUlZsWjBXa1Z6ZEZkVk1UQlNVMG81VEVOS2NGbFlVV2xQYWtVeFRWUlZlRTE2VFhkUFJHZHpTVzFXTkdORFNUWk5WRlY0VGxSRmVrNUVaelJQU0RBdWNFOTNTVmRrWjA1VFUzQkhiM2RQVmtGbFkzRlBka3ROZUVGcmVFWkpjRmhyU0dWM01XbFdNM1JPV1NKOUxDSnBZWFFpT2pFMU1UVXhNek14T1RBc0ltVjRjQ0k2TVRVeE5URXpORGs1TUgwLkU0ZFZfbkJscjRJZlVoWVlqVGNOeTdPbXZiMXowZFhSYk9PZndjTEFndVUifSwiaWF0IjoxNTE1MTMzMjY5LCJleHAiOjE1MTUxMzUwNjl9.k1A8OXnA18YYL9aO_wY0j6DSQ29yHvN-ibeOzR6o9h8
这是我从Back-end Onceized获得的完整用户详细信息。一旦我获取它,我将其保存在我的会话存储中。以下是Spring中的代码,当我在stUsr中设置令牌时如下:
stUsr.setToken(Jwts.builder().setSubject(stUsr.getUsername())
.claim("roles",
userDb.get(stUsr.getRole())).setIssuedAt(Conversion.toDate(StationCollectionWSUtilities.getTodaysDate()))
.signWith(SignatureAlgorithm.HS256, "admin").compact());
这个stUsr就是在令牌中显示的内容。
答案 0 :(得分:0)
有类似的问题。对我来说,问题是我的JWT过期并导致Spring Boot应用程序引发异常。负责将响应发送回Angular应用程序的全局异常处理程序是在未设置CORS标头的时间点完成的。
为解决此问题,我在doFilterInternal()方法中首先将CORS响应标头设置为
。CORS响应标头:
httpServletResponse.addHeader("Access-Control-Expose-Headers", "Access-Control-*");
httpServletResponse.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
httpServletResponse.addHeader("Access-Control-Allow-Headers", "Access-Control-*, Origin, X-Requested-With, Content-Type, Accept, Authorization");
httpServletResponse.addHeader("Access-Control-Allow-Origin", "http://localhost:4200");
httpServletResponse.addHeader("Access-Control-Allow-Headers", "content-Type");
httpServletResponse.addHeader("Access-Control-Allow-Credentials", "true");
希望有帮助!