欢迎文件忽略安全性约束

Iva*_*ich 3 jsf servlets security-constraint welcome-file

我的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>
Run Code Online (Sandbox Code Playgroud)

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

Bal*_*usC 7

你没有<welcome-file>完全正确使用.应该表示其需要每当被请求的文件夹要服务的文件的文件名唯一,不论所请求的文件夹(根/,或/public//secured/等).

欢迎文件由内部转发提供,由执行RequestDispatcher#forward().内部转发不会触发安全限制.您需要发送重定向.

更改<welcome-file>为更合理的默认值,例如index.xhtml.

<welcome-file-list>
    <welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
Run Code Online (Sandbox Code Playgroud)

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

  1. 映射a Filter的URL模式/index.xhtmlresponse.sendRedirect("secured/secure.xhtml")doFilter()方法内部调用.例如

    @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"));
        }
    
        // ...
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 把一个<f:event type="preRenderView">/index.xhtml其调用后台bean的方法这又做了externalContext.redirect("secured/secure.xhtml").例如

    <f:event type="preRenderView" listener="#{indexBean.redirect}" />
    
    Run Code Online (Sandbox Code Playgroud)

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

  • `<security-constraint>`已经这样做了.你不需要自己动手.唯一的一点是你需要执行重定向而不是前进.重定向会创建一个全新的请求,然后使用新的URL再次触发安全约束.对于前进而言已经太晚了,因为安全约束已经*已经基于`/`的初始URL进行了测试,这是根据您的配置公开访问而没有约束. (2认同)