【问题标题】:Web.xml Security Constraints not workingWeb.xml 安全约束不起作用
【发布时间】:2013-07-30 13:23:12
【问题描述】:

尝试让我的 Web 应用程序的安全方面正常运行。

我在 Eclipse 中创建了一个动态 Web 应用程序,并尝试使用基于表单的身份验证设置。

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0"
 xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
 <display-name>Application</display-name>
 <context-param>
  <param-name>javax.ws.rs.Application</param-name>
  <param-value>com.foo.bar.webservices.MyApplication</param-value>
 </context-param>
 <context-param>
  <param-name>resteasy.servlet.mapping.prefix</param-name>
  <param-value>/resteasy</param-value>
 </context-param>
 <listener>
  <listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
 </listener>
 <servlet>
  <servlet-name>Resteasy</servlet-name>
  <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
 </servlet>
 <servlet>
  <display-name>LoginServlet</display-name>
  <servlet-name>LoginServlet</servlet-name>
  <servlet-class>httpAuth.LoginServlet</servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>Resteasy</servlet-name>
  <url-pattern>/resteasy/*</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
  <servlet-name>LoginServlet</servlet-name>
  <url-pattern>/LoginServlet</url-pattern>
 </servlet-mapping>
 <welcome-file-list>
  <welcome-file>/login.jsp</welcome-file>
 </welcome-file-list>
 <security-constraint>
  <display-name>Authorized Only</display-name>
  <web-resource-collection>
   <web-resource-name>Authorized Only</web-resource-name>
   <url-pattern>/restricted/*</url-pattern>
   <http-method>GET</http-method>
   <http-method>PUT</http-method>
  </web-resource-collection>
  <auth-constraint>
   <description>Allowed users</description>
   <role-name>USER</role-name>
  </auth-constraint>
  <user-data-constraint>
   <transport-guarantee>NONE</transport-guarantee>
  </user-data-constraint>
 </security-constraint>
 <login-config>
  <auth-method>FORM</auth-method>
  <form-login-config>
   <form-login-page>/login.jsp</form-login-page>
   <form-error-page>/logonError.jsp</form-error-page>
  </form-login-config>
 </login-config>
 <security-role>
  <role-name>USER</role-name>
 </security-role>
</web-app>

但是,当我部署并转到 http://localhost:8080/Application/restricted/index.jsp 时,它会显示,这是不应该的。

编辑 1:已更改以删除 /Application。这样做不适用于 /restricted/index.jsp 等页面

文件夹分类

Application
   +build
   -WebContent
     +css
     +img
     +js
     login.jsp
     logonError.jsp
    +META-INF
    -restricted
      index.jsp
    +WEB-INF

【问题讨论】:

    标签: java jakarta-ee web.xml


    【解决方案1】:

    您似乎应用了错误的url-pattern。尝试更改此设置

    <url-pattern>/Application/restricted/*</url-pattern>
    

    通过这个

    <url-pattern>/restricted/*</url-pattern>
    

    【讨论】:

      【解决方案2】:

      在我们的组织中,我们使用安全注释。根据我的经验,设置和实施起来相当容易和直接。我们碰巧将 IBM WebSphere 用于我们的应用程序服务器,但安全注释可用于任何支持 Java EE 5 的服务器。

      Oracle 对此有一篇很好的文章:http://www.oracle.com/technetwork/articles/javaee/security-annotation-142276.html

      在网络上搜索“Java 安全注释”以获取更多信息。

      【讨论】:

        【解决方案3】:

        对于您的 servlet 映射,您正在使用此模式:

        <url-pattern>/resteasy/*</url-pattern>
        

        但是对于您使用此模式的安全约束:

        <url-pattern>/Application/restricted/*</url-pattern>
        

        这些必须匹配。

        我只能假设这个 Web 应用程序不是从 ROOT 上下文运行,而是从 /Application root 运行。 web.xml 中的模式锚定在上下文中,因此您应该从 url-pattern 中删除 /Application 前缀。

        【讨论】:

        • 正确。 index.jsp 页面在localhost:8080/Application/login.jsp 下,然后我在localhost:8080/Application/restricted/index.jsp 中有我的受限信息
        • @PSU_Kardi。 /Application 是您的上下文名称吗?而且,顺便说一句,你不能在localhost:8080/Application/login.jsp 下拥有index.jsp
        • 当我转到 /restricted 时,进行更改会弹出登录表单;但是,它不适用于下面的文件,即restricted/index.jsp
        【解决方案4】:

        如果您正在通过浏览器测试访问权限,那么如果您之前曾在该浏览器中登录过 Google,那么 &lt;security-constraint&gt; 可能无法正常工作。登录可以是持久的,并且可能会被拾取。在不同的浏览器中检查 URL 是值得的 - 您可能会发现安全性随后起作用。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-11-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-01-18
          相关资源
          最近更新 更多