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

后端 未结 2 1393
爱一瞬间的悲伤
爱一瞬间的悲伤 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条回答
  •  猫巷女王i
    2021-02-04 18:17

    I often use the Authentication.getDetails() object to store additional info that may not be directly linked to the user per say. So you can store any object you want in that field (a HashMap for instance) and it shares the Authentication object life cycle.

    HashMap info = new HashMap();
    info.put("extraInfo", "info");
    auth.setDetails(info);
    ...
    Map i = (Map)SecurityContextHolder.getContext().getAuthentication.getDetails();
    

提交回复
热议问题