I'm using Shiro with Spring MVC to login users. I configure Shiro in applicationContext.xml (no INI file).
This is the realm configuration:
<bean id="myRealm" class="org.apache.shiro.realm.jdbc.JdbcRealm">
<property name="dataSource" ref="dataSource"/>
<property name="authenticationQuery" value="select password from usuarios where email = ?"/>
<property name="credentialsMatcher">
<bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
<property name="storedCredentialsHexEncoded" value="false"/>
<property name="hashIterations" value="1024" />
</bean>
</property>
</bean>
This is my code generating salt and hash when the user registers:
RandomNumberGenerator rng = new SecureRandomNumberGenerator();
Object salt = rng.nextBytes();
String hashedPasswordBase64 = new Sha256Hash(password, salt, 1024).toBase64();
u.setPassword(hashedPasswordBase64);
u.setSalt(salt.toString());
usuarioDao.saveUsuario(u);
Here saveUsuario(u) calls the DAO to persist the user in MySQL. I guess that the salt.toString() is wrong.
The user table is:
CREATE TABLE usuarios (
id INTEGER AUTO_INCREMENT,
nombre VARCHAR(50),
...
password VARCHAR(50),
salt VARCHAR(50),
...
PRIMARY KEY (id)
);
Questions are: - Which type should be the hash field in the DB? The hash is created by rng.nextBytes and is of type Object. - How do I declare the field or the query to HashedCredentialsMatcher so it can authenticate properly?