NutzCN Logo
问答 整合shiro登录时报错
发布于 859天前 作者 qq_519c39e0 5928 次浏览 复制 上一个帖子 下一个帖子
标签:
Authentication failed for token submission [com.chiefdom.authority.shiro.CaptchaUsernamePasswordToken - admin, rememberMe=false (192.168.31.230)].  Possible unexpected error? (Typical or expected login exceptions should extend from AuthenticationException).

异常出现在执行以下代码的subject.login(token)时,不知道什么原因导致的

 try {
            subject.login(token);
            return onLoginSuccess(token, subject, request, response);
        }
        catch (AuthenticationException e) {
            return onLoginFailure(token, e, request, response);
        }
11 回复

抛什么异常,debug出来

刚刚的问题已经解决,是因为登录页面密码的字段和token中的命名不一致导致的。但是调整后遇到新的问题。当成功Login以后,页面提示登录成功,但是却无法跳转到后台首页,再次登录会提示Connection error,刷新页面也没用,要重启后才又能重新登录,且出现一样的情况

没有在session里面放个me属性?

已经放了,debug过经过下面这段代码,是经过了session的赋值的,然后判断请求来源为ajax,最后返回true,页面提示登录成功。但仍出现上文说的connection error的情况

protected boolean onLoginSuccess(AuthenticationToken token, Subject subject, ServletRequest req, ServletResponse resp) throws Exception {
        subject.getSession().setAttribute(NutShiro.SessionKey, subject.getPrincipal());
        if (NutShiro.isAjax(req)) {
            NutShiro.rendAjaxResp(req, resp, new NutMap().setv("ok", true));
            return false;
        }
        return super.onLoginSuccess(token, subject, req, resp);
    }

没有跳转到后台首页, 那是页面js的事情, 因为是ajax, 不会"自动"跳转的

补充贴一些代码
登录页面的js函数:

```
$(function() {
$("#login").click(function() {
$.ajax({
url : "${contextPath}/user/login",
type: "POST",
data:$('#loginForm').serialize(),
error: function(request) {
alert("Connection error");
},
dataType:"json",
success: function(data) {
if (data && data.ok) {
alert("登陆成功");
location.reload();
} else {
alert(data.msg);
}
}
});
return false;
});

     });

补充贴一些代码
登录页面的js函数:

$(function() {
             $("#login").click(function() {
                 $.ajax({
                     url : "${contextPath}/user/login",
                     type: "POST",
                     data:$('#loginForm').serialize(),
                     error: function(request) {
                         alert("Connection error");
                     },
                     dataType:"json",
                     success: function(data) {
                         if (data && data.ok) {
                             alert("登陆成功");
                        	 location.reload();
                         } else {
                             alert(data.msg);
                         }
                     }
                 });
                 return false;
             });
            
         });
	
shiro.ini的配置		

[main]
sha256Matcher = org.apache.shiro.authc.credential.Sha256CredentialsMatcher
nutzdao_realm = com.chiefdom.authority.shiro.realm.NutDaoRealm
nutzdao_realm.credentialsMatcher = $sha256Matcher

authc = com.chiefdom.authority.shiro.CaptchaFormAuthenticationFilter
authc.loginUrl = /user/login
logout.redirectUrl= /user/login

[urls]
/rs/* = anon
/user/logout = logout
/user/error = anon
/user/** = authc

```

没有跳转到后台首页, 那是页面js的事情, 因为是ajax, 不会"自动"跳转的

补充贴一些代码
登录页面的js函数:

