【发布时间】: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:commmandButton 和 f: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