Home > Back-end >  Spring security get authorization header value
Spring security get authorization header value

Time:04-14

Currently I have a RestController ProductsController that takes a basic Authorization string.

We have some external security library defined that can not be changed. The security library has the security config defined and reads the Auth and translates it to a principal without the password or the original string. the principal just have the username present.

I need to extract the auth that was passed in the header without changing the sec lib, Can you please let me know how can i do that?? we use spring boot and spring security with java 11.

CodePudding user response:

To read all http header in your Spring Boot application, we use the same @RequestHeader annotation. To collect all the header values, we have the option to collect values in to Map, a MultiValueMap or a HttpHeaders object. Let’s see how to do this:


@RestController
public class AllHeaderController {

    @GetMapping("/print-all-headers")
    public void getAllheaders(@RequestHeader Map<String,String> headers){
        headers.forEach((key,value) ->{
            System.out.println("Header Name: " key " Header Value: " value);
        });
    }
}

CodePudding user response:

For now i just reader the auth from the header.. We have authentication, so would have been nice to use that.

Need to do this as the restcontroller than calls another rest api via webclient and the same credentials needs to be passed there as well.

  • Related