缺少 [java.lang.String] 类型的标头“授权”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6368229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Missing header 'Authorization' of type [java.lang.String]
提问by P Hemans
I have a client app that communicates to the spring server with REST type calls using basic authentication. The controller method signature is below:
我有一个客户端应用程序,它使用基本身份验证通过 REST 类型调用与 spring 服务器通信。控制器方法签名如下:
@RequestMapping(value = "/REST/clients", method = RequestMethod.POST)
protected ModelAndView postClients(@ModelAttribute("ClientsCommand") ClientsCommand cmd,
@RequestHeader("Authorization") String credentials) throws IOException {
...
}
The problem I have is that if no Authorization is present in the header I get an error:
我遇到的问题是,如果标题中没有授权,我会收到一个错误:
Missing header 'Authorization' of type [java.lang.String]]
Whereas, I was kind of hoping the credentials string would just be empty. How do I stop the error from occurring and just receive a blank string if no authorization header entry was sent?
然而,我有点希望凭证字符串是空的。如果没有发送授权标头条目,我如何阻止错误发生并只收到一个空白字符串?
回答by stevevls
Try toggling required=false
on the @RequestHeader
.
尝试required=false
在@RequestHeader
.
@RequestMapping(value = "/REST/clients", method = RequestMethod.POST)
protected ModelAndView postClients(@ModelAttribute("ClientsCommand") ClientsCommand cmd,
@RequestHeader(value = "Authorization", required=false) String credentials) throws IOException {
...
}
When the @RequestHeader
is configured as required, Spring won't map a request to your handler when the header isn't there. By making it optional, your handler will be invoked as you're hoping (i.e. regardless of the header's presence).
当@RequestHeader
根据需要配置时,当标头不存在时,Spring 不会将请求映射到您的处理程序。通过将其设为可选,您的处理程序将按照您的希望被调用(即,无论标头是否存在)。