2012-02-27 3 views
1

Glassfish 3.1 + JDBCRealm + MySQL(MD5)でFORMベースの自動化を実装しました。私はuserとadminという2つの役割しか持っていません。は、それがそのように、2つの異なるインデックス・ファイルを作成することは可能です:すべてが素晴らしい起こっている、私は(ウォッチ下記ログ)認証がusetとして、および管理者としての両方のケースで働いているログインJava EE 6/Glassfishのユーザー役割に応じて2つの異なるウェルカムページにリダイレクト

Q1から見ることができますユーザーがadminの場合は/admin/index.xhtmlに行き、ユーザーがロールのユーザーの場合はfaces/user/index.xhtmlに直接進みます。

Q2:私はユーザーとしてログインしたときに今、私はまだそれを避けるために、どのように、なぜ、JA、単にブラウザでフィールドに対処するためにストレート全体のリンクを足すと、「管理者側」に行くことができますか?

Q3:私はユーザーとしてログインし、私は/が管理/いるindex.xhtmlウェルカムファイルリストで、それはxmlファイルが何かを伝えても、そのファイルに私をリダイレクト直面ONLYいる、なぜ?

<welcome-file-list> 
     <welcome-file>faces/admin/index.xhtml</welcome-file> *?? ----> it goes always here, cause it is the first one I think?* 
     <welcome-file>faces/user/index.xhtml</welcome-file> 
    </welcome-file-list> 

    <security-constraint> 
     <display-name>Admin Pages</display-name> 
     <web-resource-collection> 
      <web-resource-name>Protected Admin Area</web-resource-name> 
      <description/> 
      <url-pattern>/faces/admin/*</url-pattern> 
      <http-method>GET</http-method> 
      <http-method>POST</http-method> 
      <http-method>HEAD</http-method> 
      <http-method>PUT</http-method> 
      <http-method>OPTIONS</http-method> 
      <http-method>TRACE</http-method> 
      <http-method>DELETE</http-method> 
     </web-resource-collection> 
     <auth-constraint> 
      <description/> 
      <role-name>admin</role-name> 
     </auth-constraint> 
    </security-constraint> 
    <security-constraint> 
     <display-name>User Pages</display-name> 
     <web-resource-collection> 
      <web-resource-name>Protected Users Area</web-resource-name> 
      <description/> 
      <url-pattern>/faces/users/*</url-pattern> 
      <!--url-pattern>/faces/users/index.xhtml</url-pattern--> 
      <http-method>GET</http-method> 
      <http-method>POST</http-method> 
      <http-method>HEAD</http-method> 
      <http-method>PUT</http-method> 
      <http-method>OPTIONS</http-method> 
      <http-method>TRACE</http-method> 
      <http-method>DELETE</http-method> 
     </web-resource-collection> 
     <auth-constraint> 
      <description/> 
      <role-name>user</role-name> 
     </auth-constraint> 
    </security-constraint> 

    <login-config> 
     <auth-method>FORM</auth-method> 
     <realm-name>JDBCRealm</realm-name> 
     <form-login-config> 
      <form-login-page>/faces/loginForm.xhtml</form-login-page> 
      <form-error-page>/faces/loginError.xhtml</form-error-page> 
     </form-login-config> 

    </login-config> 
</web-app> 

LOG:

FINE: Login module initialized: class com.sun.enterprise.security.auth.login.JDBCLoginModule 
FINEST: JDBC login succeeded for: admin groups:[admin, user] 
FINE: JAAS login complete. 
FINE: JAAS authentication committed. 
FINE: Password login succeeded for : admin 
FINE: Set security context as user: admin 
FINE: [Web-Security] Setting Policy Context ID: old = null ctxID = jdbcrealm/jdbcrealm 
FINE: [Web-Security] hasUserDataPermission perm: (javax.security.jacc.WebUserDataPermission GET) 
FINE: [Web-Security] hasUserDataPermission isGranted: true 
FINE: [Web-Security] Policy Context ID was: jdbcrealm/jdbcrealm 
FINE: [Web-Security] Codesource with Web URL: file:/jdbcrealm/jdbcrealm 
FINE: [Web-Security] Checking Web Permission with Principals : null 

(myfearの答えの後編集) は-----のglassfish-web.xmlので、私はそのような役割を持っています。私がそれを正しく理解すれば、それは管理者が管理者、顧客、およびユーザーのグループに属していることを意味します。顧客はグループに属します:顧客とユーザー、ユーザーはグループユーザーに属します。私はそれを正しく理解しましたか?

<security-role-mapping> 
    <role-name>admin</role-name> 
    <group-name>admin</group-name> 
    <group-name>customer</group-name> 
    <group-name>user</group-name> 
    </security-role-mapping> 
    <security-role-mapping> 
    <role-name>customer</role-name> 
    <group-name>customer</group-name> 
    <group-name>user</group-name> 
    </security-role-mapping> 
    <security-role-mapping> 
    <role-name>user</role-name> 
    <group-name>user</group-name> 
    </security-role-mapping> 

</glassfish-web-app> 

ありがとうございます! Sami

答えて

0

A1)ウェルカムファイルは役割に関係しません。ユーザーをディスパッチするためのロジックを行う必要がある場合は、ブール型HttpServletRequest.isUserInRole(文字列ロール)などを使用して、ユーザーがどのロールに属しているかを調べる必要があります。

A2)起こる。あなたはあなたのJDBCRealmでの役割をチェックする必要があります。私がここに見るものは、すべてが正しい方法で構成されています。

A3)あなたの発言「XML」ファイルが正しく理解されているかどうかはわかりません。しかし、歓迎-ファイルがロールにバインドされ、...)質問1については

おかげで、 M

+0

ありがとうmyfear!だから私は思ったように動作するはずです。 admin/faces/admin/*の場合admin-roleの人は誰にも入るべきではありませんか?私の場合、認証された人は誰でも入ることができます。少し質問を更新しました。 – Sami

0

A1を参照されていません。あなたは、特定のページのいずれかuserloginのにユーザーをリダイレクトすることができ、そこからフィルタを使用します。 xhtmlまたはadminlogin.xhtml

public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { 
    String userName = SecurityAssociation.getPrincipal().getName(); 
    String userNameSubject = SecurityAssociation.getSubject().toString(); 

    System.out.println("Yeeey! Get me here and find me in the database: " + userName+ " Subject : "+userNameSubject); 

    filterChain.doFilter(servletRequest, servletResponse); 
} 
+0

ありがとう!少し具体的にお願いしてください、私は冬の雪だるまのようです。認証がなければ、ユーザーはindex.xhtmlに行き、ログイン後、コントローラクラスがデータベースからデータを取得した後、別のページに移動します。ログイン---> JDBC-realm --->成功した場合、コントローラまたは他のクラスのデータをフェッチする---> DB ---->ビュー。このフィルタを実装する場所と、認証されたユーザーのデータを使用してdbからデータをフェッチするために、コンテナ管理による認証を中断する方法と場所 – Sami

+0

http://www.oracle.com/technetwork/java/filters-137243.html – Sami

関連する問題