我有一个Spring Rest网络应用程序。并创建了auth过滤器。在这一层,我得到了RestController方法所需的User。我想避免DB请求重复。是否可以将一些对象从Filter传递给RestController方法作为参数?
@Override
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException,
ServletException {
String authorization = ((HttpServletRequest) req).getHeader("Authorization");
DeviceEntity deviceEntity = mDeviceService.byToken(authorization);
if (deviceEntity != null) {
if (new Date().before(deviceEntity.getExpireOn())) {
chain.doFilter(req, res);
}
} else {
((HttpServletResponse) res).setStatus(HttpServletResponse.SC_UNAUTHORIZED);
}
}
我想将deviceEntity传递给下一个类
@RestController
@RequestMapping("/general")
public class RestService {
@RequestMapping(value = "/ping", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
public void ping(DeviceEntity de) {
// ^^^^^^^^^^
//i want to have access to this device here
LOG.info("Ping called");
}
}
我需要一些方法将此Device实体传递给RequestMapped方法