0

セッションの開始時にセッション変数を取得し、結果に基づいて特定のページへのアクセスのみを許可する単純な使用例があります。bindInterceptor を使用して任意のページの @Get メソッドまたは @Post メソッドをインターセプトするのが最善なのか、それともフィルターを使用する方が良いのか、はっきりしていません。これは私がやりたいことのスケッチですが、代替手段を受け入れています:

At the start of a new session (@SessionScoped ?), check a session variable authentication token

If (authentication == admin) {
    serveRegex("admin/(jsp|html)/.*").with(GuiceContainer.class);   //only allow /admin subpages
    req.getRequestDispatcher("/admin").forward(req, res); //fwd all initial page requests to /admin
}
else If (authentication == user) {
    serveRegex("user/(jsp|html)/.*").with(GuiceContainer.class);  //only allow /user subpages
    req.getRequestDispatcher("/user").forward(req, res); //fwd all initial page requests to /user
}
else {
    serveRegex("signin/(jsp|html)/.*").with(GuiceContainer.class);  //only allow /signin subpages
    req.getRequestDispatcher("/signin").forward(req, res);  //fwd all initial page requests to /signin
}

このセキュリティ モデルを管理するための推奨される手法 (最小コード、最速など) はどれですか? サンプルプロジェクトを見てみたいです。

ご協力いただきありがとうございます!

-ジョン

4

1 に答える 1

1

これを行う一般的な方法は、フィルターを使用することです。必要なアクセス許可ごとに URI スペースを分離しているように見えることを考えると、それがおそらく最も簡単な方法でもあります。@AdminRequiredbindInterceptor スタイルは、メソッド/クラス (" " など) で宣言された認証ロジックが必要な場合に役立ちますが、実際にはそうする正当な理由はありません。URI スペースを分離する方が簡単です。

現在のユーザー/承認ロジックを取得し、アクセス許可が要求先の URI と一致するかどうかを確認するフィルターをバインドするだけです。

例えば

class AuthenticationFilter implements Filter {

  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
    User user = getUserSomehow();
    if (user == null) {
      response.sendRedirect(... login page ...);
      return;
    }
    if (request.getRequestURI().startsWith("/admin")) {
      // Enforce Admin login, error out otherwise.
    }
    // Proceed with executing the request.
    chain.doFilter(request, response);
  }
}

ServletRequest/Response を HttpServletRequest/Response にダウンキャストする必要があることに注意してください。

于 2012-08-26T17:38:28.820 に答える