Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Security request.getUserPrincipal() always null

A really strange situation is observed in our application (Spring Boot 1.5.6 with all-default BOM dependencies): you can perfectly log in (with AbstractPreAuthenticatedProcessingFilter), but this still leaves Principal in request null! I.e. request.getUserPrincipal() is null while SecurityContextHolder.getContext().getAuthentication() is not!

This in turn affects the ability of our health endpoint to be sensitive: it uses Principal (see HealthMvcEndpoint.exposeHealthDetails(HttpServletRequest, Principal)) which is injected by ServletRequestMethodArgumentResolver, which in turn takes it from the request...

Looks like I'm missing something simple, but still can't find it :(

like image 703
FlasH from Ru Avatar asked Sep 08 '17 15:09

FlasH from Ru


1 Answers

So, after creating a new Spring Boot application and debugging it to its guts, I've found out that nobody actually sets Principal into the request. It's Spring who wraps it into another one that uses Spring's SecurityContext for the above (and some other methods). And this wrapping is done by the SecurityContextHolderAwareRequestFilter, which is there by default (see HttpSecurity.servletApi())...

But somebody has disabled the default Spring Security configuration for our project, so the filter was not there!

like image 126
FlasH from Ru Avatar answered Oct 29 '22 05:10

FlasH from Ru