NutzCN Logo
问答 shiro异常
发布于 3087天前 作者 明天会吹什么风 4016 次浏览 复制 上一个帖子 下一个帖子
标签: shiro

[33]
短点链接: https://nutz.cn/s/c/33

29 回复

请问这是什么原因?

总得有点上下文描述吧???? 啥情况下,啥版本,啥配置?????

@wendal 我Nutz集成Shiro,然后登陆的时候,调用user/login的时候就报这个错误。

@qq_74967e40 不是走ajax登陆?

@wendal 不是的。。。一定要用ajax才行?

@qq_74967e40 我查查哪里的问题

@wendal 我怎么看到r3了

那是 1.b.52.r3

@wendal 不过我给你用的r2.。。问题解决了。。。囧

@wendal 问一下,如果我不用验证码,如何设置?

自行改造

来自炫酷的 NutzCN

另外,严重建议使用验证码及密码加盐,无论是不是内部系统

来自炫酷的 NutzCN

@wendal
我用了验证码一直报这个异常。
2015-11-07 17:08:59,837 [http-nio-80-exec-69] DEBUG org.apache.shiro.realm.AuthenticatingRealm - Looked up AuthenticationInfo [null] from doGetAuthenticationInfo
2015-11-07 17:08:59,838 [http-nio-80-exec-69] DEBUG org.apache.shiro.realm.AuthenticatingRealm - No AuthenticationInfo found for submitted AuthenticationToken [org.nutz.integration.shiro.CaptchaUsernamePasswordToken - null, rememberMe=false (198.1.16.12)]. Returning null.
2015-11-07 17:08:59,838 [http-nio-80-exec-69] DEBUG org.apache.shiro.web.servlet.SimpleCookie - Added HttpServletResponse Cookie [rememberMe=deleteMe; Path=/jg; Max-Age=0; Expires=Fri, 06-Nov-2015 09:08:59 GMT]

@wendal 如果我不输入验证码 就不会有这个异常 当然验证也不会通过

@wendal 是不是因为 我没有给用户任何权限和资源信息呢?
我现在只有user信息,其他role和permission都没有

@wendal 看样子不是。。。
Looked up AuthenticationInfo [null] from doGetAuthenticationInfo 为什么找不到凭证呢

没配置realm?

@wendal shiro.ini里有明确的配置啊
nutzdao_realm = com.jg.shiro.realm.NutDaoRealm

那句log应该是这个方法打印的:

org.apache.shiro.realm.AuthenticatingRealm.getAuthenticationInfo

    public final AuthenticationInfo getAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

        AuthenticationInfo info = getCachedAuthenticationInfo(token);
        if (info == null) {
            //otherwise not cached, perform the lookup:
            info = doGetAuthenticationInfo(token); // 这里是关键, NutDaoRealm覆盖了这个方法
            log.debug("Looked up AuthenticationInfo [{}] from doGetAuthenticationInfo", info);
            if (token != null && info != null) {
                cacheAuthenticationInfoIfPossible(token, info);
            }
        } else {
            log.debug("Using cached authentication info [{}] to perform credentials matching.", info);
        }

        if (info != null) {
            assertCredentialsMatch(token, info);
        } else {
            log.debug("No AuthenticationInfo found for submitted AuthenticationToken [{}].  Returning null.", token);
        }

        return info;
    }

所以, 要不就是你做的NutDaoRealm没覆盖这个doGetAuthenticationInfo方法,要不就是这个方法的确返回了null

这是nutz.cn里面的NutDaoRealm的写法

	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
	    // TODO 新版shiro插件已经在CaptchaFormAuthenticationFilter判断了,移除多余的验证码判断逻辑
		CaptchaUsernamePasswordToken upToken = (CaptchaUsernamePasswordToken) token;
		
		if (Strings.isBlank(upToken.getCaptcha()))
			throw new AuthenticationException("验证码不能为空");
		String _captcha = Strings.sBlank(SecurityUtils.getSubject().getSession(true).getAttribute(Toolkit.captcha_attr));
		if (!upToken.getCaptcha().equalsIgnoreCase(_captcha))
			throw new AuthenticationException("验证码错误");
		
		User user = dao().fetch(User.class, Cnd.where("name", "=", upToken.getUsername()));
        if (user == null)
            return null; // 没这个用户
        if (user.isLocked()) 
            throw new LockedAccountException("Account [" + upToken.getUsername() + "] is locked.");
        SimpleAccount account = new SimpleAccount(user.getId(), user.getPassword(), getName());
        account.setCredentialsSalt(ByteSource.Util.bytes(user.getSalt()));
        return account;
	}

干掉验证码逻辑的之后的方法

	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		CaptchaUsernamePasswordToken upToken = (CaptchaUsernamePasswordToken) token;
		
		User user = dao().fetch(User.class, Cnd.where("name", "=", upToken.getUsername()));
        if (user == null)
            return null;
        if (user.isLocked()) 
            throw new LockedAccountException("Account [" + upToken.getUsername() + "] is locked.");
        SimpleAccount account = new SimpleAccount(user.getId(), user.getPassword(), getName());
        account.setCredentialsSalt(ByteSource.Util.bytes(user.getSalt()));
        return account;
	}

@wendal 终于知道原因了。。。
你nutbook给的user这个对象的用户名是name
然而验证的时候用的username.....
所以一直验证不通过。。。。囧啊

@qq_74967e40 额,我检查一下是不是这样写

来自炫酷的 NutzCN

@wendal 是的,你页面和action都是对的
但是User对象里用的是name
所以我没注意。。。能统一一下就好了。

另外~如果集成beetl 这个session怎么用呢?
直接用Httpsession里面有一个默认的me,是用的user的id
即使我覆盖也没用
如果用SecurityUtils.getSubject().getSession().setAttribute("me",user)
好像也没用啊
搞不懂了
前台用${session.me}只能得到id:1

主模块加上这个试试

@SessionBy(ShiroSessionProvider.class)

@wendal 已经解决
不过不是因为这个注解的问题。
是因为我是在user/login这个方法里去建立session的
然而这个方法因为被shiro接管了,所以并没有进入过

@wendal 所以都在NutDaoRealm里去做这件事了

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