简体   繁体   中英

Spring security get authorization header value

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.

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);
        });
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM