Hi,
I'm implementing my own UserDetailsService to validate login process against the database users. I'm using spring 3.0.7., with Spring security 3.0.7. I have a entity SystemUser, from which I create UserDetail entity needed in Spring Security.
PHP Code:package com.objectverse.system.login.service;
import java.util.ArrayList;
import java.util.Collection;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.objectverse.system.login.repository.UserDetailsDao;
import com.objectverse.system.users.user.model.SystemUser;
@Service("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {
private static Log logger = LogFactory.getLog(UserDetailsServiceImpl.class);
@Autowired
private UserDetailsDao userDetailsDao;
@Override
@Transactional
public UserDetails loadUserByUsername(String username)
throws UsernameNotFoundException, DataAccessException {
if (logger.isDebugEnabled()) {
logger.debug("UserDetailsServiceImpl::getSystemUSer");
}
SystemUser systemUser = this.userDetailsDao.findByUsername(username);
if (systemUser != null) {
//RequestContextHolder.currentRequestAttributes().setAttribute("profile", systemUser, RequestAttributes.SCOPE_SESSION);
// In this point I'd like to store the systemUser in httpSession or any object available throughout the application (session scope).
boolean accountNonExpired = systemUser.getActive();
boolean credentialsNonExpired = systemUser.getActive();
boolean accountNonLocked = systemUser.getActive();
Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
authorities.add(new GrantedAuthorityImpl("ROLE_USER"));
User user = new User(
systemUser.getUsername(), systemUser.getPassword(), systemUser.getActive(),
accountNonExpired, credentialsNonExpired, accountNonLocked, authorities);
return user;
}
throw new RuntimeException("Access denied.");
}
}
My config
I have 3 questions regarding my configuration:PHP Code:<security:http auto-config="true" use-expressions="true">
<security:intercept-url pattern="/app/login*" access="permitAll"/>
<security:intercept-url pattern="/app/signin" access="permitAll"/>
<security:intercept-url pattern="/app/**" access="isAuthenticated()"/>
<security:intercept-url pattern="/**" access="permitAll" />
<security:form-login login-page="/app/login.jsp" authentication-failure-url="/app/login.jsp?error=failed" default-target-url="/app/desktop"/>
<security:logout logout-url="/app/logoff" logout-success-url="/app/login"/>
<security:session-management>
<security:concurrency-control max-sessions="1" error-if-maximum-exceeded="true" />
</security:session-management>
</security:http>
<bean id="daoAuthenticationProvider" class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
<property name="userDetailsService" ref="userDetailsService"/>
</bean>
<bean id="authenticationManager" class="org.springframework.security.authentication.ProviderManager">
<property name="providers">
<list>
<ref local="daoAuthenticationProvider" />
</list>
</property>
</bean>
<security:authentication-manager>
<security:authentication-provider user-service-ref="userDetailsService">
<security:password-encoder hash="plaintext"/>
</security:authentication-provider>
</security:authentication-manager>
1. As commented in the code I'd like to store my systemUser in httpSession or any object available throughout the application (session scope). How can I do that as the method is in the service layer and the bean is initialized when the application starts.
2. How can I handle Exceptions? If an exception occurs I get a page with stacktrace?
3. I'd like to add some logic where the system checks if there are any users in the database - the user is redirected to desktop and if there is no user in the database, the user is redirected to install.
Any help appreciated.
Bojan


Reply With Quote

