Olá pessoal.
Quero validar se um usuário que logou em um app web é o mesmo que consta no registro da tabela do BD.
Quando o usuário faz login no app, injeto (e uso) normalmente os atributos de sessão no meu manageb bean através do CDI.
CDI:
@SessionScoped
public class BeanCDI implements Serializable{
private final String httpSessionChave = (String) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("chave");
@Produces
@SessionChave
public String HttpSessionChave() {
return httpSessionChave ;
}
}
Managed Bean:
@Named(value = "taxiTripsController")
@SessionScoped
public class TaxiTripsController extends AbstractController<TaxiTrips> {
@Inject
@HttpSessionChave
private String httpSessionChave;
private String sessionChave;
public String getSessionChave() {
return sessionChave;
}
@PostConstruct
public void init() {
sessionChave = httpSessionChave;
System.out.print("valor da sessionChave = " + sessionChave); //funciona ok !
}
}
Bean Validation:
public class JustificativaFunciValidator implements ConstraintValidator<JustificativaFunciConstraint, TaxiTrips> {
@Inject
@SessionChave
private String httpSessionChave;
@Override
public void initialize(JustificativaFunciConstraint constraintAnnotation) {
}
@Override
public boolean isValid(TaxiTrips taxiTrips, ConstraintValidatorContext context) {
//se eu atribuir "manualmente" um nome de usuário e logar no app com
//esse mesmo nome de usuário, daí a validação funciona:
//httpSessionChave="user1234"
//mas se eu injeto o nome do usuário aqui, o valor está sendo sempre *null*,
//a validação abaixo retorna sempre *false* e uma Exception é lançada:
System.out.print("valor da httpSessionChave= " + httpSessionChave);
return taxiTrips.getMatricula().getMatricula().equals(httpSessionChave);
}
}
A exception:
javax.validation.ConstraintViolationException: Bean Validation constraint(s) violated while executing Automatic Bean Validation on callback event:'preUpdate'. Please refer to embedded ConstraintViolations for details.
Alguém pode me ajujdar ?