Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.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 security 使用Spring 3.0安全性,我可以使用一个登录页面重定向不同的页面吗。。?_Spring Security - Fatal编程技术网

Spring security 使用Spring 3.0安全性,我可以使用一个登录页面重定向不同的页面吗。。?

Spring security 使用Spring 3.0安全性,我可以使用一个登录页面重定向不同的页面吗。。?,spring-security,Spring Security,可能重复: 我正在用Spring用Java做我的项目。我在我的项目中使用spring安全性 我的问题是,根据role_USER或role_ADMIN的角色,我希望将它们重定向到不同的页面。 这意味着,如果管理员登录,那么他应该重定向到一个页面,如果正常用户登录,则重定向到不同的页面,但两个用户的登录页面是相同的 现在,我将以下代码用于spring-servlet.xml文件。所以请给我一些解决方案 <security:http auto-config="true"> <

可能重复:

我正在用Spring用Java做我的项目。我在我的项目中使用spring安全性

我的问题是,根据role_USER或role_ADMIN的角色,我希望将它们重定向到不同的页面。 这意味着,如果管理员登录,那么他应该重定向到一个页面,如果正常用户登录,则重定向到不同的页面,但两个用户的登录页面是相同的

现在,我将以下代码用于spring-servlet.xml文件。所以请给我一些解决方案

<security:http auto-config="true">
    <security:intercept-url pattern="/airline/*" access="ROLE_USER" />
    <security:form-login login-page="/login" default-target-url="/logout"
        authentication-failure-url="/login" />
    <security:logout logout-success-url="/logout" />
</security:http>

<security:authentication-manager>
   <security:authentication-provider>
    <security:jdbc-user-service data-source-ref="dataSrc"
       users-by-username-query="select username,password,enabled from spring_users where username=?" 
       authorities-by-username-query="select u.username, ur.authority from spring_users u, spring_roles ur where u.user_id=ur.user_id and u.username=?"/>
   </security:authentication-provider>
</security:authentication-manager>

如果要在成功验证后控制导航流,可以通过添加自己的AuthenticationSuccessHandler来实现

向引用customAuthenticationHandler bean的
元素添加以下属性

<form-login login-page="/login.xhtml" authentication-success-handler-ref="customAuthenticationHandler"/>
...
</http>
<beans:bean id="customAuthenticationHandler" class="com.examples.CustomAuthenticationHandler" />

...
CustomAuthenticationHandler类如下所示:

public class CustomAuthenticationHandler extends SimpleUrlAuthenticationSuccessHandler{

@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
        Authentication authentication) throws ServletException, IOException {
        String userTargetUrl = "/welcome.xhtml";
        String adminTargetUrl = "/admin/welcome.xhtml";
        Set<String> roles = AuthorityUtils.authorityListToSet(authentication.getAuthorities());
        if (roles.contains("ROLE_ADMIN")) {
            getRedirectStrategy().sendRedirect(request, response, adminTargetUrl);
        }
        else if(roles.contains("ROLE_USER")) {
            getRedirectStrategy().sendRedirect(request, response, userTargetUrl);
        }
        else {
            super.onAuthenticationSuccess(request, response, authentication);
            return;
        }
 }

}
公共类CustomAuthenticationHandler扩展了SimpleRuthenticationSuccessHandler{
@凌驾
AuthenticationSuccess(HttpServletRequest请求、HttpServletResponse响应、,
身份验证)抛出ServletException、IOException{
字符串userTargetUrl=“/welcome.xhtml”;
字符串adminTargetUrl=“/admin/welcome.xhtml”;
Set roles=AuthorityUtils.authorityListToSet(authentication.getAuthories());
if(roles.contains(“ROLE_ADMIN”)){
getRedirectStrategy().sendRedirect(请求、响应、adminTargetUrl);
}
else if(roles.contains(“ROLE\u USER”)){
getRedirectStrategy().sendRedirect(请求、响应、userTargetUrl);
}
否则{
super.onAuthenticationSuccess(请求、响应、身份验证);
返回;
}
}
}

如果要在成功验证后控制导航流,可以通过添加自己的AuthenticationSuccessHandler来实现

向引用customAuthenticationHandler bean的
元素添加以下属性

<form-login login-page="/login.xhtml" authentication-success-handler-ref="customAuthenticationHandler"/>
...
</http>
<beans:bean id="customAuthenticationHandler" class="com.examples.CustomAuthenticationHandler" />

...
CustomAuthenticationHandler类如下所示:

public class CustomAuthenticationHandler extends SimpleUrlAuthenticationSuccessHandler{

@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
        Authentication authentication) throws ServletException, IOException {
        String userTargetUrl = "/welcome.xhtml";
        String adminTargetUrl = "/admin/welcome.xhtml";
        Set<String> roles = AuthorityUtils.authorityListToSet(authentication.getAuthorities());
        if (roles.contains("ROLE_ADMIN")) {
            getRedirectStrategy().sendRedirect(request, response, adminTargetUrl);
        }
        else if(roles.contains("ROLE_USER")) {
            getRedirectStrategy().sendRedirect(request, response, userTargetUrl);
        }
        else {
            super.onAuthenticationSuccess(request, response, authentication);
            return;
        }
 }

}
公共类CustomAuthenticationHandler扩展了SimpleRuthenticationSuccessHandler{
@凌驾
AuthenticationSuccess(HttpServletRequest请求、HttpServletResponse响应、,
身份验证)抛出ServletException、IOException{
字符串userTargetUrl=“/welcome.xhtml”;
字符串adminTargetUrl=“/admin/welcome.xhtml”;
Set roles=AuthorityUtils.authorityListToSet(authentication.getAuthories());
if(roles.contains(“ROLE_ADMIN”)){
getRedirectStrategy().sendRedirect(请求、响应、adminTargetUrl);
}
else if(roles.contains(“ROLE\u USER”)){
getRedirectStrategy().sendRedirect(请求、响应、userTargetUrl);
}
否则{
super.onAuthenticationSuccess(请求、响应、身份验证);
返回;
}
}
}