Question

I'm developing an app based on Grails and Vaadin 7. I managed to make them work with SpringSecurity for authentication and authorization, but I had to develop my own Service that calls the Spring Security authentication manager to make it work with Vaadin:

class SecurityService {

    static transactional = true

    def springSecurityService
    def authenticationManager

    void signIn(String username, String password) {
        try {
            def authentication = new UsernamePasswordAuthenticationToken(username, password)
            SCH.context.authentication = authenticationManager.authenticate(authentication)
        } catch (BadCredentialsException e) {
            throw new SecurityException("Invalid username/password")
        }
    }
}

The problem is that now I need to implement a remember me authentication and I don't know from where to start.

How do I make the authenticationManager know that I want it to use remeberMeAuthentication? I can get a boolean value from a checkbox on the login View, but what do I do with it next?

Was it helpful?

Solution

Since your question is specific to the handling of checkbox value (remember me flag) coming from login page, the answer is that you have to call loginSuccess or loginFail method of RememberMeServices. The loginSuccess adds auto-login cookie in the response and loginFail removes that cookie.

But I guess above answer won't help you much unless you are sure that you have RememberMeServices configured in your app. Maybe following steps that configure RememberMeServices will help you do whole thing your way (or help you understand the out of the box functionality):

(1) Create a class (call it myRememberMeServices) that implements RememberMeServices and LogoutHandler.

(2) In autoLogin method, create an authentication object (UsernamePasswordAuthenticationToken) after parsing the cookie value.

(3) In loginFail method, cancel the cookie.

(4) In loginSuccess method, create an auto-login cookie. Add value that you would use in autoLogin method. Usually cookie value is encrypted.

(5) In logout method , cancel the cookie.

(6) Inject myRememberMeServices in following four places and call appropriate method:

(a) At the time of successful login (if checkbox value is set),     
(b) At the time of failed login,   
(c) On logout, and   
(d) In filter that does autologin 

It is worth noting that RememberMeAuthenticationFilter takes authenticationManager and RememberMeServices in its constructor.

Answer to your other question is that the authenticationManager doesn't need to know anything about remember me. It is the filter (or any class handling auto login) that needs to know about authenticationManager and RememberMeServices. (In other words, ask RememberMeServices for a token and pass it to authenticationManager to do auto login).

OTHER TIPS

Spring Security's architecture is based on servlet filters. The sign-in mechanism you have implemented above is normally done by the UsernamePasswordAuthenticationFilter. Another filter called RememberMeAuthenticationFilter takes the responsibility for the remember-me functionality. The authenticationManager is not aware at all whether the remember-me feature is used by the application or not.

If you want to integrate Spring Security with another web-framework, first try to find out how the filters of the two frameworks can play together.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top