Encountering an issue while developing an app on Angular.js with Spring Security. Unable to send the username and password from UI to spring security, resulting in a null pointer exception. Upon debugging, it was discovered that the username is null. Despite extensive searches on Google, the problem persists. Clicking on the login button directs the debugger to userdetailserviceimpl.java where the username is found to be null when making an AJAX call from the UI.
Here's a snippet of code for SecurityConfig.java:
@Autowired
public void configAuthBuilder(AuthenticationManagerBuilder builder) throws Exception {
builder.userDetailsService(userDetailServiceImpl);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/app/**").permitAll()
.antMatchers("/login.html").permitAll()
.anyRequest().authenticated()
.and()
.exceptionHandling()
.authenticationEntryPoint(unauthorisedHandler)
.accessDeniedHandler(accessDenied)
.formLogin()
.loginProcessingUrl("/authenticate")
.successHandler(authSuccess)
.usernameParameter("username")
.passwordParameter("password")
.permitAll()
.logout().logoutSuccessHandler(logoutSuccess).permitAll()
.csrf().disable();
}
UserDetailServiceImpl.java:
@Service
public class UserDetailServiceImpl implements UserDetailsService {
@Autowired
private LoginDao loginDao;
public UserDetails loadUserByUsername(String username)
throws UsernameNotFoundException {
UserInfo userInfo = loginDao.loadUserByUsername(username);
System.out.println(userInfo.getUserName() + "" + userInfo.getRole());
GrantedAuthority authority = new SimpleGrantedAuthority(userInfo.getRole());
UserDetails userDetails = (UserDetails)new User(userInfo.getUserName(), userInfo.getPassword(), Arrays.asList(authority));
return userDetails;
}
}
Further details about the login process can be found in login.controller.js, login.service.js, and common ajax service sections.
Astraizen