2016-01-29 103 views
1

這是我第一次使用apache shiro,所以請耐心等待。Apache Shiro IncorrectCredentialsException

我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:提交憑據令牌[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"); 
     } 

回答

0

經過這麼多時間試圖通過stackoverflow問題尋找答案沒有任何成功,我終於決定通過我的INI(shiro.ini),發現一個小錯誤。我最初將hashService.generatePublicSalt設置爲true,並在同一時間配置私有鹽,這似乎是一個錯誤。

當我刪除私人鹽,一切都像一個魅力。

0

我也是shiro的新手。我相信你可能錯過了在ini文件中添加Auth令牌信息。 地址:

yourID = passWord 

[users]部分。

希望它有幫助!

+0

什麼是認證令牌信息?我需要添加哪個ID和密碼?在此先感謝 –

+0

是的,您的用戶名和密碼。 – Helios

+0

用戶已經在包括我的用戶表中可用了!你在說什麼是隨機ID /密碼還是我必須找到它的地方?再次抱歉太多的問題。 –