【发布时间】:2018-08-06 12:30:19
【问题描述】:
我正在将 Spring Security 4.2.4 与 JSF 应用程序一起使用。
我有以下 facelet 作为登录页面。
<h:form id="formLogin" prependId="false">
<center>
<p:messages closable="true"/>
<p:panelGrid columns="2">
<f:facet name="header">Login</f:facet>
<h:outputLabel for="username" value="Username:" />
<p:inputText id="username" required="true" />
<h:outputLabel for="password" value="Password:" />
<p:password id="password" required="true" feedback="false" />
<f:facet name="footer">
<center>
<p:commandButton value="Login" ajax="false" action="#{loginController.doLogin('login')}"/>
</center>
</f:facet>
</p:panelGrid>
</center>
</h:form>
LoginController bean 中的doLogin 方法是这样定义的:
public String doLogin(String path) throws ServletException, IOException
{
ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
ServletRequest servletRequest = (ServletRequest)context.getRequest();
RequestDispatcher dispatcher = servletRequest.getServletContext().getRequestDispatcher(path.startsWith("/") ? path : "/" + path);
dispatcher.forward(servletRequest, (ServletResponse)context.getResponse());
FacesContext.getCurrentInstance().responseComplete();
return null;
}
如果以这种方式使用 XML 定义安全配置:
<http pattern="/resources/css/**" security="none"/>
<http pattern="/resources/images/**" security="none"/>
<http pattern="/javax.faces.resource/**" security="none"/>
<http>
<intercept-url pattern="/views/login.xhtml" access="isAnonymous()"/>
<intercept-url pattern="/" access="hasRole('USER')"/>
<intercept-url pattern="/**" access="hasRole('USER')"/>
<form-login
login-page="/views/login.xhtml"
login-processing-url="/login"
always-use-default-target="true"
default-target-url="/views/persones.xhtml"/>
<http-basic/>
<csrf disabled="true"/>
<logout invalidate-session="true"
delete-cookies="JSESSIONID,SPRING_SECURITY_REMEMBER_ME_COOKIE"
logout-success-url="/views/login.xhtml">
</logout>
</http>
一切正常,当我按下登录按钮时,登录没有问题。
但如果我使用 Java 定义安全配置:
@Configuration
public class WebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter
{
@Override
protected void configure(HttpSecurity http) throws Exception
{
http
.authorizeRequests()
.antMatchers("/resources/css/**, /resources/images/**").permitAll()
.antMatchers("/javax.faces.resource/**").permitAll()
.antMatchers("/views/login*").permitAll()
.antMatchers("/views/error.xhtml").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/views/login.xhtml")
.loginProcessingUrl("/login")
.defaultSuccessUrl("/views/persones.xhtml", true)
.and()
.httpBasic()
.and()
.logout().logoutSuccessUrl("/views/login.xhtml")
.invalidateHttpSession(true)
.deleteCookies("JSESSIONID", "SPRING_SECURITY_REMEMBER_ME_COOKIE")
.and()
.csrf().disable();
}
}
按下登录按钮后,我以 404 错误结束:myAppContext/login 未找到请求的资源。
【问题讨论】:
-
我今天花了一些时间摆弄这个,我看不出你的方法有什么问题。 FWIW,这是一个使用相同 Java 配置的工作示例。希望它对调试问题有所帮助:github.com/jzheaux/spring-security-examples/blob/master/…
-
我在 GitHub 上打开了这个 issue,其中提到了您的示例。谢谢。
-
嘿,太好了。我查看了这个问题,如果您有一个示例项目可以重现您的情况,因为我发送给您的示例看起来工作正常,这将更有帮助。您是否能够对其进行调整,使其以您的应用程序中断的方式中断?
标签: spring jsf spring-security