I use Spring Boot. The task is quite simple - in each method I have the same type of operations, checking whether the user has entered, if he has not entered, we throw him in one place, otherwise we continue, in the event of an error, we throw somewhere else. So far I have just concluded this in a separate method, where all the checks are done and the function passed by the parameter is called. But still it looks cumbersome, a bunch of identical method signatures, with a list of identical cookies inside:

public String method( @CookieValue(name = USER_ID_COOKIE_NAME, required = false) Cookie userId, @CookieValue(name = TOKEN_COOKIE_NAME, required = false) Cookie token) { return protectedAction(userId, token, user -> ...); } 

That is, many such methods are obtained. I know there are filters and servlets, but something in my head does not fit how best to do.

Or maybe no general action is needed, maybe there is something more interesting in the spring for these purposes? I'm not the first who wanted to work quite actively with the cookies)

    1 answer 1

    You can use the @ModelAttribute annotation on the method. Then this method will be called before the methods marked @RequestMapping . Here is an example.

     @ModelAttribute public void populateModel(@RequestParam String number, Model model) { model.addAttribute(accountManager.findAccount(number)); } 

    A new attribute will be added to your model.

    Check whether the user is logged in best of all using Spring Security. There you can register the rights to access any part of your application.