Difficulty Creating a Session

Asked

Viewed 36 times

0

i’m having a hard time creating a Java Session. I’m creating a registered api called. In short, I have the User Controllers and the Chamadoscontroller. In Callscontroller it needs to receive the id of the user who logged in to the system to be associated with the call. How can I create a session and pass the user id to the Named class?

@Api(tags="Usuarios")
@RestController
@RequestMapping("/usuarios")
public class UsuariosController {

  @Autowired
  private UsuariosRepository usuariosRepository;
  private BCryptPasswordEncoder bCryptPasswordEncoder;
  public UsuariosController(UsuariosRepository usuariosRepository, BCryptPasswordEncoder 
  bCryptPasswordEncoder)
  {
  this.usuariosRepository = usuariosRepository;
  this.bCryptPasswordEncoder = bCryptPasswordEncoder;
  }

  @ApiResponses(value = {
    @ApiResponse(code = 200, message = "Retorna a lista de usuarios"),
    @ApiResponse(code = 403, message = "Você não tem permissão para acessar este recurso"),
  })
  @ApiOperation(value = "", authorizations = { @Authorization(value = "jwtToken") })
  @RequestMapping(method=RequestMethod.GET, value="/cadastrados")
  public @ResponseBody List<Usuarios> listausuarios() {
  return usuariosRepository.findAll();
  }

  @PostMapping("/cadastro")
  public void signUp(@RequestBody Usuarios usuarios, HttpSession session)
  {
    usuarios.setSenha(bCryptPasswordEncoder.encode(usuarios.getSenha()));
    usuariosRepository.save(usuarios);

    session.setAttribute("usuario", new Usuarios().getId());

  }

@Restcontroller @Requestmapping("/called") public class Chamadoscontroller {

@Autowired
private ChamadosService chamadosService;

@ApiOperation(value = "", authorizations = { @Authorization(value = "jwtToken") })
@GetMapping(value = "/{chamadoId}")
public ResponseEntity<Chamados> findById(@PathVariable Long chamadoId) {
    Chamados obj = chamadosService.findById(chamadoId);
    return ResponseEntity.ok().body(obj);
}

@ApiOperation(value = "", authorizations = { @Authorization(value = "jwtToken") })
@PostMapping("/{userId}/{problemId}")
public ResponseEntity<Chamados> cadastrarChamado(@PathVariable("userId") Long userId, @PathVariable("problemId") Long problemId,
        @RequestParam("file") MultipartFile file, @RequestParam("descricaoProblema") String descricaoProblema,
        @RequestParam("dataCriacao") String dataCriacao) {
    
    Chamados obj = chamadosService.salvarDados(userId, problemId, file, descricaoProblema, dataCriacao);
    
    URI uri = ServletUriComponentsBuilder.fromCurrentRequest().path("/{chamadoId}")
            .buildAndExpand(obj.getId()).toUri();
    return ResponseEntity.created(uri).build();
}

@ApiOperation(value = "", authorizations = { @Authorization(value = "jwtToken") })
@GetMapping
public ResponseEntity<List<Chamados>> ListaDeChamados() {
    List<Chamados> list = chamadosService.lista();
    return ResponseEntity.ok().body(list);
}

@ApiOperation(value = "", authorizations = { @Authorization(value = "jwtToken") })
@GetMapping("/baixarAnexo/{chamadoId}")
public ResponseEntity<ByteArrayResource> downloadFile(@PathVariable Long chamadoId){
    Chamados chamado = chamadosService.getFile(chamadoId).get();
    return ResponseEntity.ok()
            .contentType(MediaType.parseMediaType(chamado.getTipoAnexo()))
            .header(HttpHeaders.CONTENT_DISPOSITION,"attachment:filename=\""+chamado.getNomeAnexo()+"\"")
            .body(new ByteArrayResource(chamado.getAnexo()));
}

}

  • To get the user authenticated with Spring Security you can use the Securitycontextholder class. See if the following article helps you: https://www.baeldung.com/get-user-in-spring-security

No answers

Browser other questions tagged

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