简体   繁体   English

Apache Shiro IncorrectCredentialsException

[英]Apache Shiro IncorrectCredentialsException

This is my first time using apache shiro, so bear with me. 这是我第一次使用apache shiro,请多多包涵。

my shiro.ini 我的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]

I use this shiro.ini file here 我在这里使用这个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;
   }

So, when executing below code, org.apache.shiro.authc.IncorrectCredentialsException: Submitted credentials for token [org.apache.shiro.authc.UsernamePasswordToken - yunusa, rememberMe=true] did not match the expected credentials 因此,在执行以下代码时,org.apache.shiro.authc.IncorrectCredentialsException:提交的令牌凭证[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");
        }

I am also new to shiro. 我也是Shiro的新手。 I believe you probably missed adding the Auth token information in your ini file. 我相信您可能会错过在ini文件中添加Auth令牌信息的信息。 Add: 加:

yourID = passWord

Under [users] section. [users]部分下。

Hope it helps! 希望能帮助到你!

After so much time trying to go through stackoverflow questions in search of an answer without any success, i finally decided to go through my INI(shiro.ini) and found a minor mistake. 经过这么多时间试图通过stackoverflow问题来寻找答案而没有获得任何成功,我终于决定通过我的INI(shiro.ini)并发现一个小错误。 I initially set hashService.generatePublicSalt to true, and the same time configured private salt, which seemed to be a mistake. 我最初将hashService.generatePublicSalt设置为true,并且同时配置了私有盐,这似乎是一个错误。

When i remove the private salt, everything worked like a charm. 当我移走私人盐时,一切都像魅力一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM