0

私はストラットと春のセキュリティに新しいです。 誰かが別の役割を持つ別のユーザーに異なるURLにリダイレクトする方法を知る手助けはできますか?言い換えれば、アクションコントローラを使用してstruts2のユーザ役割に基づいてターゲットURLを決定する方法は?struts2のロールに基づいてターゲットURLを決定します。

私は以下の質問determine target url based on roles in spring security 3.1を見つけましたが、私はどのように行動を構成するか分かりません。

私は次のセットアップを試してみましたが、それは動作しません:

のsecurity.xml

<form-login login-page="/login" authentication-failure-url="/login?error=true" login-processing-url="/j_security_check" default-target-url="/default"/> 

struts.xml

<action name="default" class="com.moblab.webapp.action.RoleRedirectAction" method="defaultAfterLogin"/> 

RoleRedirectAction.java

package com.moblab.webapp.action; 
import javax.servlet.http.HttpServletRequest; 
public class RoleRedirectAction extends BaseAction{ 

public String defaultAfterLogin(HttpServletRequest request) { 
    if (request.isUserInRole("ROLE_ADMIN")) { 
     return "redirect:/<url>"; 
    } 
    return "redirect:/<url>"; 
} 
} 

ありがとうございます。

EDIT 1 私はまた、次の注釈

@Action(value="/default",results={@Result(name="success",location="/querySessions")}) 

EDIT 2 が私の最終的な解決策は、次のようになります試してみました。私はそれが最善のアプローチであるかどうかわからないのですが、それは動作します:

public class StartPageRouter extends SimpleUrlAuthenticationSuccessHandler { 


@Autowired 
private UserService userService; 

protected final Logger logger = Logger.getLogger(this.getClass()); 
private RequestCache requestCache = new HttpSessionRequestCache(); 

@Override 
public void onAuthenticationSuccess(HttpServletRequest request, 
            HttpServletResponse response, 
            Authentication authentication) throws IOException, ServletException { 


    Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities(); 

    //default path for ROLE_USER 
    String redirectPath = <url>; 

    if (authorities != null && !authorities.isEmpty()) { 

     Set<String> roles = getUserRoles(authorities); 

     if (roles.contains("ROLE_ADMIN")) 
      redirectPath = <url>; 
     else if (roles.contains("ROLE_INSTRUCTOR")) 
      redirectPath = <url>; 
    } 

    getRedirectStrategy().sendRedirect(request, response, redirectPath); 
} 

public void setRequestCache(RequestCache requestCache) { 
    this.requestCache = requestCache; 
} 

private Set<String> getUserRoles(Collection<? extends GrantedAuthority> authorities) { 

    Set<String> userRoles = new HashSet<String>(); 

    for (GrantedAuthority authority : authorities) { 
     userRoles.add(authority.getAuthority()); 
    } 
    return userRoles; 
} 
} 

EDIT 3 より良いソリューションがここにあります:あなたが欲しいことを意味すると仮定すると

http://oajamfibia.wordpress.com/2011/07/07/role-based-login-redirect/#comment-12

答えて

2

割り当てられた役割に応じてユーザーを別の開始ページにリダイレクトするには、これを試すことができます。私はStrutsの外でこれをすべて行うことに注意してください。

まず、Spring SimpleUrlAuthenticationSuccessHandlerを拡張する独自のクラスを作成し、onAuthenticationSuccess()メソッドをオーバーライドします。実際のリダイレクトはonAuthenticationSuccess()メソッド内で行getRedirectStrategy()によって実行されます。sendRedirect(request、response、);

あなたが必要とするのは、自分のURLを置き換える手段です。だから、

、例えば私はLoginRouterがログインしているユーザーと、割り当てられた役割からユーザーが移動するURLですか決定を取り、自分のクラスです

package com.blackbox.x.web.security; 

import java.io.IOException; 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import org.apache.log4j.Logger; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.security.core.Authentication; 
import org.springframework.security.core.userdetails.User; 
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler; 
import org.springframework.security.web.savedrequest.HttpSessionRequestCache; 
import org.springframework.security.web.savedrequest.RequestCache; 

import com.blackbox.x.entities.UserDTO; 
import com.blackbox.x.services.UserService; 


public class StartPageRouter extends SimpleUrlAuthenticationSuccessHandler { 


@Autowired 
UserService userService; 

@Autowired 
LoginRouter router; 


protected final Logger logger = Logger.getLogger(this.getClass()); 
private RequestCache requestCache = new HttpSessionRequestCache(); 

@Override 
public void onAuthenticationSuccess(HttpServletRequest request, 
     HttpServletResponse response, Authentication authentication) throws IOException, 
     ServletException { 


    requestCache.removeRequest(request, response); 

    User user = (User) authentication.getPrincipal(); 
    UserDTO userDTO = userService.find(user.getUsername()); 

    getRedirectStrategy().sendRedirect(request, response, router.route(userDTO)); 
} 

public void setRequestCache(RequestCache requestCache) { 
      this.requestCache = requestCache; 
     } 
} 

を持っています。

あなたは、あなたのセキュリティコンテキストxmlファイル内

authentication-success-handler-ref="customTargetUrlResolver"/> 

<beans:bean id="customTargetUrlResolver" class="com.blackbox.x.web.security.StartPageRouter"/> 

を使用したバージョンを使用するように春のセキュリティを設定します。

+0

ありがとうございました。あなたの反応はとても助けになりました。私はいくつかの質問があります。 1.なぜあなたはrequestCache.removeRequest(request、response)が必要ですか? 2.なぜ要求には役割がなく、なぜrequest.isUserInRole()を使用できないのですか?おかげさまで – vlr

+0

1)私は確信していませんが、Springが元のブラウザの要求をキャッシュしてからログインに成功した後にユーザーをリダイレクトするために使用するログインプロセスにキャッシュするためです。したがって、ユーザーが保護されたリソースを要求すると、Springは要求をキャッシュし、認証を実行して、ユーザーが最初に要求したページにユーザーをリダイレクトします。ログイン後に特定のページにユーザーを強制しているため、最初にリクエストされたページは必要ありません。ちょうど整理しています。 – user497087

+0

2)わからない、request.isUserInRole()は動作するはずです。あなたのアクションの基本クラスとしてActionSupportを確実に拡張し、isUserInRole()を使用することは私にとっては効果的です。 – user497087

関連する問題