```
$(function() {
$("#login").click(function() {
$.ajax({
url : "${contextPath}/user/login",
type: "POST",
data:$('#loginForm').serialize(),
error: function(request) {
alert("Connection error");
},
dataType:"json",
success: function(data) {
if (data && data.ok) {
alert("登陆成功");
location.reload();
} else {
alert(data.msg);
}
}
});
return false;
});

     });

补充贴一些代码
登录页面的js函数:

$(function() {
             $("#login").click(function() {
                 $.ajax({
                     url : "${contextPath}/user/login",
                     type: "POST",
                     data:$('#loginForm').serialize(),
                     error: function(request) {
                         alert("Connection error");
                     },
                     dataType:"json",
                     success: function(data) {
                         if (data && data.ok) {
                             alert("登陆成功");
                        	 location.reload();
                         } else {
                             alert(data.msg);
                         }
                     }
                 });
                 return false;
             });
            
         });
	
shiro.ini的配置		

[main]
sha256Matcher = org.apache.shiro.authc.credential.Sha256CredentialsMatcher
nutzdao_realm = com.chiefdom.authority.shiro.realm.NutDaoRealm
nutzdao_realm.credentialsMatcher = $sha256Matcher

authc = com.chiefdom.authority.shiro.CaptchaFormAuthenticationFilter
authc.loginUrl = /user/login
logout.redirectUrl= /user/login

[urls]
/rs/* = anon
/user/logout = logout
/user/error = anon
/user/** = authc

第一次登录成功时的日志:




2016-05-13 07:54:40,151 org.nutz.mvc.impl.UrlMappingImpl.get(UrlMappingImpl.java:92) DEBUG - Found mapping for [GET] path=/user/login : UserModule.loginPage(UserModule.java:64)
2016-05-13 07:54:40,153 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:153) DEBUG - Get 'userModule'<class com.chiefdom.authority.action.UserModule>
2016-05-13 07:54:40,153 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:173) DEBUG - >> Load definition name=userModule
2016-05-13 07:54:40,154 org.nutz.ioc.loader.combo.ComboIocLoader.load(ComboIocLoader.java:149) DEBUG - Found IocObject(userModule) in IocLoader(AnnotationIocLoader@1502057819)
2016-05-13 07:54:40,154 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:203) DEBUG - >> Make...'userModule'<class com.chiefdom.authority.action.UserModule>
2016-05-13 07:54:40,157 org.nutz.aop.asm.AsmClassAgent.(AsmClassAgent.java:29) DEBUG - AsmClassAgent will define class in Version 50
2016-05-13 07:54:40,166 org.nutz.ioc.impl.ScopeContext.save(ScopeContext.java:59) DEBUG - Save object 'userModule' to [app]
2016-05-13 07:54:40,168 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:153) DEBUG - Get 'userService'<class com.chiefdom.authority.service.UserService>
2016-05-13 07:54:40,168 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:173) DEBUG - >> Load definition name=userService
2016-05-13 07:54:40,168 org.nutz.ioc.loader.combo.ComboIocLoader.load(ComboIocLoader.java:149) DEBUG - Found IocObject(userService) in IocLoader(AnnotationIocLoader@1502057819)
2016-05-13 07:54:40,169 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:203) DEBUG - >> Make...'userService'<class com.chiefdom.authority.service.UserService>
2016-05-13 07:54:40,169 org.nutz.ioc.aop.impl.DefaultMirrorFactory.getMirror(DefaultMirrorFactory.java:84) DEBUG - Load class com.chiefdom.authority.service.UserService without AOP
2016-05-13 07:54:40,169 org.nutz.ioc.impl.ScopeContext.save(ScopeContext.java:59) DEBUG - Save object 'userService' to [app]
2016-05-13 07:54:40,170 org.nutz.service.EntityService.(EntityService.java:36) DEBUG - Get TypeParams for self : com.chiefdom.authority.vo.UserVO
2016-05-13 07:54:40,170 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:153) DEBUG - Get 'dao'<>
2016-05-13 07:54:40,170 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:153) DEBUG - Get 'dao'<interface org.nutz.dao.Dao>
2016-05-13 07:54:40,492 com.chiefdom.system.LogTimeProcessor.process(LogTimeProcessor.java:33) DEBUG - [ GET]URI=/easyTransfer/user/login 339ms
2016-05-13 07:54:40,583 org.nutz.mvc.impl.UrlMappingImpl.get(UrlMappingImpl.java:92) DEBUG - Found mapping for [GET] path=/captcha/next : CaptchaModule.next(CaptchaModule.java:29)
2016-05-13 07:54:40,584 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:153) DEBUG - Get 'captchaModule'<class com.chiefdom.authority.action.CaptchaModule>
2016-05-13 07:54:40,584 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:173) DEBUG - >> Load definition name=captchaModule
2016-05-13 07:54:40,584 org.nutz.ioc.loader.combo.ComboIocLoader.load(ComboIocLoader.java:149) DEBUG - Found IocObject(captchaModule) in IocLoader(AnnotationIocLoader@1502057819)
2016-05-13 07:54:40,584 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:203) DEBUG - >> Make...'captchaModule'<class com.chiefdom.authority.action.CaptchaModule>
2016-05-13 07:54:40,585 org.nutz.ioc.aop.impl.DefaultMirrorFactory.getMirror(DefaultMirrorFactory.java:84) DEBUG - Load class com.chiefdom.authority.action.CaptchaModule without AOP
2016-05-13 07:54:40,585 org.nutz.ioc.impl.ScopeContext.save(ScopeContext.java:59) DEBUG - Save object 'captchaModule' to [app]
2016-05-13 07:54:41,488 com.chiefdom.system.LogTimeProcessor.process(LogTimeProcessor.java:33) DEBUG - [ GET]URI=/easyTransfer/captcha/next 904ms
2016-05-13 07:54:47,581 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:153) DEBUG - Get 'dao'<interface org.nutz.dao.Dao>
2016-05-13 07:54:47,604 org.nutz.dao.impl.sql.run.NutDaoExecutor._runSelect(NutDaoExecutor.java:211) DEBUG - SELECT * FROM cd_auth_user WHERE loginAcount=? LIMIT 0, 1
| 1 |
|-------|
| admin |
For example:> "SELECT * FROM cd_auth_user WHERE loginAcount='admin' LIMIT 0, 1 "
2016-05-13 07:54:47,618 org.apache.shiro.realm.AuthenticatingRealm.getAuthenticationInfo(AuthenticatingRealm.java:569) DEBUG - Looked up AuthenticationInfo [admin] from doGetAuthenticationInfo
2016-05-13 07:54:47,618 org.apache.shiro.realm.AuthenticatingRealm.cacheAuthenticationInfoIfPossible(AuthenticatingRealm.java:507) DEBUG - AuthenticationInfo caching is disabled for info [admin]. Submitted token: [com.chiefdom.authority.shiro.CaptchaUsernamePasswordToken - admin, rememberMe=false (0:0:0:0:0:0:0:1)].
2016-05-13 07:54:47,620 org.apache.shiro.authc.credential.SimpleCredentialsMatcher.equals(SimpleCredentialsMatcher.java:95) DEBUG - Performing credentials equality check for tokenCredentials of type [org.apache.shiro.crypto.hash.SimpleHash and accountCredentials of type [org.apache.shiro.crypto.hash.SimpleHash]
2016-05-13 07:54:47,621 org.apache.shiro.authc.credential.SimpleCredentialsMatcher.equals(SimpleCredentialsMatcher.java:101) DEBUG - Both credentials arguments can be easily converted to byte arrays. Performing array equals comparison
2016-05-13 07:54:47,621 org.apache.shiro.authc.AbstractAuthenticator.authenticate(AbstractAuthenticator.java:231) DEBUG - Authentication successful for token [com.chiefdom.authority.shiro.CaptchaUsernamePasswordToken - admin, rememberMe=false (0:0:0:0:0:0:0:1)]. Returned account [admin]
2016-05-13 07:54:47,621 org.apache.shiro.subject.support.DefaultSubjectContext.resolveSecurityManager(DefaultSubjectContext.java:102) DEBUG - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup.
2016-05-13 07:54:47,621 org.apache.shiro.mgt.DefaultSecurityManager.resolveSession(DefaultSecurityManager.java:436) DEBUG - Context already contains a session. Returning.
2016-05-13 07:54:47,622 org.apache.shiro.subject.support.DefaultSubjectContext.resolveSecurityManager(DefaultSubjectContext.java:102) DEBUG - No SecurityManager available in subject context map. Falling back to SecurityUtils.getSecurityManager() lookup.
2016-05-13 07:54:47,622 org.apache.shiro.web.servlet.SimpleCookie.addCookieHeader(SimpleCookie.java:226) DEBUG - Added HttpServletResponse Cookie [rememberMe=deleteMe; Path=/easyTransfer; Max-Age=0; Expires=Wed, 11-May-2016 23:54:47 GMT]
2016-05-13 07:54:47,622 org.apache.shiro.mgt.AbstractRememberMeManager.onSuccessfulLogin(AbstractRememberMeManager.java:300) DEBUG - AuthenticationToken did not indicate RememberMe is requested. RememberMe functionality will not be executed for corresponding account.


这时候被跳回到登录页面,尝试过执行 location.reload()和window.location.url重定向到其他页面都无法正常进入。

2016-05-13 07:57:20,840 org.nutz.mvc.impl.ActionInvoker.getActionChain(ActionInvoker.java:84) DEBUG - Path=[/user/login] available methods[GET] but request [POST], using the wrong http method?
2016-05-13 07:57:20,840 org.nutz.mvc.impl.UrlMappingImpl.get(UrlMappingImpl.java:101) DEBUG - Search mapping for path=/user/login : NOT Action match

```

求大神帮忙分析一下问题所在

没有跳转到后台首页, 那是页面js的事情, 因为是ajax, 不会"自动"跳转的

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