如何从授权中排除一个网址

时间:2013-02-22 12:27:16

标签: java java-ee authentication web.xml

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

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

5 个答案:

答案 0 :(得分:78)

忽略<auth-constraint>中的<security-constraint>元素,查找不需要身份验证的资源,如:

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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

我不知道我是否帮助你!由于我的知识有限,我认为为了实现安全性,使用一个或多个web-resource-collection元素声明要保护的内容。每个web-resource-collection元素都包含一系列可选的url-pattern元素,后跟一系列可选的http-method元素。 url-pattern元素值指定一个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应用程序的限制路径下的URL需要AuthorizedUser角色。

答案 3 :(得分:0)

我已尝试过@ user517491解释的上述方式,但我的tomcat 7会过滤/使用https的所有流量,但忽略上一个异常......

Y

如何使用https ???

添加使用http和所有其他流量的例外

答案 4 :(得分:0)

如果您要使用 Spring boot 解决方案寻找钥匙斗篷,请在应用程序属性文件中尝试以下操作:

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

这将对除 / info

以外的所有URL应用安全性