S
S
Sergey2017-01-08 13:21:18
Java
Sergey, 2017-01-08 13:21:18

How to correctly implement token authentication with remember me on Spring security?

I want to make an authorization that would be saved when the browser is restarted, now it turned out to make the authorization only until the end of the session. I tried to add a setting for remeber-me to the configs, but only a table was created in the database and nothing is written there and the cookie does not come back. As a client I use Angular 2 on a separate server.
Maybe someone has seen a normal example of implementing such an authorization or has come across this himself and can give advice.
UPDATE 1
Now the cookie is sent to the client and the token is also stored in the database. I did it as follows:
1) We inherit from PersistentTokenBasedRememberMeServicesoverride the method onLoginSuccessand make it public.
2) create a class bean from step 1
3) In the controller, to the parameters of the method that is mapped to the URL of the login page, add

HttpServletRequest request, HttpServletResponse response
, For example
@RequestMapping(value = "/api/login", method = RequestMethod.POST, produces = {APPLICATION_JSON_VALUE, APPLICATION_XML_VALUE})
    public void postLogin(@RequestBody EntityUser body, HttpServletRequest request, HttpServletResponse response) {
UserDetails userDetails = detailsService.loadUserByUsername(username);

        UsernamePasswordAuthenticationToken token =
            new UsernamePasswordAuthenticationToken(username,user.getPassword(),userDetails.getAuthorities());

        myTokenRememberMeService.onLoginSuccess(req,res,token);

        if (token.isAuthenticated())
        SecurityContextHolder.getContext().setAuthentication(token);
}

4) create a UsernamePasswordAuthenticationToken token, fill it in and call the onLoginSuccess method passing the created token as one of the parameters, check that authentication has passed and add the token to the security context.
PS I have such a feeling that this is a hard crutch and you can make everything much easier, but for now it works fine))

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
aol-nnov, 2017-01-08
@Bleser

You won't believe it, the first link I came across on Google

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question