How to store custom information in SecurityContext of spring-security?

后端 未结 2 1386
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-04 17:34

In my application I\'m using LDAP authentication. But i\'m also have 2 remote services which requires authentication via method login(username, password). The method returns sec

2条回答
  •  生来不讨喜
    2021-02-04 18:21

    Your implementation of 'UserDetails' may hold any additional data. This is what gets stored in the SecurityContext which is later accessible after successful login.

    You can later access it as (Assumes MyUserDetails implements UserDetails)

    Object principal = SecurityContextHolder.getContext().getAuthentication();
    if (principal instanceof MyUserDetails) {
      MyUserDetails mud = (MyUserDetails) principal;
      mud.getMyData(); //Extract your additional data here
    }
    

提交回复
热议问题