0
这是我的情景:会话被在web.xml chrome浏览器加入CORS过滤后清零
我已经开发使用angular2它运行节点服务器上的一个小应用程序。我的休息服务部署在tomcat服务器中。 我曾经得到
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource
错误,当我做任何职位请求。
我在web.xml中添加
<filter>
<filter-name>CorsFilter</filter-name>
<filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/rest/*</url-pattern>
</filter-mapping>
解决了这个问题。
现在,发布请求可以正常工作,但问题是,在后续请求中访问时,存储在请求会话中的任何内容都会被清除。
例如,我在应用程序启动时有一个登录页面。 当我登录时,我将登录用户的ID存储在请求会话中。 登录后,我将它重定向到主页,我尝试通过从会话中获取来调用另一个请求服务来获取登录用户的ID,但它返回null。
任何人都可以帮我解决这个问题吗?
这发生在Firefox和Chrome浏览器。
它在Edge和IE浏览器中运行良好,因为它们内置了cors支持。
这是代码用于调用服务:
getLoggedInUser(){
return this.http.get('http://localhost:8080/EmployeeReferralManagement/rest/login/get-logged-in-employee').toPromise()
.then(res => res.json() as Employee)
.catch(this.handleError);
}
这是Web服务的快照来电:
REST服务:
@GET
@Path("/get-logged-in-employee")
public Response getLoggedInEmployee() throws Exception {
try {
Employee loggedInEmployee = CommonUtils.checkSession(request);
JSONObject response = new JSONObject();
response.put("employeeId", loggedInEmployee.getEmployeeId());
response.put("designation", loggedInEmployee.getDesignation());
return ResponseUtils.sendResponse(200, response.toString());
} catch (Exception e) {
e.printStackTrace();
return ResponseUtils.sendResponse(500, e.getMessage());
}
}
checkSession方法:
public static Employee checkSession(HttpServletRequest request) throws Exception {
Employee loggedInEmployee = (Employee) request.getSession().getAttribute("employee");
if(loggedInEmployee == null)
throw new Exception("Session Expired");
return loggedInEmployee;
}
可以添加你如何做出请求。这是我的CORS CorsFilter过滤器名称> <滤波器级> org.apache.catalina.filters.CorsFilter滤波器级> cors.allowed.methods GET,POST,PUT ,DELETE,OPTIONS,HEAD param-value> –
Roninio
看起来不错。你可以把请求和回复 – Roninio
@罗尼尼奥,对不起,我没有得到你。我已经添加了休息服务的代码。 –