How to reload Spring Security Principal after updating in Hibernate?

后端 未结 2 549
借酒劲吻你
借酒劲吻你 2020-12-08 19:26

This must be a common issue... and I feel that after googling, and SOing I must have just not looked around thoroughly for the answer enough or that no1 has asked it... so p

2条回答
  •  长情又很酷
    2020-12-08 19:45

    OK dug around and finally found the answer.

    We can create a UsernamePasswordAuthenticationToken and assign the updated Principal to the context.

    Authentication authentication = new UsernamePasswordAuthenticationToken(userObject, userObject.getPassword(), userObject.getAuthorities());
    SecurityContextHolder.getContext().setAuthentication(authentication);
    

    See also "How to manually set an authenticated user in Spring Security / SpringMVC".

提交回复
热议问题