Get a return of a method in another Java class

Asked

Viewed 469 times

0

I have a method in my Spring boot API that receives the data from the user who is logging in, checks if it exists and if the password checks. Once this is done, return returns a JWT token. I need to catch this return of the token in another Java class. I am unable to do.

My method that receives the user and generates the token.

@RequestMapping(value = "/autenticar", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, method = RequestMethod.POST)
public LoginResponse autenticar(@RequestBody Usuarios usuarios)
        throws ServletException {

    // verifica se foram digitados o login e senha no front end
    if (usuarios.getLogin() == null
            || usuarios.getSenha() == null) {
        throw new  ServletException("Nome ou senha obrigatório");

    }

    // busca no banco de dados
    Usuarios usuariosAutenticado = uService.buscarPorLogin(usuarios.getLogin());

    if (usuariosAutenticado == null) {
        return new LoginResponse("naoEncontrado");
    }       

    // compara a senha vinda do banco de dados com a senha vinda da tela
    if (!usuariosAutenticado.getSenha()
            .equals(usuarios.getSenha())) {
        return new LoginResponse("senhaInvalida");
    }

    String token = Jwts.builder().setSubject(usuariosAutenticado.getLogin())
            .signWith(SignatureAlgorithm.HS512, "xxx")
            .setExpiration(new Date(System.currentTimeMillis() + 60 * 60 * 1000)).compact();            

    return new LoginResponse(token);
}

i need to receive the token Return within that method

public void newRevision(Object revisionEntity) {
    AuditEntity revEntity = (AuditEntity) revisionEntity;

    //aqui e preciso do token
}

There’s a way I can do that ???

  • You could not add a parameter (String token) for this method, keep a reference to an instance of the class of that method in your @Controller and evoke him within the LoginResponse autenticar(@RequestBody Usuarios usuarios)?

  • 2

    It would not be easier for you to provide the class with the method that generates the token in the Spring class pool (@Component, @Service or @Repository) and get the class and invoke the method where you need it?

  • Good afternoon, all right? I believe that the easiest way to solve your problem would be to assign this return value to an attribute of the method class, and then with the construction of a GET you could get this value.

  • In the class that has the method autenticar, what are the annotations you have put? The method newRevision(Object) is in this same class?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.