java - 如何在 RestController 中获取标头值并将其传递给父控制器
问题描述
我是 spring-boot 的新手,对 web 服务也很陌生。
我想在(抽象)通用 RestController 中实现一个授权过程,它应该获取基本身份验证的内容。来自扩展控制器的标头,然后执行授权。
像这样的东西:
public class GenericController
{
// Constructor
protected GenericController(String basicAuth) throws MalformedURLException, ProtocolException, IOException
{
// check user can execute action
}
}
@RestController
@RequestMapping( "/users" )
public class UserController extends GenericController
{
private static final Logger logger = LoggerFactory.getLogger(UserController.class);
// Constructor
protected UserController() throws MalformedURLException, ProtocolException, IOException
{
// somehow get the basic auth information from header and pass it to the parent's constructor
basicAuth = ???
super(basicAuth);
}
@GetMapping( "/user/{cn}" )
public String getUser( @PathVariable String cn )
{
logger.error("Start getUser(...): cn=" + cn);
}
我怎样才能做到这一点?(这甚至可能吗?)
附加信息:我的 Web 服务本身就是其他 Web 服务的消费者。
一旦主叫用户被授权“使用”我的 Web 服务,我必须将基本身份验证转发/设置到我调用的 Web 服务的请求中。
那么,当我的服务被调用时,我如何/在哪里“拦截”并获取标头?
解决方案
您可以使用 HttpServletRequest 从请求中获取标头。
protected UserController(HttpServletRequest httpServletRequest) throws MalformedURLException, ProtocolException, IOException
{
System.out.println( httpServletRequest.getHeaders("key name"));
// somehow get the basic auth information from header and pass it to the parent's constructor
basicAuth = ???
super(basicAuth);
}
推荐阅读
- javascript - Html隐藏按钮隐藏所有按钮而不是一个
- arrays - 如何比较和拒绝不同维度的数组项?
- php - PHP删除多行并将值返回到另一个表
- python - 按另一个列表对嵌套列表进行排序
- java - 如何将 Eclipse 插件 jar 放入 Eclipse 插件目录并开始在 Eclipse RCP 中运行
- sql - 基于多列的 Oracle Pivot
- speech-to-text - 在 google-cloud-speech 中识别 .wav 音频文件的问题
- vba - 摆脱不需要的分页符
- c# - 负载平衡系统中的 Protobuf-net 动态类型
- linkedin - LinkedIn关注按钮插件正在打开一个空白窗口