JSF 2, CDI 1.1, SpringSecurity 4: Custom login form does not redirect to page

2

I'm doing an application with JSF 2, SpringSecutiry 4 and CDI 1.1.

I did the entire implementation of SpringSecurity with JSF and apparently everything worked normally, but when doing the custom login form, it redirects to file links in the Head of HTML. If I use the spring login form everything works correctly.

Here are the codes:

@EnableWebSecurity
@Configuration
@ComponentScan(
        basePackages = {
    "br.com.projectus.acompobra.negocio",
    "br.com.projectus.acompobra.dao"})
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Inject
    private UserDetailsService userDetailsService;

    @Inject
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();
        http.authorizeRequests().anyRequest().authenticated().and().logout()
                .logoutSuccessUrl("/faces/login.xhtml?logout")
                .permitAll().and().formLogin()
                .loginPage("/faces/login.xhtml")
                .failureUrl("/faces/login.xhtml?erro").permitAll().defaultSuccessUrl("/faces/login.xhtml");
    }

}

HTML Page:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://xmlns.jcp.org/jsf/core"
      xmlns:c="http://xmlns.jcp.org/jsp/jstl/core"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
    <h:head>
        <h:outputStylesheet library="css" name="login.css"/>
        <title>Projectus - SGO</title>
        <link rel="shortcut icon" href="../resources/images/logo_prj.ico" type="image/x-icon" />
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </h:head>

    <h:body>

        <h:panelGroup rendered="#{param.erro != null}">  
            <p>Dados inválidos.</p>
        </h:panelGroup> 
        <h:panelGroup rendered="#{param.logout != null}">  
            <p>Logout efetuado com sucesso.</p>  
        </h:panelGroup>

        <div id="header-wrapperLogin">
            <div id="headerLogin">
                <div id="logoLogin">

                    <h:form id="form" prependId="false">  
                        <h2>Seja bem vindo. Entre com os dados abaixo para realizar o login.</h2> 
                        <h:panelGrid columns="2">
                            <h:outputLabel for="username" value="Usuário" />  
                            <h:inputText id="username" />   
                            <h:outputLabel for="password" value="Senha" />  
                            <h:inputSecret id="password"  />  
                        </h:panelGrid>  
                        <p:commandButton value="Enviar" /> 
                    </h:form>

                </div>
            </div>
        </div>
    </h:body>

</html>
    
asked by anonymous 23.07.2015 / 15:15

1 answer

1

The configuration order of the configure method (HttpSecurity http) is important, if you put / set all random order settings, problems may occur for Spring Security to recognize.

Even if this is not the situation in this scenario, it is possible to redirect manually, creating a Handler and setting it together with the Spring Security settings .successHandler(new AuthSuccessHandler())

Ex:

public class AuthSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {

@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException, IOException {

    if(isAdmin()) {
        response.sendRedirect("admin");
    }
    response.sendRedirect("usuario"); 
  }
}
    
09.12.2015 / 22:03