我正在尝试将Spring-Security 5.1.4.RELEASE集成到已经运行的JSF 2.2-Primefaces 6.1应用程序中,以便将其安全化。当我尝试访问受保护的页面"logged.xhtml"时,spring会触发并将我带到登录页面"login.xhtml",因此Spring似乎工作正常。
问题是,一旦我配置了Spring,所有Primefacesp:commandLink
停止工作(以及其他Primefaces组件中的一些"Action"方法)。JSF Sun 组件 (xmlns:h="http://java.sun.com/jsf/html" ) 如 "h:outputLink" 继续工作,但带有f:ajax
的h:commmandButton
也失败了。
我不明白为什么Primefaces组件或带有f:ajax
的JSF组件被破坏了......
这是我的脸配置.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 Forbidden 2ms]
我相信权限有问题,但在查看我的 SecurityConfig 文件后,我没有看到问题。
以下行应限制对受保护页面的访问:
.antMatchers("/logged.xhtml").authenticated()
这一行应该允许其余页面中的所有trafic:
.anyRequest().permitAll()
我做错了什么?
有什么建议吗?
提前感谢!
PS:让我知道您是否需要有关该项目的更多信息
我想回答这个问题,以防其他人需要它:
当使用模板来撰写JSF页面时,总是将"csrf"标记放在其上的每个表单中。将令牌放在一个地方是不够的。
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />