How to redirect request when @Preauthorize returns false

Asked

Viewed 67 times

1

I’m learning about the Spring MVC and Spring Security.

How do I redirect the page when the following line returns false within a @Controller ?

@PostAuthorize(" hasRole('page')")

If the line above returns true no problem and the page is displayed correctly.

If the line above returns false page is displayed! But all variables are beech, Ex: displays a table only with the header and no row.

Question: how to do instead of displaying the empty page, is to redirect to another page?

1 answer

0

Think if it is possible to do something like this in your controller:

    @RequestMapping(value = {"/login", "/"})
    public String login(@AuthenticationPrincipal User user){
        //se usuario for true (logado), manda pra home
        if(user != null){
            return "redirect:/home";
        }
       //se usuario for vazio (falso), retorna pro login
        return "Login";
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.