【发布时间】:2014-11-09 16:49:50
【问题描述】:
我正在尝试开发一个使用 RESTEasy 编写的 RESTFul 服务。当我尝试使用基于 RESTEasy 角色的身份验证和基于 HTTP 标头的身份验证凭据来限制服务访问时,由于以下原因,它没有成功。
标记的服务根据服务上定义的 @RolesAllowed 正确限制。但是,在访问尝试时,它会在后端引发更通用的异常(“HTTP 403 Forbidden”)。这违背了我的意愿,因为即使我在 HTTP 标头中传递了安全凭据,它也会引发相同的异常。
当我试图通过引入 ContainerRequestFilter 来解决这个问题时,它从未被调用过。
任何人都可以帮助我并指出出了什么问题。
这些是实现细节
服务类
<pre>
@Path("/user-service")
public class UserService {
@RolesAllowed("ADMIN")
@PUT
@Path("/users/{userName}")
public Response updateUser(@PathParam("userName") String userName, @Context Request req) {
// Do processing stuff here
return Response.status(200).build();
}
}
</pre><br>
SecurityIntercepter 类
<pre>
@Provider
@PreMatching
public class SecurityInterceptor implements javax.ws.rs.container.ContainerRequestFilter {
@Override
public void filter(ContainerRequestContext requestContext) {
System.out.println("<SecurityInterceptor> Invoking SecurityInterceptor filter");
ResourceMethodInvoker methodInvoker = (ResourceMethodInvoker) requestContext
.getProperty("org.jboss.resteasy.core.ResourceMethodInvoker");
Method method = methodInvoker.getMethod();
// Do process HTTP Header and authorized based on the auth Token
}
}
</pre>
web.xml
<web-app>
<context-param>
<param-name>resteasy.resources</param-name>
<param-value>my.package.UserService</param-value>
</context-param>
<context-param>
<param-name>resteasy.role.based.security</param-name>
<param-value>true</param-value>
</context-param>
<listener>
<listener-class>
org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
</listener-class>
</listener>
<servlet>
<servlet-name>resteasy-servlet</servlet-name>
<servlet-class>
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>resteasy-servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
【问题讨论】:
标签: java resteasy restful-authentication