Maddy
Maddy

Reputation: 2224

Spring Security Remember Me

I'm developing a Spring MVC web app and have configured the Spring Security to intercept all URLs and authenticate them. If the user ticks the "remember me", to automatically log in the user without redirecting to the login page.

Say, my login URL is

example.com/signin

and homepage is

example.com/home

When a user enters any URL to a valid web page (e.g. example.com/home) which needs authentication, if the remember me is enabled, he is directed to that specific web page without authenticating again.

But when a user enters example.com/signin, it just displays login form even if he is already authenticated and remembered.

How can I redirect him to the homepage(example.com/home) if he is an authenticated and remmeber-me enabled user without displaying the login page?

Upvotes: 0

Views: 726

Answers (1)

Maddy
Maddy

Reputation: 2224

I added a redirect to the controller. Please let me know if this is the correct way.

Authentication auth = SecurityContextHolder.getContext().getAuthentication();
            //if the user is already authenticated redirect him to the dashboard
            if(!(auth instanceof  AnonymousAuthenticationToken)){
                return "redirect:dashboard";
            }
            return "signin";

Upvotes: 0

Related Questions