Answer the question
In order to leave comments, you need to log in
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 PersistentTokenBasedRememberMeServices
override the method onLoginSuccess
and 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);
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question