【问题标题】:Spring Security : Redirect to page by providing rolename from DBSpring Security:通过从 DB 提供角色名重定向到页面
【发布时间】:2015-03-25 00:54:41
【问题描述】:

我有 3 个用户 ROLE ACCESS、ROLE_ADMIN、ROLE_SUPER_ADMIN、ROLE_USER。(角色将来可能会增加。所以硬编码角色根本不是强制性的)

我的 security_servlet.xml 是这样的

 <http auto-config="true" use-expressions="true">
    <intercept-url pattern="/admin*" access="hasRole('ROLE_ADMIN')" />
    <intercept-url pattern="/user*" access="hasRole('ROLE_USER')" />
    <intercept-url pattern="/" access="permitAll" />
    <intercept-url pattern="/candidate*" access="hasRole('ROLE_SUP_ADM')" />

    <form-login login-page="/login"
        authentication-failure-url="/accessdenied" />
    <logout logout-success-url="/logout" />
</http>

<authentication-manager alias="authenticationManager">
    <authentication-provider>
        <!-- <user-service> <user name="rohit" password="rohit" authorities="ROLE_ADMIN" 
            /> <user name="ronnie" password="ronnie" authorities="ROLE_USER" /> </user-service> -->

        <jdbc-user-service data-source-ref="dataSource"
            users-by-username-query="select username, password, active from users where username=?"
            authorities-by-username-query="select us.username, ur.authority from users us, user_roles ur 
    where us.user_id = ur.user_id and us.username =?  " />
    </authentication-provider>
</authentication-manager>

登录后重定向到“/”。我想要的是如果角色是“ROLE_ADMIN”那么它应该登陆管理员默认页面,如“/admin/profile”,如果角色是“ROLE_USER”那么“/user/profile”

我使用了 default-target-url,但它不适合我。

【问题讨论】:

  • 写你自己的AuthenticationSuccessHandler而不是使用默认值。

标签: spring hibernate spring-security


【解决方案1】:

您可以使用 Spring MVC 控制器将用户重定向到所需的页面:

@Controller
public class IndexController {

    @RequestMapping("/")
    public String index() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Set<String> roles = AuthorityUtils.authorityListToSet(authentication.getAuthorities());
        if (roles.contains("ROLE_ADMIN"))
            return "redirect:/admin/profile";
        if (roles.contains("ROLE_USER"))
            return "redirect:/user/profile";
        // and so on
    }

}

【讨论】:

  • 当我们比较像“ROLE_ADMIN”这样的核心角色时,如果我将来创建新角色,那么我需要更改代码然后再次编译它,即过程将继续。除了这个,还有其他可用的通用方法吗?
  • @Ronnie 你可以让它更通用,但你可以通过获取角色并进行重定向来理解。
  • 没错。我可以从 db 或任何属性文件中获取角色。但我很困惑,如果用户有两个角色,那么我应该在哪里重定向?
【解决方案2】:

像这样更新spring security的success handler

<form-login login-page="/login"
        authentication-failure-url="/accessdenied"  authentication-success-handler-ref="roleBasedRedirect"/>

添加 roleBasedRedirect 服务 bean

现在你可以像下面那样做

public class RoleBasedRedirectService extends SavedRequestAwareAuthenticationSuccessHandler{



    @Override
    protected String determineTargetUrl(HttpServletRequest request, HttpServletResponse response) {
            for(GrantedAuthority authority : SecurityContextHolder.getContext().getAuthentication().getAuthorities())
            {
                if(authority.toString().equals("ROLE_ADMIN"))
                    return "/admin/profile/";
                else if(authority.toString().equals("ROLE_USER"))
                    return "/user/profile/";
            }
            return "";
    }

}

【讨论】:

    猜你喜欢
    • 2013-04-26
    • 2013-01-22
    • 1970-01-01
    • 1970-01-01
    • 2018-01-24
    • 2012-09-12
    • 2014-12-01
    • 2018-08-31
    • 2020-04-28
    相关资源
    最近更新 更多