我是一名学生,为我们的班级项目创建了一个Web应用程序,客户端和球衣1.x的角度为7。 在服务器端。我创建了一个有角度的拦截器来捕获任何错误。我的服务器端有一个登录筛选器,用于验证在访问服务之前已记录客户端。 我想要的是拦截器捕获的错误,通过错误的状态标识错误(401),并通过编程的注销对其做出反应。通过HttpErrorResponse,我从过滤器得到的是空白:
HttpErrorResponse {headers: HttpHeaders, status: 0, statusText: "Unknown
Error", url:
"http://localhost:8080/CouponManager.web/rest/AdminService/Companies", ok:
false, …}
error: ProgressEvent {isTrusted: true, lengthComputable: false, loaded: 0,
total: 0, type: "error", …}
headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, headers:
Map(0)}
message: "Http failure response for
http://localhost:8080/CouponManager.web/rest/AdminService/Companies: 0
Unknown Error"
name: "HttpErrorResponse"
ok: false
status: 0
statusText: "Unknown Error"
url:
"http://localhost:8080/CouponManager.web/rest/AdminService/Companies"
__proto__: HttpResponseBase
这是我从其他服务的异常中得到的信息:
error-interceptor.interceptor.ts:16
HttpErrorResponse {headers: HttpHeaders, status: 500, statusText: "OK",
url"http://localhost:8080/CouponManager.web/rest/AdminService/Companies",
ok: false, …}
error: {message: "Company with name aaa already in database"}
headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null,
lazyInit: ƒ}
message: "Http failure response for
http://localhost:8080/CouponManager.web/rest/AdminService/Companies: 500
OK"
name: "HttpErrorResponse"
ok: false
status: 500
statusText: "OK"
url:
"http://localhost:8080/CouponManager.web/rest/AdminService/Companies"
_ _proto__:HttpResponseBase
以下是过滤器:
@WebFilter({"/LoginFilter", "/rest/AdminService/*",
"/rest/CompanyService/*","/rest/CustomerService/*" })
public class LoginFilter implements Filter {
public LoginFilter(){}
public void destroy() {}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) throws IOException, ServletException
{
HttpSession session = ((HttpServletRequest) request).getSession(false);
String method = ((HttpServletRequest) request).getMethod();
//filter test
if (session == null) {
System.out.println("session is null");
}
//login and facade tests + cors request
if (session != null
&& session.getAttribute("clientFacade") instanceof CouponClientFacade
&& (boolean)session.getAttribute("loggedIn")
|| method.equals("OPTIONS")) {
System.out.println("filter works");
filterChain.doFilter(request, response);
} else {
System.out.println(" filter error ");;
((HttpServletResponse)response).sendError(401, "You are not logged
into the system");
}
}
}
我还将提供一个服务方法示例,供您查看我正在使用的方法:
@GET
@Path("Items")
public Response getAllItems() {
HttpSession session = request.getSession(true);
try {
//code excuted here
} catch(Exception e) {
String json = String.format("{\"message\": \"%s\"}",
e.getMessage());
return Response.serverError().entity(json).build();
}
}