How to make a Spring Security, JSF 2.2 and ManagedBean login?

1

Hello. I need help. I'm configuring Spring Security in a JSF 2.2 Project. Using Annotation. I've already set up the filters. But I do not understand how to configure the ManagedBean of the login to search the database and log in to JSF. Below is my Spring Security configuration class

@Configuration

@EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    auth.inMemoryAuthentication().withUser("zezinho").password("123456").roles(Constantes.PERMISSAO_USER);
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable();

    http.authorizeRequests()
            .antMatchers(Constantes.RESOURCES).permitAll()
            .antMatchers(Constantes.PUBLIC + Constantes.URL_ALL).permitAll()
            .antMatchers(Constantes.PRIVATE).hasRole(Constantes.PERMISSAO_USER).anyRequest().authenticated()
            .and()
            .formLogin()
            .loginPage(Constantes.PAGE_PUBLIC_URL_LOGIN)
            .successForwardUrl(Constantes.PAGE_PRIVATE_URL_PRINCIPAL)
            .permitAll();
}

@Override
public void configure(WebSecurity web) throws Exception {

// web.ignoring (). antMatchers ("/ resources / **") ;;     }

}

Important Remember I use Hibernate and a User Table. Thus. I want to query the user on the ManagedBean. and then start the session if it exists. Can anyone help me?

    
asked by anonymous 03.04.2017 / 15:23

1 answer

0

I had to implement this solution a while ago, based on this link: link

You need to configure the AuthenticationManagerBuilder, eg:

@Autowired
public void configureAuthentication(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
        authenticationManagerBuilder
                .userDetailsService(this.userDetailsService)
                // configura um encoder para a senha armazenada no banco
                .passwordEncoder(passwordEncoder());
}

And a @Service that implements UserDetailsService :

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

@Autowired
private UsuarioRepository userRepository;

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    UsuarioEntity user = userRepository.findByNomeUsuario(username);
    //...
    }
}

Abcs!

    
04.04.2017 / 04:37