私はストラットと春のセキュリティに新しいです。 誰かが別の役割を持つ別のユーザーに異なる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
ありがとうございました。あなたの反応はとても助けになりました。私はいくつかの質問があります。 1.なぜあなたはrequestCache.removeRequest(request、response)が必要ですか? 2.なぜ要求には役割がなく、なぜrequest.isUserInRole()を使用できないのですか?おかげさまで – vlr
1)私は確信していませんが、Springが元のブラウザの要求をキャッシュしてからログインに成功した後にユーザーをリダイレクトするために使用するログインプロセスにキャッシュするためです。したがって、ユーザーが保護されたリソースを要求すると、Springは要求をキャッシュし、認証を実行して、ユーザーが最初に要求したページにユーザーをリダイレクトします。ログイン後に特定のページにユーザーを強制しているため、最初にリクエストされたページは必要ありません。ちょうど整理しています。 – user497087
2)わからない、request.isUserInRole()は動作するはずです。あなたのアクションの基本クラスとしてActionSupportを確実に拡張し、isUserInRole()を使用することは私にとっては効果的です。 – user497087