hibernate - Shiro Authentication failed -
i use shiro 1.2.3 in jsf2+hibernate project. no luck user authenticated. can't figure out i'm doing wrong.
shiro.ini
[main] cachemanager = org.apache.shiro.cache.ehcache.ehcachemanager securitymanager.cachemanager = $cachemanager hashservice = org.apache.shiro.crypto.hash.defaulthashservice hashservice.hashiterations = 100000 hashservice.hashalgorithmname = sha-256 hashservice.generatepublicsalt = true passwordservice = org.apache.shiro.authc.credential.defaultpasswordservice passwordservice.hashservice = $hashservice passwordmatcher = org.apache.shiro.authc.credential.passwordmatcher passwordmatcher.passwordservice = $passwordservice customsecurityrealm = com.sapienzo.common.customsecurityrealm customsecurityrealm.credentialsmatcher = $passwordmatcher securitymanager.realms = $customsecurityrealm
shiroutils class (helper class create salted hash)
public class shiroutils { private static int hash_iterations = 100000; public static string createsaltedhash(string plaintextpassword) { defaulthashservice hashservice = new defaulthashservice(); hashservice.sethashiterations(hash_iterations); hashservice.sethashalgorithmname(sha256hash.algorithm_name); hashservice.setgeneratepublicsalt(true); defaultpasswordservice passwordservice = new defaultpasswordservice(); passwordservice.sethashservice(hashservice); string encryptedpassword = passwordservice.encryptpassword(plaintextpassword); return encryptedpassword; } }
saving user database while registration (getting username , password form fields)
... user.setusername(username); user.setpassword(shiroutils.createsaltedhash(password); userservice.saveuser(user); ...
login (again username , password form fields)
usernamepasswordtoken token = new usernamepasswordtoken(user.getusername(), shiroutils.createsaltedhash(user.getpassword())); subject currentuser = securityutils.getsubject(); currentuser.login(token);
customsecurityrealm.java
public class customsecurityrealm extends authorizingrealm { public customsecurityrealm() { setname("customsecurityrealm"); } @override protected authenticationinfo dogetauthenticationinfo(authenticationtoken authenticationtoken) throws authenticationexception { usernamepasswordtoken token = (usernamepasswordtoken) authenticationtoken; if (token.getusername() == null) { return null; } userservice userservice = new userservice(); string saltedhashpassword = userservice.getpasswordbyusername(token.getusername()); //get encrypted password db if( saltedhashpassword != null ) { simpleauthenticationinfo authn = new simpleauthenticationinfo(token.getusername(), saltedhashpassword, getname()); return authn; } else { return null; } } @override protected authorizationinfo dogetauthorizationinfo(principalcollection principalcollection) { return null; } }
after digging out code line line, noticed passwordsmatch
method used password comparison returns false regardless of inputs.
for example:
string plaintextpassword = "foo"; defaultpasswordservice passwordservice = new defaultpasswordservice(); string encryptedpassword = passwordservice.encryptpassword(plaintextpassword); boolean result = passwordservice.passwordsmatch(plaintextpassword, encryptedpassword); system.out.println(result);
output false. found this post later. causing reported bug. if default locale different english shiro gets confused when (un)capitalizing letters. should set default locale locale.english
fix this.
Comments
Post a Comment