java - 为什么 Spring Boot Microservice 的参数 Principal 为 null 值,如何获取客户端应用程序的实际值?
问题描述
在 Spring Boot 中,我有以下微服务,它需要 Principal 对象才能访问登录的用户信息:
@RequestMapping(value="/circular-save")
public Boolean saveCircularView(HttpServletRequest request, Principal principal, HttpSession session, Locale locale, ModelAndView mav,
@ModelAttribute CircularsBean souqBean) {
System.out.println( "circular-save microservice Called...........principal="+principal);
//some user related code here that depends on principal
}
输出:
circular-save microservice Called.....................principal=null
我将上述微服务称为:
System.out.println("Will call circulars with principal:"+principal.getName());
restTemplate.getForObject("http://localhost:8081/circular-save", Boolean.class);
输出:
Will call circulars with principal:travelling.salesman
如您所见,principal 值在客户端不为 null,但在服务器端为 null。如何正确地让这个主体对象在微服务之间传递?
我的研究:
起初,我正在考虑在 json 请求中传递它并使用 POST 方法,但我失败了,因为它不是 POJO 并且无法序列化它。
我做了更多的研究,发现这行可以写在服务器端。
principal = SecurityContextHolder.getContext().getAuthentication();
不幸的是,上述解决方案给了我不同的对象,导致匿名用户与原始用户不同。
我将不胜感激任何帮助。谢谢。
解决方案
推荐阅读
- php - 如何在 Stripe 中一步创建客户、付款方式、订阅
- calculated-columns - 如何根据其 3 个相邻行的最小值和最大值插入新列?
- python - rioxarray(或xarray)在使用to_netcdf重新投影和保存后将spatial_ref坐标转换为变量?
- javascript - 为什么我收到“TypeError:this.state.users.map is not a function”,用于部署的 React 应用程序的生产版本
- javascript - MuseJSAssert:调用选择器函数时出错:TypeError:this.elem.get(...) 未定义
- linux - zsh 打印转义字符以记录
- unity3d - *已解决*我无法让我的精灵在 Unity 中触发对撞机
- acumatica - 将自定义字段中的值复制到 RowPersisting 事件中的 TaxRegistrationID 字段
- laravel - 使用标头参数的邮递员授权工作但不适用于 Axios 标头
- apache - 我正在尝试使用 ssl xampp 创建自签名证书,但出现以下错误