public
class
TokenInterceptor
implements
Filter{
private
LoginApi loginApi =
new
LoginApiImpl();
private
String tokenErrorUrl=
"http://127.0.0.1:5033/token/tokenError"
;
@Override
public
Result invoke(Invoker<?> invoker, Invocation invocation)
throws
RpcException {
HttpServletRequest request = (HttpServletRequest)RpcContext.getContext().getRequest();
HttpServletResponse response = (HttpServletResponse)RpcContext.getContext().getResponse();
String token = request.getParameter(
"token"
);
boolean
flag = loginApi.validToken(token);
if
(flag) {
return
invoker.invoke(invocation);
}
else
{
AppDomain app=
new
AppDomain();
app.setMessage(
"900"
);
app.setData(
null
);
print(JsonUtil.toJson(app),response);
return
new
RpcResult();
}
}
protected
void
print(String str,HttpServletResponse response) {
PrintWriter writer =
null
;
try
{
response.setContentType(
"text/html; charset=utf-8"
);
writer = response.getWriter();
writer.print(str);
}
catch
(Exception e) {
}
finally
{
CloseUtil.close(writer);
}
}
public
LoginApi getLoginApi() {
return
loginApi;
}
public
void
setLoginApi(LoginApi loginApi) {
this
.loginApi = loginApi;
}
public
String getTokenErrorUrl() {
return
tokenErrorUrl;
}
public
void
setTokenErrorUrl(String tokenErrorUrl) {
this
.tokenErrorUrl = tokenErrorUrl;
}
}
网友评论