我正尝试一个JSF Web应用程序与春 安全整合。集成一个JSF的登录页面使用Spring Security
目前我通过一种方法登录:在此 方法内进行身份验证并根据用户重定向到目标页面。
登录页面(login.xhtml):
<h:form id="login">
<h:outputLabel for="email" value="E-mail: "/>
<p:inputText id="email" value="#{loginManagedBean.usuario.email}" required="true"/>
<p:message for="email"/>
<h:outputLabel for="pass" value="Contraseña: "/>
<p:password id="pass" value="#{loginManagedBean.usuario.password}" required="true"/>
<p:message for="pass"/>
<!-- <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/> -->
<p:commandButton value="Login" update="@form" action="#{loginManagedBean.autenticar()}"/>
</h:form>
loginManagedBean.autenticar()(方法进行身份验证和重定向):
我怎么能取代这个页面和方法使用SpringSecurity?
SpringSecurityConfig:
@Override
protected void configure(HttpSecurity http) throws Exception {
//.csrf() is optional, enabled by default, if using WebSecurityConfigurerAdapter constructor
// Have to disable it for POST methods:
// http://stackoverflow.com/a/20608149/1199132
http.csrf().disable();
// Logout and redirection:
// http://stackoverflow.com/a/24987207/1199132
http
.logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.deleteCookies("JSESSIONID")
.invalidateHttpSession(true)
.logoutSuccessUrl("/login.xhtml");
http
.authorizeRequests()
//Permit access for all to error and denied views
.antMatchers("/WEB-INF/errorpages/general.xhtml", "/WEB-INF/errorpages/accessDenied.xhtml", "/WEB-INF/errorpages/expired.html", "/login.xhtml")
.permitAll()
// Only access with admin role
.antMatchers("/admin/**")
.hasRole("ADMIN")
//Permit access only for some roles
.antMatchers("/alumno/**")
.hasRole("ALUMNO")
//Permit access only for some roles
.antMatchers("/profesor/**")
.hasRole("PROFESOR")
//If user doesn't have permission, forward him to login page
.and()
.formLogin()
.loginPage("/login.xhtml")
.usernameParameter("login:email")
.passwordParameter("login:pass")
.loginProcessingUrl("/login") //
.defaultSuccessUrl("/admin/homeAdmin.xhtml")
.and()
.exceptionHandling()
.accessDeniedPage("/WEB-INF/errorpages/accessDenied.xhtml");
}
请不要把你所有的代码放在这里。只需将导致出现问题的代码或您不明白的部分代码放入即可。否则,它太无聊了。 –
Spring安全性可通过URL进行配置。除此之外,它还提供了一个身份验证端点,因此您可以对该身份验证端点执行POST(使用纯文本格式而不是JSF格式),一旦登录,导航至您的应用程序url。 –