【问题标题】:How to use/configure JAX-RS 2.0, SpringSecurity 3.1.+, EJB 3.2 all together如何一起使用/配置 JAX-RS 2.0、SpringSecurity 3.1.+、EJB 3.2
【发布时间】:2013-11-12 15:36:32
【问题描述】:

我目前正在尝试使用以下主要技术建立一个项目:

  • Java EE 7
  • EJB 3.2
  • JAX-RS(泽西)2.0
  • 玻璃鱼 4
  • Spring 安全 3.1.5

我看到可以写这样的东西

@Stateless
@Path("apath")
public class WebResource {
    @EJB
    private SomeService serviceInjected;

    @GET
    public Response doSomething() {
        return Response.ok(injectedService.doSomethingElse()).build();
    }
}

然后,这意味着 SomeService Session Bean 由容器注入,一旦我们调用路径::///apath,一切正常。

现在,我尝试实现的是将 SpringSecurity 框架集成到该代码中。所以我的代码变成了这样:

@Component
@Stateless
@Path("apath")
public class WebResource {
    @EJB
    private SomeService serviceInjected;

    @GET
    @PreAuthorized("hasPermission('ROLE_SOMETHING')")
    public Response doSomething() {
        return Response.ok(injectedService.doSomethingElse()).build();
    }
}

但是,这不起作用。除了 SpringSecurity 注释之外的所有东西都可以继续工作。只是不考虑授权注释。

在 SpringSecurity 配置文件中,我有这样的东西:

<security:global-method-security
    access-decision-manager-ref="preVoteAccessDecisionManager"
    pre-post-annotations="enabled" />

与过滤器链相关的所有内容都正确配置。例如,我有:

<beans:bean id="securityInterceptor" class="org.springframework.security.web.access.intercept.FilterSecurityInterceptor">
    <beans:property name="securityMetadataSource">
        <security:filter-security-metadata-source>
            <security:intercept-url pattern="/**" access="ROLE_TEST" />
        </security:filter-security-metadata-source>
    </beans:property>
    <beans:property name="authenticationManager" ref="authenticationManager" />
    <beans:property name="accessDecisionManager" ref="accessDecisionManager" />
</beans:bean>

我在 Glassfish 4 服务器日志中看到 SpringSecurity 为我的经过身份验证的用户管理了 ROLE_TEST 访问。我还看到经过身份验证的用户具有我期望的角色列表。

我也尝试使用此配置并依赖 javax.annotation.security 注释,如下所示:

<security:global-method-security
    access-decision-manager-ref="preVoteAccessDecisionManager"
    jsr250-annotations="enabled" />

@Stateless
@Path("apath")
public class WebResource {
    @EJB
    private SomeService serviceInjected;

    @GET
    @RolesAllowed("ROLE_SOMETHING")
    public Response doSomething() {
        return Response.ok(injectedService.doSomethingElse()).build();
    }
}

这一次,注解起作用了,当用户通过身份验证时抛出异常。但是在这种情况下,我的用户有角色,但容器使用的SecurityContext没有填充与SpringSecurity认证的用户相关的Principal和角色信息。

最后,我的问题。有没有办法将 JAX-RS / @Stateless / SpringSecurity 授权集成在一起?如果没有,有没有办法从 SrpingSecurity 填充 SecurityContext 以允许 javax.annotation.security 像魅力一样工作?

在此先感谢您提供任何帮助、提示、技巧或其他任何可以解决我的问题的方法:D

【问题讨论】:

    标签: spring-security jax-rs glassfish-4 java-ee-7 ejb-3.2


    【解决方案1】:

    Spring Security 的方法安全注解通常只适用于生命周期由 Spring 控制的 Spring beans。这不包括 EJB。但是,如果您希望可以使用适用于任何对象(包括 EJB 实例)的 AspectJ 集成。 Spring Security 代码库中有一个sample application,您可以将其用作参考。您是否需要使用 EJB 也可能值得考虑。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-21
      • 1970-01-01
      • 2020-10-14
      相关资源
      最近更新 更多