java - Spring 将 Autowired SessionAttribute 注入服务层
问题描述
有没有办法将@Inject/@Autowired
SessionAttribute 直接放入@Service
图层而不通过@Controller
?
我正在寻找这样的东西:
@Autowired
@SessionAttribute("userprincipal")
UserPrincipal principal;
可能的解决方案:
@Configuration
public class ApplicationConfig {
@Bean
@Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)
public UserPrincipal sessionUserPrincipal() {
// not sure here the user does not exist at creation of bean
}
}
解决方案
我的解决方案,希望这可以节省其他人一些时间。
注意:注入的依赖是隐藏的,如果在会话之外使用会导致问题。Optional<T>
在这种情况下使用并在内部处理。如果您共享您的代码,您的团队将不会意识到所需的依赖关系。
测试:在测试时,您需要提供会话 bean 的@Autowired
功能。
会话 Bean 类:
public class SessionUserPrincipal implements Serializable {
private static final long serialVersionUID = 1L;
private UserPrincipal principal;
public SessionUserPrincipal() {}
// mutator methods omitted
}
Optional<T>
如果不能保证会话属性可用,则返回
将 Bean 添加到上下文:
@Configuration
public class WebServletContextConfiguration implements WebMvcConfigurer {
@Bean
@Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)
public SessionUserPrincipal sessionUserPrincipal() {
return new SessionUserPrincipal();
}
}
将 RequestContextListener 添加到 web.xml
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
这是下面的代码工作的要求。它公开了实现会话范围所必需的状态。默认情况下,该状态由 DispatcherServlet 公开,因此在请求进入 DispatcherServlet(Spring Security 过滤器)之前它不可用。@Autowire
如果您在会话 bean 可用之前尝试访问它,您将得到一个异常。
成功验证时将会话属性添加到会话@Bean
。
public class CustomAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
@Autowired SessionUserPrincipal sessionUserPrincipal;
@Override
public void onAuthenticationSuccess(
HttpServletRequest request,
HttpServletResponse response,
Authentication authentication) throws IOException, ServletException
{
// find/get userprincipal code omitted
sessionUserPrincipal.setPrincipal(userprincipal);
}
}
使用会话 bean:
@Service
public class DefaultSomeService implements SomeService {
@Autowired private SessionUserPrincipal sessionUserPrincipal;
}
推荐阅读
- cordova - 使用 Cordova 中的地址打开设备的地图应用程序
- reactjs - 反应国际删除错误日志并列出键
- sql - 今天安装 Microsoft .NET Framework 4.8 版本 4.8.03761 后,Access 数据库 vba UPDATE SQL 命令停止工作
- vim - VIM Colemak yank 重新映射问题
- java - Android Wear OS 关闭 Wifi
- javascript - 有没有办法禁用对 div 及其子节点的关注?
- leaflet - 传单点击地图并检索地址
- java - 数组访问时出现 ArrayIndexOutOfBoundsException
- javascript - 如何根据 Jira 中的自定义字段仅在脚本用户选择器中显示特定组的用户?
- sql - Oracle 中是否有等效于 TRY_CONVERT() 的方法?