在javax.faces.webapp.FacesServlet
文档中提到了
允许的HTTP方法
JSF规范仅要求使用GET和POST http方法。如果您的Web应用程序不需要任何其他http方法(例如PUT和DELETE),请考虑使用
<http-method>
and<http-method-omission>
元素限制允许的http方法。有关使用这些元素的更多信息,请参见Java Servlet规范的安全性。
我的应用程序确实不依赖于其他HTTP方法(GET
和除外POST
)。因此,我尝试使用<http-method>
(或<http-method-omission>
)排除除GET
和以外的所有方法POST
。
在web.xml中,如下配置JAAS Servlet安全约束。
<security-constraint>
<display-name>AdminConstraint</display-name>
<web-resource-collection>
<web-resource-name>ROLE_ADMIN</web-resource-name>
<description/>
<url-pattern>/admin_side/*</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint>
<description/>
<role-name>ROLE_ADMIN</role-name>
</auth-constraint>
<user-data-constraint>
<description/>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
<security-constraint>
<display-name>UserConstraint</display-name>
<web-resource-collection>
<web-resource-name>ROLE_USER</web-resource-name>
<description/>
<url-pattern>/user_side/*</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint>
<description/>
<role-name>ROLE_USER</role-name>
</auth-constraint>
<user-data-constraint>
<description/>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
使用这些元素,
<http-method>GET</http-method>
<http-method>POST</http-method>
我希望所有其他HTTP方法都是不允许的。
但是,GlassFish Server 4.1在服务器终端上记录以下警告。
警告:JACC:对于URL模式
/user_side/*
,除以下方法外,所有其他方法均未发现:POST,GET警告:JACC:对于URL模式
/admin_side/*
,除以下方法外,所有其他方法均未发现:POST,GET
这是什么意思?
另外<security-constraint>
,也可以在全局中配置它,而不是在所有元素中都使用它,以便可以将其应用于应用程序中的所有资源,GET
并且POST
可以省略所有HTTP请求和HTTP请求之外的所有请求,也就是可以全局地应用于应用程序-也许可以使用更通用的专用工具。像url模式/*
?
有一个例子在这里。
<security-constraint> <display-name>WebConstraint</display-name> <web-resource-collection> <web-resource-name>test</web-resource-name> <description/> <url-pattern>/test.jsp</url-pattern> <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>dev</role-name> </auth-constraint> </security-constraint>
上述元素表示由url模式/test.jsp引用的资源在被除GET之外的所有http方法访问时,应限制为仅由属于角色dev的经过身份验证的用户查看。请注意,安全性约束不适用于http方法GET,而仅适用于其他方法(POST,HEAD,PUT等)。
我发现强文本中的最后一句话令人困惑。这是否意味着使用GET
请求,匿名用户也可以访问给定url模式中列出的资源,因为这意味着“安全性约束不适用于http-method GET ”?
这是什么意思?
这意味着除GET和POST之外的所有方法均被发现,即不受保护。每个人都可以使用/user_side/*
PUT和HEAD之类的方法访问url模式,而无需进行身份验证。
为了保护其他方法,请添加以下内容:
<security-constraint>
<web-resource-collection>
<web-resource-name>protected</web-resource-name>
<url-pattern>/user_side/*</url-pattern>
<http-method-omission>GET</http-method-omission>
<http-method-omission>POST</http-method-omission>
</web-resource-collection>
<auth-constraint/>
</security-constraint>
如果使用的是Servlet 3.1,则还可以使用更短的标记:
<deny-uncovered-http-methods/>
另外,也可以在全局中配置它,而不是在所有元素中都使用它,以便可以将其应用于应用程序中的所有资源,并且可以忽略除GET和POST HTTP请求以外的所有请求,即,将其全局应用到应用程序-也许使用更多像/ *这样的通用url模式?
是的,这是可能的。您可以使用url模式/
包括所有子文件夹。
我发现强文本中的最后一句话令人困惑。这是否意味着使用GET请求,匿名用户也可以访问给定url模式中列出的资源,因为这意味着“安全性约束不适用于http-方法GET”?
没错,这意味着匿名用户可以使用GET方法访问给定的url-pattern。所有其他方法均受保护。
也可以看看:
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句