NutzCN Logo
问答 自定义全局日志后,shiro报错
发布于 2257天前 作者 文涛(wentao) 1469 次浏览 复制 上一个帖子 下一个帖子
标签:

具体情况如下:
在MainLauncher上增加了 @ChainBy(type= LogChainMaker.class, args={})
LogChainMaker.class文件内容如下

public class LogChainMaker implements ActionChainMaker {

    @Override
    public ActionChain eval(NutConfig config, ActionInfo ai) {
        // 提醒: config可以获取ioc等信息, ai可以获取方法上的各种配置及方法本身
        // 正常处理的列表
        List<Processor> list = new ArrayList<>();
        list.add(new LogTimeProcessor()); // 设置日志处理类
        list.add(new UpdateRequestAttributesProcessor()); // 设置base/msg等内置属性
        list.add(new EncodingProcessor()); // 设置编码信息@Encoding
        list.add(new ModuleProcessor()); // 获取入口类的对象,从ioc或直接new
        list.add(new ActionFiltersProcessor()); // 处理@Filters
        list.add(new AdaptorProcessor()); // 处理@Adaptor
        list.add(new MethodInvokeProcessor()); // 执行入口方法
        list.add(new ViewProcessor()); // 对入口方法进行渲染@Ok
        for (Processor p : list) {
            try {
                p.init(config, ai);
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        }
        // 最后是专门负责兜底的异常处理器,这个处理器可以认为是全局异常处理器,对应@Fail
        Processor error = new FailProcessor();
        try {
            error.init(config, ai);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return new NutActionChain(list, error, ai);
    }
}

访问页面报错:

VAR_NOT_DEFINED
	at org.beetl.core.statement.VarRef.getValue(VarRef.java:198)
	at org.beetl.core.statement.VarRef.evaluate(VarRef.java:76)
	at org.beetl.core.statement.PlaceholderST.execute(PlaceholderST.java:34)
	at org.beetl.core.statement.Program.execute(Program.java:70)
	at org.beetl.core.Template.renderTo(Template.java:136)
	at org.beetl.core.Template.renderTo(Template.java:102)
	at org.beetl.ext.web.WebRender.render(WebRender.java:122)
	at org.beetl.ext.nutz.BeetlView.render(BeetlView.java:28)
	at org.nutz.mvc.impl.processor.ViewProcessor.process(ViewProcessor.java:66)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.MethodInvokeProcessor.process(MethodInvokeProcessor.java:33)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.AdaptorProcessor.process(AdaptorProcessor.java:38)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.ActionFiltersProcessor.process(ActionFiltersProcessor.java:58)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.ModuleProcessor.process(ModuleProcessor.java:123)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.EncodingProcessor.process(EncodingProcessor.java:27)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor.process(UpdateRequestAttributesProcessor.java:15)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at com.iptv.ad_auth_manage.telecom.utils.LogTimeProcessor.process(LogTimeProcessor.java:29)
	at org.nutz.mvc.impl.NutActionChain.doChain(NutActionChain.java:44)
	at org.nutz.mvc.impl.ActionInvoker.invoke(ActionInvoker.java:67)
	at org.nutz.mvc.ActionHandler.handle(ActionHandler.java:31)
	at org.nutz.mvc.NutFilter.doFilter(NutFilter.java:202)
	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1642)
	at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449)
	at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365)
	at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)
	at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)
	at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383)
	at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362)
	at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1642)
	at com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:123)
	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1642)
	at org.nutz.mvc.WhaleFilter.doFilter(WhaleFilter.java:171)
	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1634)
	at org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter.doFilter(WebSocketUpgradeFilter.java:215)
	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1642)
	at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:533)
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:146)
	at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:548)
	at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
	at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:257)
	at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1595)
	at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:255)
	at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1340)
	at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:203)
	at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:473)
	at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1564)
	at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:201)
	at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1242)
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:144)
	at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
	at org.eclipse.jetty.server.Server.handle(Server.java:503)
	at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:364)
	at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:260)
	at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:305)
	at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:103)
	at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:118)
	at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.runTask(EatWhatYouKill.java:333)
	at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:310)
	at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.tryProduce(EatWhatYouKill.java:168)
	at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.produce(EatWhatYouKill.java:132)
	at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:765)
	at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:683)
	at java.lang.Thread.run(Thread.java:748)
[DEBUG] 18:10:04.419 org.beetl.ext.nutz.LogErrorHandler.processExcption(LogErrorHandler.java:32) - null
>>06:10:04:变量未定义(VAR_NOT_DEFINED):obj 位于41行 资源:/tml/main.btl
38|                            <span><img alt="image" src="/img/logo.png" style="width:100%" /></span>
39|                            <a data-toggle="dropdown" class="dropdown-toggle" href="#">
40|                                <span class="clear">
41|                               <span class="block m-t-xs"><strong class="font-bold">${obj.userName}</strong></span>
42|                                <span class="text-muted text-xs block">${obj.realName}<b class="caret"></b></span>
43|                                </span>
44|                            </a>

