获取异常apache shiro中的令牌提交认证失败

时间:2013-10-29 09:38:17

标签: java shiro

我是apache shiro中的新手。我执行此声明时会遇到异常。

currentUser.login(token);

例外是

 errororg.apache.shiro.authc.AuthenticationException: Authentication failed for token submission [org.apache.shiro.authc.UsernamePasswordToken - abc@gmail.com, rememberMe=true].  Possible unexpected error? (Typical or expected login exceptions should extend from AuthenticationException).

我正在调用此方法进行登录。代码为。

  public boolean authorize(String username,String password)
{
    Boolean status=false;
    log.debug("the user id "+username+"passwrodD::"+password);
    Realm realm = new JdbcRealm();
    DefaultSecurityManager securityManager = new DefaultSecurityManager(realm);
    UsernamePasswordToken token = new UsernamePasswordToken(username, password);
    token.setRememberMe(true);
    SecurityUtils.setSecurityManager(securityManager);
    Subject currentUser = SecurityUtils.getSubject();

    Response r = null;
    log.debug("before process for login");
    try
    {
        currentUser.login(token);   //This throws an error upon form submission

        r = Response.ok().entity(token).build();            

    }
    catch (UnknownAccountException uae ) {
        //username wasn't in the system, show them an error message?
        System.out.println("the user name is invalid");
    } catch ( IncorrectCredentialsException ice ) {
        //password didn't match, try again?
        System.out.println("the password name is invalid");
    } catch ( LockedAccountException lae ) {
        //account for that username is locked - can't login.  Show them a message?

    } catch ( AuthenticationException ae ) {
        //unexpected condition - error?
        System.out.println("unexpect error"+ae);
    }
    return status;
}

我的shiro.ini文件

 [main]
 jdbcRealm = org.apache.shiro.realm.jdbc.JdbcRealm
 jdbcRealm.permissionsLookupEnabled = true
 jdbcRealm.authenticationQuery =select User_Password FROM  user_master where User_id=?
 ds = com.mysql.jdbc.jdbc2.optional.MysqlDataSource
 ds.serverName = localhost
 ds.user = root
 ds.password = root
 ds.databaseName = test
 jdbcRealm.dataSource = $ds
 [users]
 [roles]
 [urls]

我在我的web.xml文件中包含了监听器和过滤器。 我将authenticationQuery更改为我的查询。当我执行时,我得到上述错误。我也知道修改或覆盖查询是正确的方法。

2 个答案:

答案 0 :(得分:0)

我认为问题在于您securityManager.realm = $jdbcRealm

中缺少shiro.ini

答案 1 :(得分:0)

我刚刚遇到此异常,问题是我在securityManager.realm中错误地设置了shiro.ini。这就是我所拥有的:

[main]
fooRealm = com.company.foo.Realm
securityManager.realms = fooRealm

这就是修复它的原因(我错过了$):

[main]
fooRealm = com.company.foo.Realm
securityManager.realms = $fooRealm