Injection of Principal into MVC Controller
I've just moved my spring security configuration (3.1.1) from using the security namespace to manual bean definitions.
I seem to have everything working fine ... however the Principal that was injected into my (Spring MVC) controllers is now null! When using the security namespace configuration the principal is injected fine.
I can get around the issue by accessing the principal via the SecurityContextHolder but it isn't very neat.
Code:
@RequestMapping(value="/home", method = RequestMethod.GET)
public String home(ModelMap model, Principal principal ) {
// principal is null here so the following will throw a NullPointerException !!
String theUsername = principal.getName();
// The following is fine and gets me the username
User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
String username = user.Username();
I'm really not sure where to look to get to the bottom of this one - any ideas?