Problema: Spring Security "j_spring_security_check"

153 views
Skip to first unread message

Davi Mustafa

unread,
May 11, 2016, 2:15:32 PM5/11/16
to javasf: JavaServer Faces Group
Galera,

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");
}
}

Pagina de login.xhtml

 <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.

                          



João Miguel

unread,
May 19, 2016, 8:37:33 AM5/19/16
to javasf: JavaServer Faces Group

--
Você recebeu essa mensagem porque está inscrito no grupo "javasf: JavaServer Faces Group" dos Grupos do Google.
Para cancelar inscrição nesse grupo e parar de receber e-mails dele, envie um e-mail para javasf+un...@googlegroups.com.
Acesse esse grupo em https://groups.google.com/group/javasf.
Para ver essa discussão na Web, acesse https://groups.google.com/d/msgid/javasf/20d05f2a-79f2-4a09-a0c4-08d5ec12c51b%40googlegroups.com.
Para mais opções, acesse https://groups.google.com/d/optout.

Davi Mustafa

unread,
May 19, 2016, 8:49:46 AM5/19/16
to javasf: JavaServer Faces Group
Valeu João. Tinha dado uma olhada aqui nisso tb. Por enquanto eu desativei o csrf no login. No sistema aqui a gente usa uma tela de login que vem como depêndencia de outro sistema. Ainda não tenho como fazer modificações na tela pra por o input hidden com o parametro por exemplo. 


Para mais opções, acesse https://groups.google.com/d/optout.



--
Davi Mustafa

João Miguel

unread,
May 19, 2016, 9:37:26 AM5/19/16
to javasf: JavaServer Faces Group
Qualquer coisa estamos por aqui no grupo.

Reply all
Reply to author
Forward
0 new messages