f:ajax 的 PrimeFaces 组件和纯 JSF 组件停止使用 Spring-Security
PrimeFaces components and plain JSF components with f:ajax stopped woking with Spring-Security
我正在尝试将 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:commmandButton
和 f:ajax
也失败了。
我不明白为什么 Primefaces 组件或带有 f:ajax
的 JSF 组件损坏了...
这是我的脸-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 link/button 都会出现以下错误:
XHR POST localhost:8080/springtest/index.xhtml [HTTP/1.1 403 禁止 2ms]
我认为权限有问题,但在查看我的 SecurityConfig 文件后我没有发现问题。
以下行应限制对受保护页面的访问:
.antMatchers("/logged.xhtml").authenticated()
并且此行应允许其余页面中的所有流量:
.anyRequest().permitAll()
我做错了什么?
有什么建议吗?
提前致谢!
PS:让我知道您是否需要有关该项目的更多信息
我想回答这个问题以防其他人需要:
使用 TEMPLATES 编写 JSF 页面时 始终 将 "csrf" 标记放在其上的每个表单中。将令牌放在一个地方是不够的。
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
我正在尝试将 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:commmandButton
和 f:ajax
也失败了。
我不明白为什么 Primefaces 组件或带有 f:ajax
的 JSF 组件损坏了...
这是我的脸-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 link/button 都会出现以下错误:
XHR POST localhost:8080/springtest/index.xhtml [HTTP/1.1 403 禁止 2ms]
我认为权限有问题,但在查看我的 SecurityConfig 文件后我没有发现问题。
以下行应限制对受保护页面的访问:
.antMatchers("/logged.xhtml").authenticated()
并且此行应允许其余页面中的所有流量:
.anyRequest().permitAll()
我做错了什么?
有什么建议吗?
提前致谢!
PS:让我知道您是否需要有关该项目的更多信息
我想回答这个问题以防其他人需要:
使用 TEMPLATES 编写 JSF 页面时 始终 将 "csrf" 标记放在其上的每个表单中。将令牌放在一个地方是不够的。
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />