Spring security 如何使用/配置JAX-RS 2.0、SpringSecurity 3.1和#x2B;,EJB3.2一起使用
我目前正在尝试使用以下主要技术建立一个项目:Spring security 如何使用/配置JAX-RS 2.0、SpringSecurity 3.1和#x2B;,EJB3.2一起使用,spring-security,jax-rs,glassfish-4,java-ee-7,ejb-3.2,Spring Security,Jax Rs,Glassfish 4,Java Ee 7,Ejb 3.2,我目前正在尝试使用以下主要技术建立一个项目: Java EE 7 EJB3.2 JAX-RS(泽西岛)2.0 玻璃鱼4 Spring Security 3.1.5 我发现写这样的东西是可能的 @Stateless @Path("apath") public class WebResource { @EJB private SomeService serviceInjected; @GET public Response doSomething() {
- Java EE 7
- EJB3.2
- JAX-RS(泽西岛)2.0
- 玻璃鱼4
- Spring Security 3.1.5
@Stateless
@Path("apath")
public class WebResource {
@EJB
private SomeService serviceInjected;
@GET
public Response doSomething() {
return Response.ok(injectedService.doSomethingElse()).build();
}
}
然后,这意味着SomeService会话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为经过身份验证的用户管理了角色测试访问。我还看到我的用户认证了我期望的角色列表
我还尝试使用此配置并依赖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();
}
}
@无国籍
@路径(“磷灰石”)
公共类网络资源{
@EJB
私人服务注入;
@得到
@允许角色(“角色/某物”)
公众反应{
返回Response.ok(injectedService.doSomethingElse()).build();
}
}
这一次,注释正在工作,当用户通过身份验证时,会引发异常。但在本例中,我的用户具有角色,但容器使用的SecurityContext没有填充与SpringSecurity验证的用户相关的主体和角色信息
最后,我的问题。有没有办法将JAX-RS/@Stateless/SpringSecurity授权集成在一起?如果没有,有没有办法从SrpingSecurity中填充SecurityContext,让javax.annotation.security像符咒一样工作
提前感谢您提供的任何帮助、提示、技巧或任何其他可以解决我的问题的东西:DSpring Security的方法安全性注释通常只适用于生命周期由Spring控制的Spring Bean。这不包括EJB。但是,如果您愿意,可以使用AspectJ集成,它将适用于包括EJB实例在内的任何对象。Spring安全代码库中有一个可用作参考的代码。您是否需要使用EJB也值得考虑