gpt4 book ai didi

java - SwitchUserFilter 维护早期的管理员用户

转载 作者:行者123 更新时间:2023-12-02 05:30:33 27 4
gpt4 key购买 nike

我尝试将用户从 ADMIN 切换到 USER。但是切换后我发现我丢失了 ADMIN 的主体信息。

如何检查该用户是否被 ADMIN 切换?

最佳答案

来自spring-security 3.2 docs (也适用于 version 4.0.0 ):

On a successful switch, the user's SecurityContext will be updated to reflect the specified user and will also contain an additional SwitchUserGrantedAuthority which contains the original user. Before switching, a check will be made on whether the user is already currently switched, and any current switch will be exited to prevent "nested" switches.

因此,在切换用户后,当您调用时:

request.getUserPrincipal().getAuthorities()

为了检索登录的用户,它将包含当前USER先前ADMIN用户的权限。前一个用户的权限将位于 SwitchUserGrantedAuthority 实例中。

我在代码中所做的是验证当前用户的权限中是否有一些 SwitchUserGrantedAuthority 并向该用户添加一个属性,以便客户端我知道我以管理员身份登录,但有切换用户。我用它来显示一个按钮,用于从该用户注销并返回到管理页面。

关于java - SwitchUserFilter 维护早期的管理员用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25591320/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com