【问题标题】:Exclude one role in web.xml在 web.xml 中排除一个角色
【发布时间】:2015-06-10 18:09:43
【问题描述】:

我的 Web 应用程序中有一个 web.xml,其中包含类似于以下内容的安全约束。

<security-constraint>
    <web-resource-collection>
        <web-resource-name>Application</web-resource-name>
        <url-pattern>/*</url-pattern>
        <http-method>GET</http-method>
        <http-method>POST</http-method>
        <http-method>PUT</http-method>
        <http-method>DELETE</http-method>
    </web-resource-collection>
    <auth-constraint>
        <role-name>*</role-name>
    </auth-constraint>
    <user-data-constraint>
        <transport-guarantee>NONE</transport-guarantee>
    </user-data-constraint>
</security-constraint>

<security-role>
    <description>Any logged in user can access this application.</description>
    <role-name>*</role-name>
</security-role>

这将允许任何登录用户访问该应用程序。有没有办法允许任何用户访问应用程序,除非他们具有特定角色?

类似这样的:

<security-constraint>
    <web-resource-collection>
        <web-resource-name>Application</web-resource-name>
        <url-pattern>/*</url-pattern>
        <http-method>GET</http-method>
        <http-method>POST</http-method>
        <http-method>PUT</http-method>
        <http-method>DELETE</http-method>
    </web-resource-collection>
    <auth-constraint>
        <role-name>*</role-name>
    </auth-constraint>
    <exclude-role>
        <role-name>exclude</role-name>
    </exclude-role>
    <user-data-constraint>
        <transport-guarantee>NONE</transport-guarantee>
    </user-data-constraint>
</security-constraint>

<security-role>
    <description>Any logged in user can access this application.</description>
    <role-name>*</role-name>
</security-role>

<security-role>
    <description>Except for users with this role.</description>
    <role-name>exclude</role-name>
</security-role>

我可以列出应该有权访问的每个角色,但是角色数量很多,而且我们不断添加新角色,这些新角色也应该有权访问此应用程序,因此我需要一种方法来仅排除单个角色.

【问题讨论】:

    标签: java xml web-applications war web.xml


    【解决方案1】:

    您可以使用过滤器:

    在下面的链接中解释得很好

    访问http://www.avajava.com/tutorials/lessons/what-is-a-filter-and-how-do-i-use-it.html

    【讨论】:

    • 我认为该链接不能很好地解释它。它只是显示了如何使用它的示例代码,而不是与我的问题相关的示例。仍然不确定我将如何使用它来过滤掉特定角色。我可以在过滤器中执行response.sendError(HttpServletResponse.SC_FORBIDDEN); 之类的操作吗?
    • 你是对的,你可以用同样的方式做 else if (!ipList.contains(httpReq.getRemoteAddr())) { logger.warn("尝试对脚本 Vault:: 进行非特权修改:" +远程地址); httpResponse.setStatus(HttpServletResponse.SC_FORBIDDEN); httpResponse.getWriter().write("未从IP地址授予权限" + remoteAddress); httpResponse.flushBuffer();返回; }
    猜你喜欢
    • 1970-01-01
    • 2014-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-25
    • 2021-11-06
    • 2012-02-21
    相关资源
    最近更新 更多