I'm trying to do my own CustomUserDetailsService and looking up the database via my DAO. Unfortunately, I can not do @Autowired within this Service.
Here is my code.
The security.xml file:PHP Code:@Service
@Transactional(readOnly = true)
public class CustomUserDetailsService implements UserDetailsService {
@Autowired
private PersonDAO personDAO;
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
UserDetails user = null;
Person person = null;
try {
// It fails here!!!!
person = personDAO.getPerson( email );
user = new SecurityUser( person, person.getFirstName()+" "+person.getLastName(),
person.getPassword().toLowerCase(), getGrantedAuthorities( person.getRoleList() ) );
return user;
} catch (Exception e) {
System.out.println( "Exception message: "+e.getMessage() );
System.out.println( "Exception cause: "+e.getCause() );
throw new RuntimeException(e);
}
}
public static List<GrantedAuthority> getGrantedAuthorities(List<Role> roles) {
List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
for( Role role : roles ) {
authorities.add( new SimpleGrantedAuthority( role.getRoleName() ));
}
return authorities;
}
}
The Problem is, that all my services in my Controller classes are working fine and I'm connecting to the DB, but not if I'm trying to get a user from my CustomUserDetailsService.java. This is really weird....PHP 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"
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.1.xsd">
<debug />
<global-method-security pre-post-annotations="enabled" />
<http use-expressions="true">
<intercept-url pattern="/account/**" access="hasRole('ROLE_USER')" />
<form-login login-page="/auth/login"
authentication-failure-url="/auth/denied"
default-target-url="/" />
<access-denied-handler error-page="/auth/denied" />
<logout invalidate-session="true"
logout-success-url="/auth/logout"
logout-url="/logout"
delete-cookies="JSESSIONID" />
<remember-me />
<session-management invalid-session-url="/">
<concurrency-control max-sessions="1" error-if-maximum-exceeded="true" />
</session-management>
</http>
<beans:bean id="passwordEncoder" class="org.springframework.security.authentication.encoding.Md5PasswordEncoder" />
<authentication-manager>
<authentication-provider user-service-ref="customUserDetailsService">
<password-encoder ref="passwordEncoder"/>
</authentication-provider>
</authentication-manager>
</beans:beans>It seems like my PersonDAO is not initialized at all.
If the function loadUserByUsername(email) is called I'm catching the Exception with the output: null.
I would appreciate any hints or suggestions!
Thanks in advance.


It seems like my PersonDAO is not initialized at all.
Reply With Quote
.
