【发布时间】:2020-09-21 14:55:21
【问题描述】:
我是 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);
}
我该怎么做? (这可能吗?)
补充信息: 我的网络服务本身就是其他网络服务的消费者。
一旦调用用户被授权“使用”我的网络服务,我必须将基本身份验证转发/设置到我调用的网络服务的请求中。
那么,当我的服务被调用时,我如何/在哪里“拦截”并获取标头?
【问题讨论】:
-
在您构造
Controller时,您不在请求上下文中。没有要获取的标头(除非您在每个请求上都创建一个新实例,而您当前的注释设置并非如此,并且通常是一个可怕的想法)。您究竟想用这些信息做什么,为什么它必须在施工时而不是在请求到来时发生?
标签: java spring-boot web-services