1

Glassfish 3.1 + JDBCRealm + MySQL(MD5)を使用してFORMベースの認証を実装しました。ユーザーと管理者の2つの役割しかありません。すべてが順調に進んでいます。ログから、認証が使用者としても管理者としても機能していることがわかります(以下のログをご覧ください)

Q1:ユーザーがadminの場合は/admin/index.xhtmlに移動し、ユーザーがロールuserの場合はfaces / user / index.xhtmlに直接移動するように、2つの異なるインデックスファイルを作成することはできますか?

Q2:ユーザーとしてログインした場合でも、ブラウザのアドレスフィールドにリンク全体を直接書き込むだけで「管理者側」に移動できます。なぜそれを回避するのですか?

Q3:ユーザーとしてログインし、ウェルカムファイルリストにfaces / admin / index.xhtmlしかない場合、xmlファイルが他のことを示していても、そのファイルにリダイレクトされます。なぜですか?

<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>

ログ:

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には、そのような役割があります。私が正しく理解していれば、それはadminがadmin、customer、userのグループに属していることを意味します。顧客はグループに属します:顧客とユーザーおよびユーザーはグループユーザーに属します。正しく理解できましたか?

    <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>

ありがとうございました!サーミ人

4

3 に答える 3

1

私は大学のクラスの一部としてこれを試みたところです。これが、あなたが求めていると思う機能をどのように取得したかです。Glassfish 4.1.1サーバーでNetbeansを使用しており、サーバーファイルレルムでユーザーロールをすでに構成しています。

私のプロジェクトには3つのファイルがあります:

index.xhtml users/mainmenu.xhtml admin/mainmenu.xhtml

ウェルカムページはindex.xhtml、次のハイパーリンクで設定されています。

 <h4>
     <a href="/ED-Secure-war/faces/admin/mainmenu.xhtml">
         Admin Login
     </a>
 </h4>
 <h4>
     <a href="/ED-Secure-war/faces/user/mainmenu.xhtml">
           User Login
        </a>
 </h4>

web.xmlのセキュリティセクションで、次のロールを構成しています

これらのそれぞれへのアクセスはユーザーグループを介して制限されているため、インデックスのハイパーリンクをクリックすると、ログインするように求められます。管理者リンクに有効な管理者ログインを入力するとadmin/mainmenu.xhtml、にリダイレクトされます。ユーザーログインの場合はその逆になります。

于 2020-04-29T12:20:01.473 に答える
0

質問1の場合:ユーザーを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);
}
于 2012-03-07T11:54:35.547 に答える
0

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

A2)それは起こらないはずです。JDBCRealmでのロールを確認する必要があります。私がここで見ているように、すべてが正しい方法で構成されています。

A3)あなたの発言「XML」ファイルを正しく理解しているかどうかわかりません。ただし、ウェルカムファイルはロールにバインドされていません。..A1を参照)

ありがとう、M

于 2012-02-29T12:10:11.070 に答える