Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

283
Visualizações
Can I use the Spring Security @PreAuthorize to inspect the HTTP headers?

I have a REST API written in Spring with Spring Security and Spring Boot with the Web Started pack. Few of my methods inspect a JWT token:

@RequestMapping(value = "/list",
        method = GET,
        produces = APPLICATION_JSON_VALUE)
public HttpEntity<List<Item>> list(@RequestHeader(name = TOKEN_HEADER) String token) {
    // validate token
    // do something
}

Is it possible to move the token inspection to a method that would be invoked through the @PreAuthorize annotation? Something like:

@RequestMapping(value = "/list",
        method = GET,
        produces = APPLICATION_JSON_VALUE)
@PreAuthorize("myMethod(headers)")
public HttpEntity<List<Item>> list(@RequestHeader(name = TOKEN_HEADER) String token) {
    // do something
}

void myMethod(HttpHeaders headers) {
    // validate token
}

The token contains some information about the resources that can be accessed by specific users. If the token doesn't contain the resource that the user requests or the data in underlying data layer indicate the user doesn't have access to it, I am returning HTTP 403.

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You are trying to delegate authentication and authorization to the controller, and is not a good idea.

The way to deal with tokens on headers is to implement a filter that encapsulates the authentication and authorization by token and put in session the user and roles of the user logged.

Then the pre-authorization annotation checks context as you want.

You can see https://jwt.io/ to understand token flow.

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda