gpt4 book ai didi

java - Apache Shiro 错误凭据异常

转载 作者:太空宇宙 更新时间:2023-11-04 12:59:05 24 4
gpt4 key购买 nike

这是我第一次使用 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.In CorrectCredentialsException:提交的 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");
}

最佳答案

我也是shiro的新手。我相信您可能错过了在 ini 文件中添加身份验证 token 信息。添加:

yourID = passWord

[users]部分下。

希望对你有帮助!

关于java - Apache Shiro 错误凭据异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35085418/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com