从 AuthenticationSuccessHandler 获取映射器
Get mapper from AuthenticationSuccessHandler
我正在尝试 return 在 Spring 启动应用程序中成功登录后的一些用户数据。
为此,我需要使用已通过工厂在 bootstrap 设置的 Jackson 映射器序列化我的 Principal。
有没有办法将其注入身份验证处理程序?
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
response.setStatus(HttpServletResponse.SC_OK);
UserDetails me = (UserDetails) authentication.getPrincipal();
PrintWriter writer = response.getWriter();
/*mapper.writeValue(writer, user); <- how to get the mapper?
writer.flush();*/
}
tl;博士;
不使用 Factory
,而是使 ObjectMapper
成为 Spring bean。这样它就可以被注入到 Spring 和 Jersey 组件中。
@Bean
public ObjectMapper mapper() {
ObjectMapper mapper = new ObjectMapper();
return mapper;
}
当您使用 HK2(Jersey 的 DI 框架)Factory
创建 ObjectMapper
时,注入仅适用于通过 HK2 ServiceLocator
检索的组件,即Spring ApplicationContext
.
的 HK2 类似物
当您使用 Spring-Boot with Jersey 时,两者在后台如何交互,是通过 HK2 spring-bridge。例如,假设 AuthenticationSuccessHandler
在 ApplicationContext
中注册。通过配置 spring-bridge,我们还可以通过 ServiceLocator
获取 AuthenticationSuccessHandler
ServiceLocator l = ...
AuthenticationSuccessHandler handler = l.getService(AuthenticationSuccessHandler.class);
应该可以。如果您通过 HK2 Factory
绑定了 ObjectMapper
,并且您要将映射器 @Inject
放入 AuthenticationSuccessHandler
,则当通过 [=16] 检索时,它将被注入=].
也就是说,Spring 安全性不会通过 HK2,因此它永远没有机会通过 HK2 获得 ObjectMapper
。但是,如果您将映射器注册为 Spring bean,则 Spring 可以将其注入 AuthenticationSuccessHandler
。映射器仍然可以通过 HK2 ServiceLocator
使用(因为 spring-bridge),所以你仍然可以将它与 Jersey 一起使用,就像它与 Factory
绑定一样。
我正在尝试 return 在 Spring 启动应用程序中成功登录后的一些用户数据。 为此,我需要使用已通过工厂在 bootstrap 设置的 Jackson 映射器序列化我的 Principal。
有没有办法将其注入身份验证处理程序?
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
response.setStatus(HttpServletResponse.SC_OK);
UserDetails me = (UserDetails) authentication.getPrincipal();
PrintWriter writer = response.getWriter();
/*mapper.writeValue(writer, user); <- how to get the mapper?
writer.flush();*/
}
tl;博士;
不使用 Factory
,而是使 ObjectMapper
成为 Spring bean。这样它就可以被注入到 Spring 和 Jersey 组件中。
@Bean
public ObjectMapper mapper() {
ObjectMapper mapper = new ObjectMapper();
return mapper;
}
当您使用 HK2(Jersey 的 DI 框架)Factory
创建 ObjectMapper
时,注入仅适用于通过 HK2 ServiceLocator
检索的组件,即Spring ApplicationContext
.
当您使用 Spring-Boot with Jersey 时,两者在后台如何交互,是通过 HK2 spring-bridge。例如,假设 AuthenticationSuccessHandler
在 ApplicationContext
中注册。通过配置 spring-bridge,我们还可以通过 ServiceLocator
AuthenticationSuccessHandler
ServiceLocator l = ...
AuthenticationSuccessHandler handler = l.getService(AuthenticationSuccessHandler.class);
应该可以。如果您通过 HK2 Factory
绑定了 ObjectMapper
,并且您要将映射器 @Inject
放入 AuthenticationSuccessHandler
,则当通过 [=16] 检索时,它将被注入=].
也就是说,Spring 安全性不会通过 HK2,因此它永远没有机会通过 HK2 获得 ObjectMapper
。但是,如果您将映射器注册为 Spring bean,则 Spring 可以将其注入 AuthenticationSuccessHandler
。映射器仍然可以通过 HK2 ServiceLocator
使用(因为 spring-bridge),所以你仍然可以将它与 Jersey 一起使用,就像它与 Factory
绑定一样。