【问题标题】:PrimeFaces components and plain JSF components with f:ajax stopped woking with Spring-SecurityPrimeFaces 组件和带有 f:ajax 的普通 JSF 组件停止使用 Spring-Security
【发布时间】:2019-11-21 14:15:27
【问题描述】:

我正在尝试将 Spring-Security 5.1.4.RELEASE 集成到已经运行的 JSF 2.2-Primefaces 6.1 APP 中保护它。 当我尝试访问受保护页面“logged.xhtml”时,spring 触发并带我进入登录页面“login.xhtml”,所以 Spring 似乎工作正常。

问题是,一旦我配置了 Spring,所有 Primefaces p:commandLink 都会停止工作(以及其他 Primefaces 组件中的一些“Action”方法)。 JSF Sun 组件 ( xmlns:h="http://java.sun.com/jsf/html" ) 像 "h:outputLink" 继续工作,但 h:commmandButtonf:ajax 失败也是。

我不明白为什么 Primefaces 组件或带有 f:ajax 的 JSF 组件坏了...

这是我的 faces-config.xml:

<application>
    <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>

    <resource-bundle>
        <base-name>messages</base-name>
        <var>msg</var>
    </resource-bundle>

    <message-bundle>messages</message-bundle>

    <locale-config>
        <default-locale>en</default-locale>
        <supported-locale>en</supported-locale>
        <supported-locale>es</supported-locale>
    </locale-config>
</application>

这是我的 WEB.XML:

<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>
    <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>client</param-value>
</context-param>

<context-param>
    <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
    <param-value>resources.application</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
    <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
    <param-value>.xhtml</param-value>
</context-param>

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

<session-config>
    <session-timeout>30</session-timeout>
</session-config>

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

这是我的安全初始化程序:

public class SecurityWebInitializer extends AbstractSecurityWebApplicationInitializer{

}

这是我的安全配置:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {

    auth.inMemoryAuthentication().withUser(User.withDefaultPasswordEncoder().username("admin").password("1234").roles("ADMIN").build());
    auth.inMemoryAuthentication().withUser(User.withDefaultPasswordEncoder().username("usu").password("1234").roles("NORMAL").build());
}

@Override
protected void configure(HttpSecurity http) throws Exception {

    http
    .authorizeRequests()
        .antMatchers("/logged.xhtml").authenticated()
        .anyRequest().permitAll()
        .and()
    .formLogin()
        .loginPage("/login.xhtml").defaultSuccessUrl("/logged.xhtml").failureUrl("/error.xhtml")
        .permitAll()
        .and()
    .logout().logoutUrl("/logout")
        .permitAll(); 

}

}

编辑:

检查浏览器控制台后,我发现每次按下任何 Primefaces 链接/按钮时都会出现以下错误:

XHR POST localhost:8080/springtest/index.xhtml [HTTP/1.1 403 禁止 2ms]

我认为权限存在问题,但在查看了我的 SecurityConfig 文件后,我没有发现问题。

以下行应限制对受保护页面的访问:

.antMatchers("/logged.xhtml").authenticated()

并且这一行应该允许其余页面中的所有流量:

.anyRequest().permitAll()

我做错了什么?

有什么建议吗?

提前致谢!

PS:如果您需要有关该项目的更多信息,请告诉我

【问题讨论】:

  • 检查浏览器控制台?和浏览器网络标签?为什么不?尝试使用搜索引擎?你发现了什么?有帮助吗?见How to Ask
  • 您没有注意到 PrimeFaces 组件失去了外观和感觉?
  • 嗨!我在谷歌上搜索了很多,但是 JSF-Spring 集成主题给出了很多糟糕的结果,与发生在我身上的事情没有什么相似之处。我现在不在电脑前,但我稍后会检查 javascript 控制台。我相信 Jquery/javascript 已经停止工作,因为 h:outputLink 和 p:commandlink 之间的主要区别是
  • Primefaces 方面没有失败,只是有一些组件功能......
  • 而且我几乎 100% 确定 css 也应该失败(除非您为 css 而不是 javascript 使用 CDN 解决方案。如果网络选项卡中没有 40x 错误,检查 javascript 和 css 的 html 中的源。脚本或 css 的 URL 应该在哪里?

标签: jsf spring-security primefaces


【解决方案1】:

我想回答这个问题以防其他人需要它:

当使用 TEMPLATES 编写 JSF 页面时总是将“csrf”标记放在每个表单中。将令牌放在一个地方是不够的。

<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />

【讨论】:

  • jsf 已经内置了 csrf。所以在 spring security 中不需要它(你可以在那里禁用它吗?)你为什么要删除 jsf 和 primefaces 标签?
  • 我没有删除任何 primefaces 标签。我使用的大多数页面都由 3 或 4 个模板组成,最初,我只是在主模板中添加了 spring“csrf”标记。问题是其余的 os 模板也有表单和 primefaces 组件,在使用时,它们会通过这些表单发送 POST 请求。我希望我自己解释一下。
  • 但是会影响 jsf 的行为,所以我会说 jsf/Primefaces 标签是合理的
  • 我刚刚又添加了标签。顺便问一下,您能告诉我如何使用 JSF 原生指定令牌吗?
  • 你不需要stackoverflow.com/questions/7722159/…。与所有大肆宣传的客户端 js 框架相反,这些框架应该是“容易”使用的,你需要添加各种类似 owasp 的东西才能变得安全...... JSF 有这个内置的
猜你喜欢
  • 1970-01-01
  • 2013-08-22
  • 1970-01-01
  • 2013-04-25
  • 2016-09-27
  • 1970-01-01
  • 2023-04-04
  • 2014-02-21
  • 2013-04-03
相关资源
最近更新 更多