NutzCN Logo
问答 slog记录uid不正常???
发布于 2617天前 作者 老司机 1530 次浏览 复制 上一个帖子 下一个帖子
标签:

首先贴出realm方法

  protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        SimpleShiroToken upToken = (SimpleShiroToken) token;
        User user = (User) upToken.getPrincipal();
        return new SimpleAccount(user.getId(), user.getName(), getName());
    }

slogservice里面每次到

         if (uid != null && uid instanceof Number)
                slog.setUid(((Number)uid).intValue());

的时候直接跳过不执行,debug显示的uid是1啊!
然后我自己重写了方法才好的

 public static Callable<Integer> GET_USER_ID = new Callable<Integer>() {
        public Integer call() throws Exception {
            Integer u;
            try {
                u = Integer.valueOf(SecurityUtils.getSubject().getPrincipal().toString());
            } catch (Throwable e) {
                return Integer.valueOf(-1);
            }
            if (u != null) {
                return u;
            }
            return Integer.valueOf(0);
        }
    };
    public void log(String t, String tag, String source, String msg, boolean async) {
        SlogBean slog = c(t, tag, source, msg);
        try {
            Integer uid = GET_USER_ID.call();
            if (uid != null && uid instanceof Number)
                slog.setUid(uid);
        }
        catch (Exception e) {
            log.debug("get user id fail", e);
        }
        if (async)
            async(slog);
        else
            sync(slog);
    }
12 回复

看了看逻辑,的确有问题的样子,默认回调总是返回String,导致uid总是不对

@wendal 那么顺便把slogbean的uid设成string呗,如果我把shiro的getPrincipal设为用户名,那岂不是记录不了,而且对于脑残的客户来说,他不需要知道哪个id的人做了哪些事,而是想直观地知道,到底谁(名字)做了哪些事,不是吗?

来自炫酷的 NutzCN

恩,考虑一下

删除了id属性, 新增的username属性.

@wendal
兼容性提醒
求详解下。这个变动是无影响的升级吗?
删除的是主键id?还是user_id? 看2楼的意思是user_id。如果是user_id的话,还是不要删除的好。

@qq_a930202f 忘记考虑删除了。。。

不过,根据uid/日志时间/内容,基本上能匹配到特定记录

没看懂。。。。请明示啊
貌似意思是主键?

@qq_a930202f 没有主键

	@Id
	private long id;
	
	@Column("t")// aop.before aop.after aop.error
	private String t;
	
	@Column("tg")
	private String tag; 
	
	@Column("src")
	@ColDefine(width=1024)
	private String source;
	
	@Column("u_id")
	private int uid;
	
	@Column("ip")
	private String ip;
	
	@Column
	@ColDefine(width=8192)
	private String msg;

	@Column("ct")
	protected Date createTime;

@wendal
个人感觉1.60这个就挺好的啊,不用动了,username需要的话,关联一下就取到了。这种基础的功能插件还是简单点好,复杂了通用性就下降了

@qq_a930202f 自增会有问题,两台机器的时候,不过slogservice可自定义,弄成自己想要的就好了

@wendal
那用UUID呢?最好还是有主键。

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