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.
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.
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.
Spring Interceptor are used to intercept client requests and process them.
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.
You can get the Principal wherever you are with
SecurityContextHolder.getContext().getAuthentication().getPrincipal();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With