Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring安全性:通过从DB提供rolename重定向到页面_Spring_Hibernate_Spring Security - Fatal编程技术网

Spring安全性:通过从DB提供rolename重定向到页面

Spring安全性:通过从DB提供rolename重定向到页面,spring,hibernate,spring-security,Spring,Hibernate,Spring Security,我有3个用户角色访问权限,角色\管理员,角色\超级\管理员,角色\用户。(角色可能会在将来增加。因此硬编码角色完全不是必需的) 我的security_servlet.xml是这样的 <http auto-config="true" use-expressions="true"> <intercept-url pattern="/admin*" access="hasRole('ROLE_ADMIN')" /> <intercept-url patt

我有3个用户角色访问权限,角色\管理员,角色\超级\管理员,角色\用户。(角色可能会在将来增加。因此硬编码角色完全不是必需的)

我的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>
<form-login login-page="/login"
        authentication-failure-url="/accessdenied"  authentication-success-handler-ref="roleBasedRedirect"/>

登录后,它会重定向到“/”。我想要的是,如果角色是“role\u ADMIN”,那么它应该放在管理员的默认页面上,比如“/ADMIN/profile”,如果角色是“role\u USER”,那么“/USER/profile”


我使用了默认的目标url,但它对我不起作用。

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

@控制器
公共类索引控制器{
@请求映射(“/”)
公共字符串索引(){
身份验证=SecurityContextHolder.getContext().getAuthentication();
Set roles=AuthorityUtils.authorityListToSet(authentication.getAuthories());
if(roles.contains(“ROLE_ADMIN”))
返回“重定向:/admin/profile”;
if(roles.contains(“ROLE\u USER”))
返回“重定向:/user/profile”;
//等等
}
}

如下更新spring security的成功处理程序

 <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>
<form-login login-page="/login"
        authentication-failure-url="/accessdenied"  authentication-success-handler-ref="roleBasedRedirect"/>

编写您自己的
AuthenticationSuccessHandler
,而不是使用默认值。由于我们正在比较角色核心,如“角色\管理员”,如果我将来创建新角色,则需要更改代码,然后再次编译,即此过程将继续。除了这个,还有其他通用方法吗?@Ronnie你可以让它更通用,但是你得到了角色和重定向的想法。没错。我可以从db或任何属性文件获取角色。但我很困惑,若用户有两个角色,那个么我应该在哪里重定向呢?