Ola eu tenho o seguinte codigo para controlar o acesso ao meu sistema:
@Override
public Authentication authenticate(Authentication a) throws AuthenticationException {
if (!a.getName().isEmpty() && !a.getCredentials().toString().isEmpty()) {
usuario = usuarioDao.login(a.getName(), a.getCredentials().toString());
if (usuario != null) {
if ("Ativo".equals(usuario.getSituacao())) {
return new UsernamePasswordAuthenticationToken(a.getName(), a.getCredentials(), permissao(usuario.getPermissaoList()));
} else {
mensagem = "Ops! Seu usuário bloqueado ou inativo!";
throw new BadCredentialsException(mensagem);
}
} else {
mensagem = "Ops! Suas informações são inválidas!";
throw new BadCredentialsException(mensagem);
}
} else {
mensagem = "Ops! Faltou informar usuário ou senha!";
throw new BadCredentialsException(mensagem);
}
}
teria como eu pegar esse usuario e registrar ele como um HttpServletRequest ou algo semelhante que eu possa utilizar nas paginas em meu projeto por exemplo exibindo o nome do usuario logado.
`vc pode fazer de duas maneiras:
1- colocar o managed bean da classe login com a anotação SessionScoped
ou
após o usuário estar autenticado vc pode coloca-lo no contexto assim:
FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(“usuarioLogado”, usuario);
e recupera-lo assim:
Usuario u = FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("usuarioLogado);
`
lembrando que será necessário um cast para Usuario
Mas neste caso o FacesContext.getCurrentInstance() ele esta me retornando null porque ele nao encontra uma requisicao HTTP
public static UsuarioAutenticado usuarioLogado() {
Usuario usuario = null;
Authentication autenticacao = SecurityContextHolder.getContext().getAuthentication();
@SuppressWarnings("unchecked")
Map<String, UsuarioAutenticado> detalhes = (HashMap<String, UsuarioAutenticado>) autenticacao.getDetails();
if (detalhes != null) {
usuario = detalhes.get("usuario");
}
return usuario;
}
E no AuthProvider tenta algo assim:
public Authentication authenticate(Authentication a) throws AuthenticationException {
if (!a.getName().isEmpty() && !a.getCredentials().toString().isEmpty()) {
usuario = usuarioDao.login(a.getName(), a.getCredentials().toString());
if (usuario != null) {
if ("Ativo".equals(usuario.getSituacao())) {
UsernamePasswordAuthenticationToken autenticacao = new UsernamePasswordAuthenticationToken(a.getName(), a.getCredentials(), permissao(usuario.getPermissaoList()));
Map<String, Usuario> detalhes = new HashMap<String, UsuarioAutenticado>();
detalhes.put("usuario", usuario);
autenticacao.setDetails(detalhes);
return autenticacao;
} else {
mensagem = "Ops! Seu usuário bloqueado ou inativo!";
throw new BadCredentialsException(mensagem);
}
} else {
mensagem = "Ops! Suas informações são inválidas!";
throw new BadCredentialsException(mensagem);
}
} else {
mensagem = "Ops! Faltou informar usuário ou senha!";
throw new BadCredentialsException(mensagem);
}
}
1 curtida
Muito obrigado pelas ajudas, a solução do AndreAlcantara funcionou perfeitamente.