How to reload Spring Security Principal after updating in Hibernate?

后端 未结 2 546
借酒劲吻你
借酒劲吻你 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".

    0 讨论(0)
  • 2020-12-08 19:54

    i had the same problem

    i fixed it with getting the authentication from SecurityContextHolder cast it's principal to User then update principal.

    everything is working but i don't know this is a good practice or not.

    0 讨论(0)
提交回复
热议问题