【问题标题】:after web filter all images gone in jsf page网络过滤后,jsf页面中的所有图像都消失了
【发布时间】:2011-09-24 05:34:43
【问题描述】:

我正在使用带有 jsf 2 的 spring security。我有一个过滤器来控制每个页面中的 db 访问是否正常。 :

public void doFilter(ServletRequest aReq, ServletResponse aResponse, FilterChain aChain) throws IOException,
        ServletException
{

    ...
    if(!myContext.isdbRunning())
    {
        mLogger.debug("System not working. Redirecting to: "+"/error.jsf");
        aReq.setAttribute("errorMsj", "DB is not started. Please contact DB admin.");
        aReq.getRequestDispatcher("/error.jsf").forward(aReq, aResponse);
        return;
    }

    aChain.doFilter(aReq, aResponse);
    return; 
}

如果一切正常,我的 jsf 页面就会正确呈现。但是当过滤器在 db 中发现问题时,它会处理到错误页面。

aReq.getRequestDispatcher("/error.jsf").forward(aReq, aResponse);

但该页面不显示图像和其他基于 css 的东西..

spring security 是否会控制并禁止我的页面内容?还是我有错误?我该如何解决?我可以使用 Phase 监听器吗?

编辑:我的 web.xml 的一部分是

  <filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  </filter>

  <filter>
    <filter-name>Gatekeeper</filter-name>
    <filter-class>com.jsfsample.filter.GateKeeperFilter</filter-class>
  </filter>

   <filter-mapping>
    <filter-name>Gatekeeper</filter-name>
    <url-pattern>*.jsf</url-pattern>
   </filter-mapping>


  <filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

【问题讨论】:

  • 如何将图片排除在过滤器的处理范围之外?
  • 请发布您的安全上下文配置。
  • 嗨,感谢您的回复,我编辑并添加了我的 web.xml,希望我没有误解您。另外,有没有可能是因为路径问题而找不到网页内容?
  • 感谢您的更新,但我需要您的 applicationSecurity.xml 或者您已将其命名。定义 元素、 等的文件。

标签: jsf-2 spring-security servlet-filters


【解决方案1】:

您的 css/scripts/images 是否通过单独的请求加载?

如果是这样,请确保他们的网址 (http://domain.com/styles.css) 不安全。


关于取消保护特定 URL 的更多详细信息。

在您的安全上下文配置文件中,您应该有如下内容:

<http auto-config="false" use-expressions="true" entry-point-ref="authenticationEntryPoint">
    <intercept-url pattern="/something/relativeUrlThatLoadsImages.jsf" filters="none" />

    <!-- OR -->    

    <intercept-url pattern="/something/relativeUrlThatLoadsImages.jsf" access="IS_AUTHENTICATED_ANONYMOUSLY" />
</http>

filters="none"access="IS_AUTHENTICATED_ANONYMOUSLY" 将取消在 pattern 属性中指定的相对 URL 的安全性。

我个人更喜欢使用filters="none",因为它告诉 spring 根本不要为这些 URL 加载过滤器链。

这样你就不需要编写代码来让 spring 忽略这些 URL,如果你需要的话,你将来可以很容易地更改对它们的访问。

【讨论】:

  • 嗨 Simeon,它们加载了单独的请求,并且模式也以 .jsf 扩展名结尾。(如:xx.css.jsf)
  • @asyard 在这种情况下,请确保请求的 url 不受 Spring Security 的保护,如果是,这很可能是问题所在。
  • 西蒙,谢谢。我修改了我的过滤器,如果 request.getservletcontext.contains(".css.jsf") 或 ("jpg.jsf") 然后我将它链接起来,否则我转发到错误页面:解决了我的问题。是个坏主意吗?如果是这样,你能告诉我更多细节吗?为什么 css 和 jpg 文件以 .jsf 结尾?
  • 好吧,唯一让我担心的事情是,如果您将来尝试从配置中保护这些 URL,您将无法通过代码保护这些 URL,但如果您不是担心这个没关系,我会更详细地编辑我的答案
  • 你说得对,Simeon,我会考虑你的建议,谢谢你的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-25
相关资源
最近更新 更多