Jsf 欢迎文件忽略安全约束

Jsf 欢迎文件忽略安全约束,jsf,servlets,security-constraint,welcome-file,Jsf,Servlets,Security Constraint,Welcome File,myweb.xml: <context-param> <param-name>javax.faces.PROJECT_STAGE</param-name> <param-value>Development</param-value> </context-param> <welcome-file-list>

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

javax.faces.PROJECT_阶段
发展
/secured/secure.xhtml
Facesservlet
javax.faces.webapp.FacesServlet
1.
Facesservlet
*.xhtml
javax.faces.explait_EMPTY_STRING_SUBMITTED_VALUES_为_NULL
真的
受限制的
/安全的/*
得到
邮递
管理
形式
jdbc领域
/public/login.xhtml
/public/error.xhtml
我希望我的web应用将未经授权的用户重定向到登录页面。有趣的是,我让它工作了,但我做了一些愚蠢的更改,现在在访问
localhost:8080时,我总是看到secure.xhtml,即使没有登录<代码>本地主机:8080/secured/secured.xhtml可以很好地重定向。

您没有完全正确地使用
。它应该表示在请求文件夹时需要提供的文件的唯一文件名,而不考虑请求的文件夹(根目录
/
,或
/public/
/secured/
等)

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

更改为更合理的默认值,例如
index.xhtml


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

  • /index.xhtml
    的URL模式上映射a,并在
    doFilter()方法内调用。例如

    @WebFilter(“/index.xhtml”)
    公共类IndexFilter实现过滤器{
    @凌驾
    public void doFilter(ServletRequest-req、ServletResponse-res、FilterChain-chain)抛出IOException、ServletException{
    HttpServletResponse=(HttpServletResponse)res;
    sendRedirect(“secured/secure.xhtml”);
    }
    // ...
    }
    
  • /index.xhtml
    中放置一个
    ,它调用一个backingbean方法,该方法反过来执行一个操作。例如

    
    

    @ManagedBean
    @适用范围
    公共类索引bean{
    public void redirect()引发IOException{
    FacesContext.getCurrentInstance().getExternalContext().redirect(“secured/secure.xhtml”);
    }
    }
    

  • 哇,谢谢!但检查用户是否已登录呢?我应该在同一个过滤器中执行此操作吗?可以从容器中取出这个吗?
    已经这样做了。你不需要自己做。唯一的一点是,您需要执行重定向而不是转发。重定向将创建一个全新的请求,该请求将使用新的URL再次触发安全约束。对于转发来说已经太晚了,因为安全约束已经基于
    /
    的初始URL进行了测试,该URL符合您的配置,可以在没有约束的情况下公开访问。我在使用筛选器时遇到了问题。如果我有
    安全约束
    过滤器未被调用,如果我注释掉
    约束
    它将开始工作。可能是因为它们在/secured/*页面上重叠?服务器-glassfish 3.1。如果您有时间,请帮助我。显然您已将筛选器的URL模式更改为与安全约束匹配的模式,或者您已将安全约束更改为与
    /index.xhtml
    匹配。这不是我的初衷。非常感谢,非常有效。但如果你不介意的话,我还有一个问题:是否可以将一些过滤器映射到与约束相同的url模式,并在约束之前调用它,例如,如果我想检查cookie或类似的东西?