把 @ChainBy(type= LogChainMaker.class, args={}) 注解去掉,问题就没有了

17 回复

nutzboot吗?

话说,缺了ShiroProcessor呢

嗯,加了NutShiroProcessor()就可以了,那是不是这种形式,只要引用了新的模块,都需要在eval方法内增加对应的Processor

新模块是指什么?? 你加的基本上就是全部默认Processor了, 然后有两个可选的, Shiro和校验的, 应该不存在其他的了吧

哦,校验的是指的哪个?

org.nutz.plugins.validation.ValidationProcessor

@wendal 加入这个了还是不行,登录不了,也不报错
登录部分代码

@At("/manage")
    @Ok("beetl:/tml/main.btl")
    @RequiresUser
    public UserModel index() {
        Subject subject = SecurityUtils.getSubject();
        UserModel userModel = (UserModel)subject.getPrincipal();
        return userModel;
    }

SimpleAuthorizingRealm代码:

@IocBean(name="shiroRealm")
public class SimpleAuthorizingRealm extends AbstractSimpleAuthorizingRealm {

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        if (principals == null) {
            throw new AuthorizationException("PrincipalCollection method argument cannot be null.");
        }
        UserModel user = (UserModel) principals.getPrimaryPrincipal();
        if (user == null)
            return null;
        SimpleAuthorizationInfo auth = new SimpleAuthorizationInfo();
        auth.addRole(user.getRealName());
        for (String authPermission : user.getAuthPermissions()) {
            Logs.get().debugf("User [%s] addStringPermission - [%s]", user.getUserName(), authPermission);
            auth.addStringPermission(authPermission);
        }
        return auth;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        SimpleShiroToken upToken = (SimpleShiroToken) token;

        UserModel user = (UserModel)upToken.getPrincipal();
        if (user == null)
            return null;
        return new SimpleAccount(user, user.getPassword(), user.getRealName());
    }
}

依然是去掉@ChainBy(type= LogChainMaker.class, args={}) 后,就可以正常登录了

日志里面有打印动作链的配置, 对比一下里面的处理器顺序

不知道咋看
我把NutShiroProcessor放在第一个也不行

public class LogChainMaker implements ActionChainMaker {

    @Override
    public ActionChain eval(NutConfig config, ActionInfo ai) {
        // 提醒: config可以获取ioc等信息, ai可以获取方法上的各种配置及方法本身
        // 正常处理的列表
        List<Processor> list = new ArrayList<>();
        list.add(new NutShiroProcessor()); // 设置shiro处理类
        list.add(new LogTimeProcessor()); // 设置日志处理类
        list.add(new UpdateRequestAttributesProcessor()); // 设置base/msg等内置属性
        list.add(new EncodingProcessor()); // 设置编码信息@Encoding
        list.add(new ModuleProcessor()); // 获取入口类的对象,从ioc或直接new
        list.add(new ActionFiltersProcessor()); // 处理@Filters
        list.add(new AdaptorProcessor()); // 处理@Adaptor
        list.add(new MethodInvokeProcessor()); // 执行入口方法
        list.add(new ViewProcessor()); // 对入口方法进行渲染@Ok
        for (Processor p : list) {
            try {
                p.init(config, ai);
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        }
        // 最后是专门负责兜底的异常处理器,这个处理器可以认为是全局异常处理器,对应@Fail
        Processor error = new FailProcessor();
        try {
            error.init(config, ai);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return new NutActionChain(list, error, ai);
    }
}

找找启动日志,nutz mvc启动那段,里面有动作链的完整列表

默认是这个顺序

{
	"default" : {
		"ps" : [
		      "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.plugins.validation.ValidationProcessor",
		      "org.nutz.mvc.impl.processor.MethodInvokeProcessor",
		      "org.nutz.mvc.impl.processor.ViewProcessor"
		      ],
		"error" : 'org.nutz.mvc.impl.processor.FailProcessor'
	}
}

其中的叹号代表可选, 没这个类就忽略的意思

实在不会看,搜索processor也没有,但是我按照你发的,把NutShiroProcessor放到ModuleProcessor的后面就可以了

额, 正常了?

给你贴例子

20:00:17.063 DEBUG (Scans.java:280) scan - Found 1 resource by src( mvc/nutzbook-mvc-chain.js ) , regex( ^(.+[.])(js|json)$ )
20:00:17.069 DEBUG (JsonActionChainMakerConfiguretion.java:37) <init> - ActionChain Config:
{
   "default": {
      "ps": ["net.wendal.nutzbook.common.mvc.LogTimeProcessor", "net.wendal.nutzbook.common.mvc.DailyUniqueUsersProcessor", "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": "org.nutz.mvc.impl.processor.FailProcessor"
   }
}
20:00:17.069 DEBUG (NutLoading.java:267) createChainMaker - @ChainBy(org.nutz.mvc.impl.NutActionChainMaker)

我的nutzboot,真没有这个打印

加了自定义@ChainBy就没有了,那是默认的JsonActionChainMakerConfiguretion打印的

添加回复
请先登陆
回到顶部