如何使用spring mvc获取Principal in interceptor?

sha*_*tap 4 spring-mvc spring-security

我在Spring MVC 3.1中编写了Web项目,并通过扩展HandlerInterceptorAdapter来实现Interceptor类,我需要找到一种获取Principal的方法.我尝试request.getUserPrincipal()并返回null,我尝试request.getRemoteUser()并返回null.我使用Oauth 2和基本身份验证.谢谢.

Dan*_*ani 8

无论身在何处,您都可以获得校长

SecurityContextHolder.getContext().getAuthentication().getPrincipal();


小智 0

如果用户尚未经过身份验证,则返回 null。确保用户在请求 request.getRemoteUser() 之前已通过身份验证