【问题标题】:How to exclude one url from authorization如何从授权中排除一个 url
【发布时间】:2013-02-08 01:46:46
【问题描述】:

我的 web.xml 看起来像:

<security-constraint>
    <web-resource-collection>
        <web-resource-name>app</web-resource-name>
        <url-pattern>/*</url-pattern>
    </web-resource-collection>
    <auth-constraint>
        <role-name>Role</role-name>
    </auth-constraint>
</security-constraint>

这可以保护每一方免受授权,但我想排除 /info。这可能吗?

【问题讨论】:

    标签: java jakarta-ee authentication web.xml


    【解决方案1】:

    如果您正在寻找带有 Spring boot 解决方案的 keycloak,请在您的应用程序属性文件中尝试这样:

    keycloak.security-constraints[0].authRoles[0]=users
    keycloak.security-constraints[0].security-collections[0].patterns[0]=/*
    keycloak.security-constraints[1].security-collections[0].patterns[0]=/info
    

    这将对所有 URL 应用安全性,除了 /info

    【讨论】:

      【解决方案2】:

      我不知道我是否正确!以我有限的知识,我认为为了实现安全性,要使用一个或多个 web-resource-collection 元素声明要保护的内容。每个 web-resource-collection 元素都包含一系列可选的 url-pattern 元素,后跟一系列可选的 http-method 元素。 url-pattern 元素值指定 URL 模式,请求 URL 必须与该 URL 模式匹配,以使请求对应于访问受保护内容的尝试。 http-method 元素值指定允许的 HTTP 请求类型。

      <security-constraint>
          <web-resource-collection>
              <web-resource-name>Secure Content</web-resource-name>
              <url-pattern>/restricted/*</url-pattern>
          </web-resource-collection>
          <auth-constraint>
              <role-name>AuthorizedUser</role-name>
          </auth-constraint>
          <user-data-constraint>
              <transport-guarantee>NONE</transport-guarantee>
          </user-data-constraint>
      </security-constraint>
      <!-- ... -->
      <login-config>
          <auth-method>BASIC</auth-method>
          <realm-name>The Restricted Zone</realm-name>
      </login-config>
      <!-- ... -->
      <security-role>
          <description>The role required to access restricted content </description>
          <role-name>AuthorizedUser</role-name>
      </security-role>
      

      位于 Web 应用程序 /restricted 路径下的 URL 需要 AuthorizedUser 角色。

      【讨论】:

        【解决方案3】:

        对于不需要身份验证的资源,省略 &lt;security-constraint&gt; 中的 &lt;auth-constraint&gt; 元素,例如:

        <security-constraint>
            <web-resource-collection>
                <web-resource-name>app</web-resource-name>
                <url-pattern>/info</url-pattern>
            </web-resource-collection>
            <!-- OMIT auth-constraint -->
        </security-constraint>
        
        <security-constraint>
            <web-resource-collection>
                <web-resource-name>app</web-resource-name>
                <url-pattern>/*</url-pattern>
            </web-resource-collection>
            <auth-constraint>
                <role-name>Role</role-name>
            </auth-constraint>
        </security-constraint>
        

        【讨论】:

        • 我试过这种方法但没有奏效。这有效吗?
        • /path 对我不起作用,而 /path/* 适用于 /path 下的所有文件.此外, /path/myfile.xhtml 适用于单个文件,但 /path/*.xhtml 不起作用
        • 我尝试过并且工作正常,但请注意容器特定授权:在我的情况下,wildfly 保护所有资源,因此您也必须记住这一点
        【解决方案4】:

        一种解决方案是使用替代安全框架,例如Apache Shiro,而不是基于容器的安全性。然后很容易从受保护的内容中排除资源。使用 Shiro 你可以输入WEB-INF/shiro.ini:

        [urls]
        /info = anon
        /**   = authc
        

        【讨论】:

          猜你喜欢
          • 2015-12-03
          • 2015-04-03
          • 2011-06-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-02-27
          • 2012-02-17
          • 1970-01-01
          相关资源
          最近更新 更多