You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

单用户拥双角色,登录需跳转/admin却跳转/user问题

问题分析与解决

你的问题出在SuccessUserHandler的角色判断顺序上:当用户同时拥有ROLE_ADMIN和ROLE_USER两个角色时,代码会先命中roles.contains("ROLE_USER")的条件,直接跳转到/user页面,不会执行后续的ROLE_ADMIN判断逻辑。

只需调整判断顺序,优先检查高权限角色即可解决问题,修改后的SuccessUserHandler代码如下:

@Component
public class SuccessUserHandler implements AuthenticationSuccessHandler {
    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest,
                                        HttpServletResponse httpServletResponse,
                                        Authentication authentication) throws IOException {
        Set<String> roles = AuthorityUtils
                .authorityListToSet(authentication.getAuthorities());
        // 优先判断高权限角色
        if (roles.contains("ROLE_ADMIN")) {
            httpServletResponse.sendRedirect("/admin");
        } else if(roles.contains("ROLE_USER")){
            httpServletResponse.sendRedirect("/user");
        } else {
            httpServletResponse.sendRedirect("/");
        }
    }
}

调整后,同时拥有两个角色的用户会优先匹配ROLE_ADMIN的跳转规则,登录后将正确跳转到/admin页面。RoleHierarchy配置不影响这个问题,因为核心矛盾是登录成功后的角色判断顺序,而非权限继承逻辑。

内容的提问来源于stack exchange,提问作者Sonya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 05:09:59