public class UserDetailsAdapter extends org.springframework.security.core.userdetails.User {
private final Long id;
private static List useTypes = new ArrayList();
public UserDetailsAdapter(ServerAdmin userEntity) {
super(userEntity.getWebLoginId(), userEntity.getWebpassword(), userEntity.isWebLoginEnabled(), true, true, true, toAuthorities(userEntity.getAuthorities()));
this.id = userEntity.getId();
}
private static Collection toAuthorities(List authorities) {
Collection authorityList = new ArrayList();
for (String authority : authorities) {
authorityList.add(new GrantedAuthorityImpl(authority));
}
return authorityList;
}
public Long getId() {
return id;
}
}
Create UserDetailsServiceManager which implements UserDetailsService of Spring Security
@Service("userDetailsService")
public class UserDetailsServiceManager implements UserDetailsService {
private DAOFactory daoFactory;
@Transactional(readOnly = true)
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {
UserDetails userDetails = null;
ServerAdmin userEntity = daoFactory.getServerAdminDAO().findByWebLoginId(username);
if (userEntity == null) {
throw new UsernameNotFoundException("user not found");
}
userDetails = new UserDetailsAdapter((ServerAdmin) userEntity);
return userDetails;
}
/**
* default constructor
*/
public UserDetailsServiceManager() {
}
public UserDetailsServiceManager(DAOFactory daoFactory) {
this.daoFactory = daoFactory;
}
@Autowired
public void setDaoFactory(DAOFactory daoFactory) {
this.daoFactory = daoFactory;
}
}
create the applicationContext-Security.xml. add the following code:
<?xml version="1.0" encoding="UTF-8"?>
<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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.0.3.xsd">
<!-- HTTP security configurations -->
<http auto-config="true" use-expressions="true">
<form-login login-processing-url="/resources/j_spring_security_check" login-page="/login"
authentication-failure-url="/login?login_error=t"/>
<logout logout-url="/logout"/>
<remember-me/>
<!-- Configure these elements to secure URIs in your application -->
<intercept-url pattern="/coupon/**" access="hasRole('ROLE_SERVERADMIN')"/>
<intercept-url pattern="/resources/**" access="permitAll"/>
<intercept-url pattern="/**" access="permitAll"/>
<!--
Uncomment to enable X509 client authentication support
<x509 />
-->
<!-- Uncomment to limit the number of sessions a user can have -->
<!--
<session-management invalid-session-url="/timeout.jsp">
<concurrency-control max-sessions="1" error-if-maximum-exceeded="true"/>
</session-management>
-->
</http>
<!-- Configure Authentication mechanism -->
<authentication-manager alias="authenticationManager">
<authentication-provider user-service-ref="userDetailsService">
<password-encoder ref="passwordEncoder">
<salt-source ref="saltSource"/>
</password-encoder>
</authentication-provider>
</authentication-manager>
<!-- For hashing and salting user passwords -->
<beans:bean id="passwordEncoder" class="org.springframework.security.authentication.encoding.ShaPasswordEncoder"/>
<beans:bean id="saltSource" class="org.springframework.security.authentication.dao.ReflectionSaltSource"
p:userPropertyToUse="id"/>
</beans:beans>