【问题标题】:Welcome file ignores security constraint欢迎文件忽略安全约束
【发布时间】:2023-04-03 03:50:01
【问题描述】:

我的web.xml:

    <context-param>
            <param-name>javax.faces.PROJECT_STAGE</param-name>
            <param-value>Development</param-value>
        </context-param>

        <welcome-file-list>
            <welcome-file>/secured/secure.xhtml</welcome-file>
        </welcome-file-list>

        <servlet>
            <servlet-name>Faces Servlet</servlet-name>
            <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>

        <servlet-mapping>
            <servlet-name>Faces Servlet</servlet-name>
            <url-pattern>*.xhtml</url-pattern>
        </servlet-mapping>

        <context-param>
            <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
            <param-value>true</param-value>
        </context-param> 
    <security-constraint>
        <web-resource-collection>
          <web-resource-name>Restricted</web-resource-name>
          <url-pattern>/secured/*</url-pattern>
          <http-method>GET</http-method>
          <http-method>POST</http-method>
        </web-resource-collection>
        <auth-constraint>
          <role-name>ADMIN</role-name>
        </auth-constraint>
      </security-constraint>
<login-config>
     <auth-method>FORM</auth-method>
     <realm-name>jdbc-realm</realm-name>
     <form-login-config>
       <form-login-page>/public/login.xhtml</form-login-page>
       <form-error-page>/public/error.xhtml</form-error-page>
     </form-login-config>
   </login-config>

我希望我的网络应用程序将未经授权的用户重定向到登录页面。有趣的是我有这个工作,但我做了一些愚蠢的改变,现在访问localhost:8080我总是看到secure.xhtml,即使没有登录。localhost:8080/secured/secure.xhtml重定向很好。

【问题讨论】:

    标签: jsf servlets security-constraint welcome-file


    【解决方案1】:

    您没有完全正确地使用&lt;welcome-file&gt;。它应该代表在请求文件夹时需要提供的文件的唯一文件名,而与请求的文件夹无关(根/,或/public/ 或/secured/ 等)。

    欢迎文件由RequestDispatcher#forward() 执行的内部转发提供。内部转发不会触发安全约束。您需要改为发送重定向。

    将&lt;welcome-file&gt; 更改为更合理的默认值,例如index.xhtml。

    <welcome-file-list>
        <welcome-file>index.xhtml</welcome-file>
    </welcome-file-list>
    

    并在 webapp 的根目录中创建一个,例如 /index.xhtml。如果您需要将/index.xhtml 上的每个 请求重定向到/secured/secure.xhtml,那么基本上有两种方式:

    1. 在/index.xhtml 的URL 模式上映射Filter,并在doFilter() 方法中调用response.sendRedirect("secured/secure.xhtml")。例如

      @WebFilter("/index.xhtml")
      public class IndexFilter implements Filter {
      
          @Override
          public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
              HttpServletResponse response = (HttpServletResponse) res;
              response.sendRedirect("secured/secure.xhtml"));
          }
      
          // ...
      }
      
    2. 将&lt;f:event type="preRenderView"&gt; 放在/index.xhtml 中,它调用一个支持bean 方法,该方法又执行一个externalContext.redirect("secured/secure.xhtml")。例如

      <f:event type="preRenderView" listener="#{indexBean.redirect}" />
      

      与

      @ManagedBean
      @ApplicationScoped
      public class IndexBean {
      
          public void redirect() throws IOException {
              FacesContext.getCurrentInstance().getExternalContext().redirect("secured/secure.xhtml");
          }
      
      }
      

    【讨论】:

    • 哇,谢谢!但是检查用户是否登录呢?我应该在同一个过滤器中这样做吗?可以把它从容器中拿走吗?
    • &lt;security-constraint&gt; 已经做到了。你不需要自己做。唯一的一点是您需要执行重定向而不是转发。重定向会创建一个全新的请求,然后该请求将使用新 URL 再次触发安全约束。对于转发来说,为时已晚,因为安全约束已经根据/ 的初始 URL 进行了测试,根据您的配置,可以不受约束地公开访问。
    • 过滤器有问题。如果我没有调用 security-constraint 过滤器,它会接缝,如果我注释掉 constraint 它开始工作。也许是因为它们在 /secured/* 页面上重叠?服务器 - glassfish 3.1。如果你有时间,请帮帮我。
    • 显然您已将过滤器的 URL 模式更改为与安全约束匹配的模式,或者您已将安全约束也更改为匹配 /index.xhtml。这不是本意。
    • 非常感谢,完美运行。但是我还有 1 个问题,如果您不介意的话:是否可以将某些过滤器映射到与约束相同的 url 模式并在约束之前调用它,例如,如果我想检查 cookie 或类似的东西?
    猜你喜欢
    • 2011-07-03
    • 1970-01-01
    • 2012-11-23
    • 2011-07-22
    • 2016-11-04
    • 2015-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多