Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can i get the Principal in interceptor using spring mvc?

I write Web project in spring MVC 3.1 and i implement Interceptor class by extends HandlerInterceptorAdapter and i need to find a way get the Principal. I try request.getUserPrincipal() and it return null, I try request.getRemoteUser() and it return null. I using Oauth 2 and basic Authentication. Thanks.

like image 648
shai.tap Avatar asked Feb 23 '12 08:02

shai.tap


People also ask

How do you use interceptor in spring?

To work with interceptor, you need to create @Component class that supports it and it should implement the HandlerInterceptor interface. preHandle() method − This is used to perform operations before sending the request to the controller. This method should return true to return the response to the client.

What is Spring MVC interceptor?

Simply put, a Spring interceptor is a class that either extends the HandlerInterceptorAdapter class or implements the HandlerInterceptor interface. The HandlerInterceptor contains three main methods: prehandle() – called before the execution of the actual handler. postHandle() – called after the handler is executed.

Which interceptor is used in an application when maintenance page needs to be shown?

Spring Interceptor are used to intercept client requests and process them.

What is principal Spring Security?

The principal is the currently logged in user. However, you retrieve it through the security context which is bound to the current thread and as such it's also bound to the current request and its session.


1 Answers

You can get the Principal wherever you are with

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

like image 101
Dani Avatar answered Oct 19 '22 18:10

Dani