【问题标题】:PreAuthorize annotation doesn't work with jerseyPreAuthorize 注释不适用于球衣
【发布时间】:2014-05-25 19:55:06
【问题描述】:

我正在尝试使用 spring 安全注释来保护球衣服务,但没有任何运气。

我已将此部分添加到 web.xml:

<servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.test.proj.ui.web.rest;com.fasterxml.jackson.jaxrs</param-value>
    </init-param>
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>com.test.commons.ui.web.jersey.RestApplication</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

还在 applicationContext 上使用 this 启用了 pre-post-annotations:

<global-method-security secured-annotations="enabled" pre-post-annotations="enabled" />

这是我的服务类:

@Component
@Path("/user/{uid: .*}")
public class UserResource {
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @PreAuthorize("hasRole('ROLE_MANAGE_USER')")
    public Response getUserDetail(@PathParam("uid") String uid) {
        return "Hi, this is a test";
    }
}

Spring 安全性在身份验证中运行良好,但授权无法按预期工作,并忽略 PreAuthorize 注释而没有任何错误或日志。

我正在使用 Spring 3.2.4 和 Spring Security 3.2.1 和 Jersy 2.6。

有什么想法吗?

谢谢

【问题讨论】:

    标签: java spring-security annotations jersey-2.0


    【解决方案1】:

    弹簧组件扫描配置不正确!要解决这个问题,只需正确添加组件扫描即可。

    【讨论】:

    • 我也有同样的问题。你能解释一下到底该怎么做吗?我已经用 - @Component 注释了服务类。
    • 在你的 spring 配置中添加 &lt;context:component-scan base-package="com.example.something" /&gt; 之类的东西
    • @ComponentScan(...)
    【解决方案2】:

    我们面临着完全相同的问题。在业务层 @PreAuthorize 注释有效,但在 REST 资源上无效。这种情况的副作用是 Spring bean 注入不能正常工作。一切都没有任何错误。

    100% 可行的解决方案是使用 RESTEasy 而不是 Jersey。它们非常相似,因此没有太多工作。

    【讨论】:

      猜你喜欢
      • 2013-05-13
      • 2011-07-12
      • 1970-01-01
      • 1970-01-01
      • 2015-06-24
      • 1970-01-01
      • 1970-01-01
      • 2015-04-10
      • 2016-04-16
      相关资源
      最近更新 更多