Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 106 | espaco | 1 | package br.com.ec.controller.seguranca; |
| 2 | |||
| 3 | import java.util.ArrayList; |
||
| 4 | import java.util.Collection; |
||
| 5 | |||
| 6 | import org.springframework.beans.factory.annotation.Autowired; |
||
| 7 | import org.springframework.security.authentication.AuthenticationProvider; |
||
| 8 | import org.springframework.security.authentication.BadCredentialsException; |
||
| 9 | import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
||
| 10 | import org.springframework.security.core.Authentication; |
||
| 11 | import org.springframework.security.core.AuthenticationException; |
||
| 12 | import org.springframework.security.core.GrantedAuthority; |
||
| 13 | import org.springframework.security.core.authority.GrantedAuthorityImpl; |
||
| 14 | |||
| 15 | import br.com.ec.domain.model.Usuario; |
||
| 16 | import br.com.ec.domain.model.UsuarioPerfil; |
||
| 17 | import br.com.ec.domain.service.usuario.UsuarioService; |
||
| 18 | import br.com.ec.domain.shared.SistemaAuthentication; |
||
| 19 | |||
| 20 | public class SistemaAuthenticationProvider implements AuthenticationProvider { |
||
| 21 | |||
| 22 | @Autowired |
||
| 23 | private UsuarioService usuarioService; |
||
| 24 | |||
| 25 | public boolean supports(Class<? extends Object> authentication) { |
||
| 26 | return UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication); |
||
| 27 | } |
||
| 28 | |||
| 29 | public Authentication authenticate(Authentication authentication) throws AuthenticationException { |
||
| 30 | try { |
||
| 31 | Usuario usuario = new Usuario(); |
||
| 32 | usuario.setLogin((String)authentication.getPrincipal()); |
||
| 33 | usuario.setSenha((String)authentication.getCredentials()); |
||
| 34 | |||
| 35 | usuario = usuarioService.efetuarLogin(usuario); |
||
| 36 | |||
| 37 | if (usuario == null) { |
||
| 38 | throw new BadCredentialsException("Usuário inválido!"); |
||
| 39 | } |
||
| 40 | |||
| 41 | Collection<GrantedAuthority> authorities = obterPermissoes(usuario); |
||
| 42 | |||
| 43 | return criarAuthentication(authentication, usuario, authorities); |
||
| 44 | } catch (Exception e) { |
||
| 45 | throw new BadCredentialsException(e.getMessage()); |
||
| 46 | } |
||
| 47 | } |
||
| 48 | |||
| 49 | private SistemaAuthentication criarAuthentication(Authentication sistemaAuthentication, Usuario usuario, Collection<GrantedAuthority> authorities) { |
||
| 50 | return new SistemaAuthentication ( |
||
| 51 | String.valueOf(sistemaAuthentication.getPrincipal()), |
||
| 52 | String.valueOf(sistemaAuthentication.getCredentials()), |
||
| 53 | usuario, authorities); |
||
| 54 | } |
||
| 55 | |||
| 56 | private Collection<GrantedAuthority> obterPermissoes(Usuario usuario) throws Exception { |
||
| 57 | Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>(); |
||
| 58 | for (UsuarioPerfil perfil : usuario.getPerfis()) { |
||
| 59 | String regra = "ROLE_" + perfil.getSequencial(); |
||
| 60 | authorities.add(new GrantedAuthorityImpl(regra)); |
||
| 61 | } |
||
| 62 | return authorities; |
||
| 63 | } |
||
| 64 | |||
| 65 | } |