Guice:在 ServletModule 中注入拦截器

发布于 2024-12-24 20:28:09 字数 2270 浏览 0 评论 0原文

我正在尝试使用 Guice 创建的 Vaadin 应用程序实例注入拦截器。 我已按照 Vaadin 维基Guice Wiki 中有关 Interceptor DI 的文档:

public class RecruitmentServletConfig extends GuiceServletContextListener {

    @Override
    protected Injector getInjector() {

        ServletModule servletModule = new ServletModule() {

            @Override
            protected void configureServlets() {
                ...
                bind(Application.class).to(RecruitmentApplication.class).in(ServletScopes.SESSION);
                SecurityGuard securityGuard = new SecurityGuard();
                requestInjection(securityGuard);
                bindInterceptor(Matchers.subclassesOf(CustomComponent.class), Matchers.annotatedWith(AllowedRoles.class), securityGuard);
            }
        };
        return Guice.createInjector(servletModule);
    }
}

SecurityGuard 拦截器:

public class SecurityGuard implements MethodInterceptor {
    @Inject private Application application;

    public Object invoke(MethodInvocation invocation) throws Throwable {
        AllowedRoles allowedRoles = invocation.getMethod().getAnnotation(AllowedRoles.class);
        if (((User) application.getUser()).hasRole(allowedRoles.value())) {
            return invocation.proceed(); 
        } else {
            return null;
        }
}

但是,我在服务器启动时收到 OutOfScopeException:

SEVERE: Exception sending context initialized event to listener instance of class de.embl.eicat.recruit.ioc.RecruitmentServletConfig
com.google.inject.CreationException: Guice creation errors:
1) Error in custom provider, com.google.inject.OutOfScopeException: Cannot access scoped object. Either we are not currently inside an HTTP Servlet request, or you may have forgotten to apply com.google.inject.servlet.GuiceFilter as a servlet filter for this request.
at recruit.ioc.RecruitmentServletConfig$1.configureServlets(RecruitmentServletConfig.java:86)

I am trying to inject an Interceptor with a Vaadin Application instance created by Guice.
I've followed the documentation for Vaadin-Guice integration in the Vaadin Wiki and
the documenation on Interceptor DI in the Guice Wiki:

public class RecruitmentServletConfig extends GuiceServletContextListener {

    @Override
    protected Injector getInjector() {

        ServletModule servletModule = new ServletModule() {

            @Override
            protected void configureServlets() {
                ...
                bind(Application.class).to(RecruitmentApplication.class).in(ServletScopes.SESSION);
                SecurityGuard securityGuard = new SecurityGuard();
                requestInjection(securityGuard);
                bindInterceptor(Matchers.subclassesOf(CustomComponent.class), Matchers.annotatedWith(AllowedRoles.class), securityGuard);
            }
        };
        return Guice.createInjector(servletModule);
    }
}

The SecurityGuard interceptor:

public class SecurityGuard implements MethodInterceptor {
    @Inject private Application application;

    public Object invoke(MethodInvocation invocation) throws Throwable {
        AllowedRoles allowedRoles = invocation.getMethod().getAnnotation(AllowedRoles.class);
        if (((User) application.getUser()).hasRole(allowedRoles.value())) {
            return invocation.proceed(); 
        } else {
            return null;
        }
}

However, I get an OutOfScopeException on server startup:

SEVERE: Exception sending context initialized event to listener instance of class de.embl.eicat.recruit.ioc.RecruitmentServletConfig
com.google.inject.CreationException: Guice creation errors:
1) Error in custom provider, com.google.inject.OutOfScopeException: Cannot access scoped object. Either we are not currently inside an HTTP Servlet request, or you may have forgotten to apply com.google.inject.servlet.GuiceFilter as a servlet filter for this request.
at recruit.ioc.RecruitmentServletConfig$1.configureServlets(RecruitmentServletConfig.java:86)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仙气飘飘 2024-12-31 20:28:09

如果将 Application 包装在 Provider 中,它可以工作吗?

public class SecurityGuard implements MethodInterceptor {
    @Inject private Provider<Application> application;

    public Object invoke(MethodInvocation invocation) throws Throwable {
       AllowedRoles allowedRoles = invocation.getMethod().getAnnotation(AllowedRoles.class);
        if (((User) application.get().getUser()).hasRole(allowedRoles.value())) {
            return invocation.proceed(); 
        } else {
            return null;
        }
}

Does it work if you wrap your Application in a Provider?

public class SecurityGuard implements MethodInterceptor {
    @Inject private Provider<Application> application;

    public Object invoke(MethodInvocation invocation) throws Throwable {
       AllowedRoles allowedRoles = invocation.getMethod().getAnnotation(AllowedRoles.class);
        if (((User) application.get().getUser()).hasRole(allowedRoles.value())) {
            return invocation.proceed(); 
        } else {
            return null;
        }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文