【问题标题】:Authentication(Spring Security) object is null when URL is not mapped by PrettyFaces当 PrettyFaces 未映射 URL 时,Authentication(Spring Security) 对象为空
【发布时间】:2013-05-24 03:06:22
【问题描述】:
  1. 我在我的应用程序中使用 JSF(Primefaces)、Spring、Spring Security、MyBatis 和 PrettyFaces。
  2. 我有一个未被 PrettyFaces 映射的 URL。
  3. 此 URL 重定向到 XHTML 页面。 XHTML 页面使用 Spring Security 进行映射,并与我的应用程序的角色相关联
  4. 在 XHTML 页面中,我调用了一个 Authentication 对象。此对象存储用户已登录 在我的应用程序中。
  5. 当我输入 url 时出现异常,因为 Authentication 对象为 null
  6. 但是当我映射 url 时,Authentication 对象返回值存储了用户。

发生了什么事?

这是我的 webapp 的日志:

40094 [http-8080-6] TRACE com.ocpsoft.pretty.PrettyContext  - Initialized PrettyContext
 40094 [http-8080-6] TRACE com.ocpsoft.pretty.PrettyContext  - Initialized PrettyContext
 40094 [http-8080-6] TRACE com.ocpsoft.pretty.PrettyFilter  - Request is not mapped using PrettyFaces. Continue.
 40094 [http-8080-6] TRACE com.ocpsoft.pretty.PrettyContext  - Initialized PrettyContext
 40094 [http-8080-6] TRACE com.ocpsoft.pretty.PrettyContext  - PrettyContext not found in Request - building new instance
 40094 [http-8080-6] DEBUG org.springframework.faces.support.RequestLoggingPhaseListener  - Entering JSF Phase: RESTORE_VIEW 1
 40141 [http-8080-6] TRACE com.ocpsoft.pretty.PrettyContext  - Retrieved PrettyContext from Request
 40141 [http-8080-6] TRACE com.ocpsoft.pretty.faces.beans.ParameterInjector  - Validating parameters.
 40141 [http-8080-6] TRACE com.ocpsoft.pretty.PrettyContext  - Retrieved PrettyContext from Request
 40141 [http-8080-6] TRACE com.ocpsoft.pretty.faces.beans.ParameterInjector  - Injecting parameters
 40141 [http-8080-6] TRACE com.ocpsoft.pretty.PrettyContext  - Retrieved PrettyContext from Request
 40141 [http-8080-6] TRACE com.ocpsoft.pretty.PrettyContext  - Retrieved PrettyContext from Request
 40141 [http-8080-6] INFO  com.indra.siguip.view.exceptions.SiguipExceptionHandler  - Entrando al manejador de Excepciones del JSF
 40141 [http-8080-6] TRACE com.ocpsoft.pretty.PrettyContext  - Retrieved PrettyContext from Request
 40141 [http-8080-6] DEBUG org.springframework.faces.support.RequestLoggingPhaseListener  - Entering JSF Phase: APPLY_REQUEST_VALUES 2
 40141 [http-8080-6] INFO  com.indra.siguip.view.exceptions.SiguipExceptionHandler  - Entrando al manejador de Excepciones del JSF
 40141 [http-8080-6] TRACE com.ocpsoft.pretty.PrettyContext  - Retrieved PrettyContext from Request
 40141 [http-8080-6] DEBUG org.springframework.faces.support.RequestLoggingPhaseListener  - Entering JSF Phase: PROCESS_VALIDATIONS 3
 40141 [http-8080-6] TRACE org.springframework.web.jsf.el.SpringBeanFacesELResolver  - Successfully resolved variable 'applicationConfig' in Spring BeanFactory
 40141 [http-8080-6] TRACE org.springframework.web.jsf.el.SpringBeanFacesELResolver  - Successfully resolved variable 'menuGeneratorService' in Spring BeanFactory
 28/05/2013 07:54:00 PM com.sun.faces.lifecycle.ProcessValidationsPhase execute
ADVERTENCIA: Se ha producido un error al realizar la inyección de recurso en el bean administrado menuController
com.sun.faces.mgbean.ManagedBeanCreationException: Se ha producido un error al realizar la inyección de recurso en el bean administrado menuController
    at com.sun.faces.mgbean.BeanBuilder.invokePostConstruct(BeanBuilder.java:229)

这是我对 Spring Securiy 和 PretyFaces 的配置

<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>FORWARD</dispatcher>
  </filter-mapping>
  <filter>
    <filter-name>Pretty Filter</filter-name>
    <filter-class>com.ocpsoft.pretty.PrettyFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>Pretty Filter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>ERROR</dispatcher>
  </filter-mapping>

PD:我已经把话题改得更明确了

【问题讨论】:

    标签: jsf-2 spring-security prettyfaces


    【解决方案1】:

    不,您绝对不必在使用 PrettyFaces 时映射每个 URL。如果传入的请求不匹配任何映射,它会简单地转发到应用程序,而 PrettyFaces 不会对其进行任何处理。

    所以您的异常是由其他原因引起的。如果您发布带有未本地化错误消息的完整堆栈跟踪,我可以查看它。

    【讨论】:

    • springSecurityFilterChain 被配置为仅应用于转发请求(如 PrettyFaces 创建的请求)。您应该将所有 REQUEST 都发送到过滤器,以便它也被正常请求调用。
    【解决方案2】:

    您只配置了 spring-security 过滤器来处理 FORWARD 请求,它还需要处理其他类型:

    <filter>
      <filter-name>springSecurityFilterChain</filter-name>
      <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
      <filter-name>springSecurityFilterChain</filter-name>
      <url-pattern>/*</url-pattern>
      <dispatcher>FORWARD</dispatcher>
      <dispatcher>REQUEST</dispatcher>
      <dispatcher>INCLUDE</dispatcher>
      <dispatcher>ERROR</dispatcher>
    </filter-mapping>
    

    PrettyFaces 如果请求没有被框架处理,则不会转发请求,因此,调度类型保持为 REQUEST,并且 Spring Security 过滤器永远不会执行。

    【讨论】:

      猜你喜欢
      • 2017-11-21
      • 2016-02-16
      • 2013-05-03
      • 1970-01-01
      • 2012-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多