This is the second time I use apache shiro in a project, but for the first time am salting password. At this time, I am using apache shiro 1.2.0 . I use siro in a web application using jsp, spring, JPA (spring -data-jpa) and using SHA256 for encryption, then base64 before saving to the database. I have SaltedJPARealm , a Sha256CredentialMatcher that implements HashedCredentialMatcher. this is how i do
create user in my controller
RandomNumberGenerator rng = new SecureRandomNumberGenerator(); ByteSource salt = rng.nextBytes(10); String hashedPasswordBase64 = new Sha256Hash(signupForm.getPassword(),salt).toBase64(); userService.createUser(signupForm.getFullName(), signupForm.getEmail(), hashedPasswordBase64, salt.toBase64());
so my password is password1234 and the generated salt is /ZFfGOcSxYhy+g== , so in my database I have a password: whb+0AihIGJ4n8QwULj1tR6qSwCrA+1BUvnoe4q4Cy4= salt in the salt field in the database same.
In my configuration in spring there is:
<bean id="saltedJPARealm" class="bla.bla.webapp.security.SaltedJPARealm"> <constructor-arg ref="credMatcher"/> </bean> <bean id="credMatcher" class="bla.bla.webapp.security.Sha256CredentialMatcher"> <property name="storedCredentialsHexEncoded" value="false" /> <property name="hashAlgorithmName" value="SHA-256" /> </bean> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager" depends-on="userRepository"> <property name="realm" ref="saltedJPARealm" /> </bean>
login user
Subject currentUser = SecurityUtils.getSubject(); if (!currentUser.isAuthenticated()) { UsernamePasswordToken token = new UsernamePasswordToken(loginForm.getEmail(), loginForm.getPassword(), loginForm.isRememberMe()); SecurityUtils.getSubject().login(token); }
SaltedJPARealm doGetAuthenticationInfo(AuthenticationToken at) returns SaltedAuthenticationInfo after receiving the user from the database:
ByteSource salt = ByteSource.Util.bytes(user.getSalt()); return new SimpleAuthenticationInfo(user, user.getPassword().toCharArray(),salt,this.getName());
doCredentialsMatch Sha256CredentialMatcher looks like this:
Object tokenfromSubmition = hashProvidedCredentials(token.getCredentials(),((SaltedAuthenticationInfo)info).getCredentialsSalt(),0); Object passwordFromStorage =this.getCredentials(info); Boolean match = equals(tokenfromSubmition, passwordFromStorage); return match;
full code is available here on pastie , authentication fails with this. but when I change the code so as not to salt the password (when creating the account) and return AuthenticationInfo, not SaltedAuthenticationInfo. he works with the same class. I wonder what exactly am I doing wrong?