【问题标题】:Apache Shiro IncorrectCredentialsExceptionApache Shiro IncorrectCredentialsException
【发布时间】:2016-05-07 05:47:20
【问题描述】:

这是我第一次使用 apache shiro,请多多包涵。

我的 shiro.ini

#   =============================================================================
# Quickstart INI Realm configuration
#
# =============================================================================
[main]
jdbcRealm = org.apache.shiro.realm.jdbc.JdbcRealm
jdbcRealm.permissionsLookupEnabled = true
ds = shiro.ShiroBoneCPDataSource
jdbcRealm.dataSource=$ds 
jdbcRealm.authenticationQuery = SELECT password FROM users WHERE username = ?
jdbcRealm.userRolesQuery = SELECT role_id FROM user_role WHERE user_id = (SELECT id FROM users WHERE username = ?)
jdbcRealm.permissionsQuery = SELECT permission FROM role_permission WHERE role_id = ?



hashService = org.apache.shiro.crypto.hash.DefaultHashService
# NONE of the hashService settings is required.  The defaults will work fine.

hashService.hashAlgorithmName = SHA-256
hashService.generatePublicSalt = true
# If you wanted to use some private salt bytes, provide in Base64 (NOT A BAD IDEA!)
hashService.privateSalt = aGltYWxheWFu

# We use this one to create a new test user with a hashed password.
passwordService = org.apache.shiro.authc.credential.DefaultPasswordService
passwordService.hashService = $hashService

# We use this for our authentication tests.
passwordMatcher = org.apache.shiro.authc.credential.PasswordMatcher
passwordMatcher.passwordService = $passwordService
jdbcRealm.credentialsMatcher = $passwordMatcher

securityManager.realms = $jdbcRealm
# -----------------------------------------------------------------------------
# Users and their assigned roles
#
# Each line conforms to the format defined in the
# org.apache.shiro.realm.text.TextConfigurationRealm#setUserDefinitions JavaDoc
#   -----------------------------------------------------------------------------
[users]


# -----------------------------------------------------------------------------
# Roles with assigned permissions
# 
# Each line conforms to the format defined in the
# org.apache.shiro.realm.text.TextConfigurationRealm#setRoleDefinitions JavaDoc
# -----------------------------------------------------------------------------
[roles]

我在这里使用这个 shiro.ini 文件

public Subject authenticateWithShiro(String username, char[] pass) {

    Subject currentUser = null;
    try {
            log.info("Authentication with shiro is started...");

            Factory<org.apache.shiro.mgt.SecurityManager> factory = new IniSecurityManagerFactory(
                            "classpath:resources/shiro.ini");
            org.apache.shiro.mgt.SecurityManager securityManager = factory
                            .getInstance();
            SecurityUtils.setSecurityManager(securityManager);

            currentUser = SecurityUtils.getSubject();

            Session session = currentUser.getSession();
            session.setAttribute("someKey", "aValue");
            String value = (String) session.getAttribute("someKey");
            if (value.equals("aValue")) {
                    log.info("Retrieved the correct value! [" + value + "]");
            }

            // let's login the current user so we can check against roles and
            // permissions:
            if (!currentUser.isAuthenticated()) {
                System.out.println("Current user has been authenticated.");
                    UsernamePasswordToken token = new UsernamePasswordToken(
                                    username, pass);
                    token.setRememberMe(true);
                    currentUser.login(token);

            }
    } catch (Exception e) {
            log.error("Authentication failed", e);
            log.error(e.getMessage());
    }

    return currentUser;
   }

因此,在执行以下代码时,org.apache.shiro.authc.IncorrectCredentialsException: Submitted credentials for token [org.apache.shiro.authc.UsernamePasswordToken - yunusa, rememberMe=true] 与预期的凭据不匹配

Subject subject = new UserHandler().authenticateWithShiro(username, textPassword.getPassword());
        if (subject != null && subject.isAuthenticated()) {
                System.out.println("successfull login");
        } else {
                System.out.println("Failed log in");
        }

【问题讨论】:

    标签: java apache security authentication shiro


    【解决方案1】:

    在尝试通过stackoverflow问题寻找答案但没有任何成功之后,我终于决定检查我的INI(shiro.ini)并发现了一个小错误。我一开始设置hashService.generatePublicSalt为true,同时配置了private salt,貌似是个错误。

    当我移除私人盐时,一切都像魅力一样。

    【讨论】:

      【解决方案2】:

      我也是shiro的新手。我相信您可能错过了在 ini 文件中添加 Auth 令牌信息。 添加:

      yourID = passWord
      

      [users] 部分下。

      希望对你有帮助!

      【讨论】:

      • 什么是身份验证令牌信息?我应该添加哪个 ID 和密码?提前致谢
      • 是的,您的用户名和密码。
      • 用户表中已经有用户,包括我的!你说的是随机的 id/密码还是我必须在某个地方找到它?再次抱歉有太多问题。
      • 您是否在您的 ini 文件中输入了您期望的用户 ID?
      • 我已经在我的ini文件代码上方发布了,我不期待任何东西
      猜你喜欢
      • 2015-07-22
      • 2017-09-20
      • 2017-01-04
      • 2013-03-13
      • 2017-12-29
      • 2014-08-31
      • 2013-10-25
      • 2017-10-02
      • 2013-06-11
      相关资源
      最近更新 更多