Have config (applicationContext-security.xml):
<authentication-manager alias="authenticationManager">
<authentication-provider>
<password-encoder hash="sha"/>
<jdbc-user-service data-source-ref="dataSource"/>
</authentication-provider>
</authentication-manager>
from other side have SQLs from my dataSource
(it's JdbcDaoImpl):
...
public static final String DEF_USERS_BY_USERNAME_QUERY =
"select username,password,enabled " +
"from users " +
"where username = ?";
...
There is now word about sha
in this code,so password selected from standard Spring Security users
table not encoded.
Perhaps, I should provide some sha
attribute for password
column in my hibernate mapping config here:
<class name="model.UserDetails" table="users">
<id name="id">
<generator class="increment"/>
</id>
<property name="username" column="username"/>
<property name="password" column="password"/>
<property name="enabled" column="enabled"/>
<property name="mail" column="mail"/>
<property name="city" column="city"/>
<property name="confirmed" column="confirmed"/>
<property name="confirmationCode" column="confirmation_code"/>
<set name="authorities" cascade="all" inverse="true">
<key column="id" not-null="true"/>
<one-to-many class="model.Authority"/>
</set>
</class>
For now password saved to DB as is,but should be encoded.
How to friend applicationContext
config and DB queries to be the same password encoding?
A little more explanation on the accepted answer. Hope it helps someone.
Hash the password yourself before putting it to database:
Add BCryptPasswordEncoder bean to your security-config.xml
Add passwordEncoder as a property to Authentication Provider class. Autowire it or provide setter and getter methods.
Get the property while you authendicate user for login
And in the authenticating class match both passwords
If you are choosing a hashing system yourself, rather than building an app using an existing database which already contains hashed passwords, then you should make sure your hashing algorithm also uses a salt. Don't just use a plain digest.
A good choice is bcrypt, which we now support directly in Spring Security 3.1 via the
BCryptPasswordEncoder
(implemented using jBCrypt). This automatically generates a salt and concatenates it with the hash value in a single String.Some databases have built-in support for hashing (e.g. Postgres). Otherwise, you need to hash the password yourself before passing it to JDBC:
That's all you need to do to encode the passwords when you create a user.
For authentication, you would use something like:
Using Spring Security 3.1, try this:
What's new:
authentication-provider
points toservice
andservice
points todatasource
.Edit: In Java you will have to encode the password with something like this:
Warn: Be careful! Do not mix SHA with MD5!
If you set the
password-encoder
of theauthentication-provider
as SHA, you need to encode in Java the same way to keep consistent. But if you enconde in Java as MD5, as the sample you found, do not forget to set the hash to "md5". DigestUtils also provides md5 encoder:with 3.1.x this mapping doesnt work for auth. Working way is:
The accepted answer is right. I tested it with spring 3.1 and BCrypt encode algorithm.
When create a user.
When the user login, Remember, use the plain password (not hashed). just like:
Here is security-config:
Hope it will help somebody!
In a simple way can you do something like in applicationContext-security.xml
In Java
Then test it