Eu ja testei com um projeto a parte com Spring MVC e deu certo. Agora to tentando configurar o security com um projeto em JSF 2.2.
Segue arquivos de configuração e a página de login.
public class WebInitializer implements WebApplicationInitializer {
public void onStartup(ServletContext container) {
WebApplicationContext context = getContext();
Scope viewScoped = new ViewScoped();
// Create the 'root' Spring application context
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.register(SecurityConfiguration.class);
rootContext.register(JPAConfiguration.class);
rootContext.register(SpringConfig.class);
rootContext.register(EnumList.class);
rootContext.setServletContext(container);
//Adiciona o ViewScoped do JSF ao Spring
rootContext.refresh();
rootContext.getBeanFactory().registerScope("view", viewScoped);
registerListener(container, rootContext);
registerOpenEntityManagerInViewFilter(container);
// Configure facelets to use xhtml instead of jsp extension
container.setInitParameter("javax.faces.DEFAULT_SUFFIX", ".xhtml");
registerDispatcherServlet(container, context);
}
private void registerDispatcherServlet(ServletContext container, WebApplicationContext context) {
ServletRegistration.Dynamic dispatcher = container.addServlet("DispatcherServlet", new DispatcherServlet(context));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("");
}
private void registerListener(ServletContext container, AnnotationConfigWebApplicationContext rootContext) {
container.addListener((new ContextLoaderListener(rootContext)));
container.addListener(new RequestContextListener());
}
private void registerOpenEntityManagerInViewFilter(ServletContext container) {
container.addFilter("Spring OpenEntityManagerInViewFilter", OpenEntityManagerInViewFilter.class)
.addMappingForUrlPatterns(null, false, "/*");
}
private AnnotationConfigWebApplicationContext getContext() {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
return context;
}
}
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
private UsuarioServiceImpl usuarioService;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/**").hasRole("ROLE_ADMIN")
.anyRequest().authenticated()
.and().formLogin().loginPage("/login").loginProcessingUrl("/login").failureUrl("/login").isCustomLoginPage();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(usuarioService);
}
// Libera acesso às pastas resources para a aplicação
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/javax.faces.resource/**");
web.ignoring().antMatchers("/resources/**");
web.ignoring().antMatchers("/template/**");
web.ignoring().antMatchers("/resources/images/favicon.icon");
web.ignoring().antMatchers("/login");
}
<form action="#{request.contextPath}/j_spring_security_check" method="post">
<h:inputText id="j_username" pt:placeholder="Username" styleClass="form-control" required="true"/>
<h:inputSecret styleClass="form-control" pt:placeholder="Password" id="j_password" required="true"/>
<h:commandButton styleClass="btn btn-lg btn-primary btn-block"
value="Login"/>
</form>
Quando clico no login, aparece o seguinte erro:
HTTP Status 403 - Invalid CSRF Token 'null' was found on the request parameter '_csrf' or header 'X-CSRF-TOKEN'.
E to querendo evitar botar o input type="hidden" com o csrf_param.
Alguem ja passou por esse problema?
Desde ja, valeu.