3

新しいロールは CAS サーバーで作成されており、それらを制御できないため、新しいロールを作成できないことを考えると、ユーザーが「顧客」と「プロフェッショナル」の両方を持っている場合にのみ開く PDF ファイルを保護する方法はありますか? 「役割?

つまり、次の 3 人のユーザーを考えてみます。

user1 には「customer」ロールのみが割り当てられます user2 には「customer」ロールと「professional」ロールが割り当てられます user3 には「customer」ロールと「professional」ロールが割り当てられます user4 には「professional」ロールのみが割り当てられます

user2 と user3 のみが PDF を表示できるようにする必要があります。

基本的に、私は次のようなことをしたいと思います:

<security-constraint>
    <web-resource-collection>
        <web-resource-name>auth</web-resource-name>
        <url-pattern>/doc/profesionalCustomer.pdf</url-pattern>
    </web-resource-collection>
    <auth-constraint>
        <role-name>professional,customer</role-name>
    </auth-constraint>
</security-constraint>

これは可能ですか?

前もって感謝します

4

2 に答える 2

6

This is not possible using declarative security (i.e. via web.xml). You can only list roles that have access to a resource like in the following:

<security-constraint>
<web-resource-collection>
    <web-resource-name>auth</web-resource-name>
    <url-pattern>/doc/profesionalCustomer.pdf</url-pattern>
</web-resource-collection>
<auth-constraint>
    <role-name>professional</role-name>
    <role-name>customer</role-name>
</auth-constraint>

however in this case you would grant access to all users that have either professional or customer role which is not what you want. There is no construct that allows you to grant access for a user that has a combination of roles.

One way you can go about it is to deal with it programmatically: direct a client to a servlet that examines whether the user is in both customer and professional role using HttpServletRequest#isUserInRole(String) and if it is forwards the request to the default servlet which retrieves the pdf. Furthermore if you want to defer what combination of roles are granted access to deployment time, rather then hard-coding it in the servlet you can have the granting servlet parameterized appropriately through /web-app/servlet/init-param or /web-app/context-param element of your web.xml.

The following is web.xml excerpt that would support this:

<servlet>
    <servlet-name>PDF Retriever</servlet-name>
    <servlet-class>com.stackoverflow.PDFRetrieverServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>PDF Retriever</servlet-name>
    <url-pattern>/docs/pdf/*</url-pattern>
</servlet-mapping>
<security-constraint>
    <web-resource-collection>
        <web-resource-name>PDF Docs - customer and professional only</web-resource-name>
        <url-pattern>/docs/pdf/*</url-pattern>
    </web-resource-collection>
    <auth-constraint>
        <role-name>*</role-name>
    </auth-constraint>
</security-constraint>
<security-constraint>
    <web-resource-collection>
        <web-resource-name>PDF Docs Private</web-resource-name>
        <url-pattern>/private/pdf/*</url-pattern>
    </web-resource-collection>
    <auth-constraint>
        <role-name />
    </auth-constraint>
</security-constraint>`

and here is coding for doGet of the servlet:

protected void doGet(HttpServletRequest request, 
    HttpServletResponse response) throws ServletException, IOException {
    if (request.isUserInRole("customer") && request.isUserInRole("professional")) {
         String urlSuffix = request.getPathInfo();
         RequestDispatcher rd = request.getRequestDispatcher("/private/pdf"
              + urlSuffix);
          rd.forward(request, response);
     } else {
          response.sendError(HttpServletResponse.SC_FORBIDDEN);
     }
}
于 2013-02-06T21:52:39.347 に答える
0

このトピックについて少し読みたかったのですが、以下のリンクが非常に役立つことがわかりました。

http://www.devarticles.com/c/a/Java/Securing-Struts-Applications/1/

于 2014-10-14T15:52:15.557 に答える