First you must define this file WEB-INF/spring/serurity-context.xml :
<beans:beans xmlns="http://www.springframework.org/schema/security" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.1.xsd"> <http auto-config="true" /> <beans:bean id="myUserService" class="org.my.UserService" /> <authentication-provider user-service-ref="myUserService" /> </beans:beans>
Now you must create the org.my.UserService class and implement the org.springframework.security.core.userdetails.UserDetailsService interface. This interface has one method:
UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, org.springframework.dao.DataAccessException
And in this method, you can use Hibernate to load the user by username. If the user does not exist, just throw a UsernameNotFoundException, otherwise return a new, initialized instance of UserDetails (there you can provide many things, such as user roles, account expiration dates, etc.).
Now comes web.xml :
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name>My Webapp</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/spring/*-context.xml </param-value> </context-param> <filter> <filter-name>springSecurityFilterChain</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>springSecurityFilterChain</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>
If you have any questions or something is wrong, feel free to ask :)
PS: Thus, with UserDetailsService you do not need to check the password, whether the user account is active, etc. You simply provide spring security information for the user with the provided userName , and the user checks the environment. If you, for example, encode your passwords using MD5, you can use password-encoder as follows:
<beans:bean id="myUserService" class="org.my.UserService" /> <authentication-provider user-service-ref="myUserService"> <password-encoder hash="md5"/> </authentication-provider>
Update
Now we UserService deeper into UserService - my (simplified) real-world example.
UserService class:
import org.my_company.my_app.domain.User public class UserService implements UserDetailsService { private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {
Now SecurityUser :
import org.my_company.my_app.domain.User public class SecurityUser extends org.springframework.security.core.userdetails.User { private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public SecurityUser(String username, String password, boolean enabled, boolean accountNonExpired, boolean credentialsNonExpired, boolean accountNonLocked, GrantedAuthority[] authorities) throws IllegalArgumentException { super(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities); } }
And finally, UserDao :
import org.my_company.my_app.domain.User public class UserDao extends HibernateDaoSupport { public User getUser(String username) { List users = getHibernateTemplate().find("from User where username = ?", username); return users == null || users.size() <= 0 ? null : (User) users.get(0); } }
As you can see, I used the HibernateTemplate here.