比如,@RequiresUser,当用户没有登录的时候,需要捕获异常,然后向前台发送一个json,这个如何实现啊?
6 回复
var chain={
"default" : {
"ps" : [
"com.fqx.zb.reportsys.LogTimeProcessor",
"org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor",
"org.nutz.mvc.impl.processor.EncodingProcessor",
"org.nutz.mvc.impl.processor.ModuleProcessor",
"org.nutz.integration.shiro.NutShiroProcessor",
"org.nutz.mvc.impl.processor.ActionFiltersProcessor",
"org.nutz.mvc.impl.processor.AdaptorProcessor",
"org.nutz.mvc.impl.processor.MethodInvokeProcessor",
"org.nutz.mvc.impl.processor.ViewProcessor"
],
"error" : 'com.fqx.zb.reportsys.RptFailProcessor'
}
};
package com.fqx.zb.reportsys;
import org.apache.shiro.authz.AuthorizationException;
import org.nutz.integration.shiro.NutShiro;
import org.nutz.lang.util.NutMap;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.ActionContext;
import org.nutz.mvc.ActionInfo;
import org.nutz.mvc.Mvcs;
import org.nutz.mvc.NutConfig;
import org.nutz.mvc.impl.processor.ViewProcessor;
public class RptFailProcessor extends ViewProcessor {
private static final Log log = Logs.get();
@Override
public void init(NutConfig config, ActionInfo ai) throws Throwable {
view = evalView(config, ai, ai.getFailView());
}
@Override
public void process(ActionContext ac) throws Throwable {
if (log.isWarnEnabled()) {
String uri = Mvcs.getRequestPath(ac.getRequest());
log.warn(String.format("Error@%s :", uri), ac.getError());
}
Throwable e = ac.getError();
// 捕获shiro异常
log.debug("进入异常", e);
if (e instanceof AuthorizationException ) {
log.debug("进入异常", e);
NutShiro.rendAjaxResp(ac.getRequest(), ac.getResponse(), new NutMap().setv("needlogin", true));
return;
}
super.process(ac);
}
}
为什么捕获不到异常呢?如果没有登陆用户,shiro直接跳到shiroini配置的登陆地址,都没有到这里面来
添加回复
请先登陆