Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add Precall method in Spring MVC Controller

I have one requirement to fulfill.

Actually when ever user Login into my application , i will save the logged in user name in session attribute like .

session.setAttribute("LOGIN_USER", userName);

So that, if user tries to access my method links directly with out coming from Login page i can check in controller method whether this session has the specific attributive value as below and i can restrict user to access that page and redirect him to login page.

if(request.getSession(false).getAttribute("LOGIN_USER")==null)
 //redirect to login page

Now,my requirement is if i have 50 methods in controller i can't add this condition in each method.I think there is a way we can add some sort of filters or any method in controller class like ex @preExecuteMethod to first execute this method and proceed for the actual call if success.

like image 466
Ran Avatar asked Dec 05 '25 13:12

Ran


1 Answers

Yes, you can use a HandlerInterceptor for this. See the docs here and here.

like image 171
skaffman Avatar answered Dec 07 '25 14:12

skaffman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!