I have been trying to setup Spring Security with using UserDetailsService. As an example, I used baeldung's tutorial. The application has launched w/o any exception but authentification doesn't work. Currently, I have a core spring java config and spring java config for each app module.
Core Spring Java conf:
public class AppInitializer implements WebApplicationInitializer {
@Override
@Autowired
public void onStartup(ServletContext container) throws ServletException {
AnnotationConfigWebApplicationContext context = getContext();
container.addListener(new ContextLoaderListener(context));
container.setInitParameter("spring.profiles.active", "dev"); //Workaround for NamingException
container.setInitParameter("spring.profiles.default", "dev"); //Workaround for NamingException
container.setInitParameter("spring.liveBeansView.mbeanDomain", "dev"); //Workaround for NamingException
ServletRegistration.Dynamic mainDispatcher =
container.addServlet("dispatcher", new DispatcherServlet(context));
ServletRegistration.Dynamic businessDispatcher =
container.addServlet("businessDispatcher", BusinessAppConfig.createDispatcherServlet(context));
ServletRegistration.Dynamic ppaDispatcher =
container.addServlet("ppaDispatcher", PpaAppConfig.createDispatcherServlet(context));
initDispatcher(mainDispatcher, 1, "/");
initDispatcher(businessDispatcher, 2, "/business");
initDispatcher(businessDispatcher, 3, "/ppa");
}
private void initDispatcher(ServletRegistration.Dynamic dispatcher, int loadOnStartUp, String mapping) {
if (dispatcher == null) {
System.out.println("Servlet" + dispatcher.getName() + " is already added");
} else {
dispatcher.setLoadOnStartup(loadOnStartUp);
dispatcher.addMapping(mapping);
}
}
public AnnotationConfigWebApplicationContext getContext() {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(MvcConfiguration.class);
return context;
}
@Bean(name = "propertyConfigurer")
public PropertySourcesPlaceholderConfigurer getPropertyPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer placeholderConfigurer = new PropertySourcesPlaceholderConfigurer();
placeholderConfigurer.setLocation(new ClassPathResource("common.properties"));
placeholderConfigurer.setLocation(new ClassPathResource("amazon.S3Storage.properties"));
placeholderConfigurer.setLocation(new ClassPathResource("local.storage.properties"));
placeholderConfigurer.setLocation(new ClassPathResource("log4j.properties"));
placeholderConfigurer.setIgnoreUnresolvablePlaceholders(true);
return placeholderConfigurer;
}
}
Spring java conf for a business module
@Configuration
public class BusinessAppConfig {
public static Servlet createDispatcherServlet(AnnotationConfigWebApplicationContext context) {
context.register(BusinessMvcConfig.class);
context.register(BusinessHibernateConfig.class);
context.register(BusinessSecurityConfig.class);
return new DispatcherServlet(context);
}
}
Spring Security java conf for a business module
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class BusinessSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(authenticationProvider());
}
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Autowired
public DaoAuthenticationProvider authenticationProvider() {
DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
authProvider.setUserDetailsService(userDetailsService);
authProvider.setPasswordEncoder(getPasswordEncoder());
return authProvider;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().authenticated().
and().formLogin().permitAll().
and().logout().permitAll();
}
@Bean(name = "passwordEncoder")
public PasswordEncoder getPasswordEncoder(){
return new BCryptPasswordEncoder(11);
}
}
UserDetailsService implementation
@Service
public class UserDetailsServiceImpl extends BaseServiceImpl<User, UserRepository<User>> implements UserDetailsService, UserService {
@Override
public UserDetails loadUserByUsername(String login) throws UsernameNotFoundException {
User user = dao.findUserByLogin(login);
if (user == null) {
throw new UsernameNotFoundException(login);
}
return new UserPrincipal(user);
}
}
UserDetails based on User model
public class UserPrincipal implements UserDetails, Serializable {
private User user;
public UserPrincipal(User user) {
this.user = user;
}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return null;
}
@Override
public String getPassword() {
return user.getPassword();
}
@Override
public String getUsername() {
return user.getName();
}
@Override
public boolean isAccountNonExpired() {
return user.isAccountNonExpired();
}
@Override
public boolean isAccountNonLocked() {
return user.isAccountNonLocked();
}
@Override
public boolean isCredentialsNonExpired() {
return user.isCredentialsNonExpired();
}
@Override
public boolean isEnabled() {
return user.isEnabled();
}
}
In debug mode, I've encountered couple exceptions
2018-05-16 22:23:51 DEBUG InjectionMetadata:74 - Registered injected element on class [business.config.BusinessSecurityConfig$$EnhancerBySpringCGLIB$$c0bd9f7f]: AutowiredMethodElement for public org.springframework.security.authentication.dao.DaoAuthenticationProvider business.config.BusinessSecurityConfig.authenticationProvider()
2018-05-16 22:23:51 DEBUG InjectionMetadata:74 - Registered injected element on class [business.config.BusinessSecurityConfig$$EnhancerBySpringCGLIB$$c0bd9f7f]: AutowiredMethodElement for public void business.config.BusinessSecurityConfig.configureGlobal(org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder) throws java.lang.Exception
2018-05-16 22:23:51 DEBUG DefaultListableBeanFactory:569 - Eagerly caching bean 'businessSecurityConfig' to allow for resolving potential circular references
AND
[org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration$$EnhancerBySpringCGLIB$$3d61bda9]: AutowiredMethodElement for public void org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration.setGlobalAuthenticationConfigurers(java.util.List) throws java.lang.Exception
AND
2018-05-16 22:24:11 DEBUG AnnotationUtils:1889 - Failed to meta-introspect annotation interface org.springframework.beans.factory.annotation.Autowired: java.lang.NullPointerException
So, what is wrong in this configuration?