我正在开发一个使用 Jersey 框架的 REST 应用程序。我想知道如何控制用户身份验证。我搜索了很多地方,我找到的最接近的文章是:http ://weblogs.java.net/blog/2008/03/07/authentication-jersey 。
但是,本文只能与 GlassFish 服务器和附加数据库一起使用。无论如何,我可以在 Jersey 中实现一个接口并在到达请求的 REST 资源之前将其用作过滤器吗?
我现在想使用基本身份验证,但它应该足够灵活,以便我以后可以更改它。
我正在开发一个使用 Jersey 框架的 REST 应用程序。我想知道如何控制用户身份验证。我搜索了很多地方,我找到的最接近的文章是:http ://weblogs.java.net/blog/2008/03/07/authentication-jersey 。
但是,本文只能与 GlassFish 服务器和附加数据库一起使用。无论如何,我可以在 Jersey 中实现一个接口并在到达请求的 REST 资源之前将其用作过滤器吗?
我现在想使用基本身份验证,但它应该足够灵活,以便我以后可以更改它。
我成功地使用 spring security 来保护我的基于 Jersey 的 API。它具有可插入的身份验证方案,允许您稍后从基本身份验证切换到其他方式。我一般不使用 Spring,只是安全性的东西。
这是我的 web.xml 中的相关部分
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/security-applicationContext.xml,
/WEB-INF/applicationContext.xml
</param-value>
</context-param>
<!-- Enables Spring Security -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>
org.springframework.web.filter.DelegatingFilterProxy
</filter-class>
<init-param>
<param-name>targetBeanName</param-name>
<param-value>springSecurityFilterChain</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
您可以将 applicationContext.xml 留空 (<beans></beans>)。可以在此处找到 security-applicationContext.xml 的示例
我正在做类似的事情。在我的实现中,我们有 Apache httpd 前端来处理 HTTP Basic 身份验证,它只是转发所有带有一些包含用户和角色的标头信息的请求。
从那以后,我正在使用servlet 过滤器HttpServletRequest解析这些片段,以使用我在CodeRanch上找到的帖子来包装这些片段。这使我可以在要过滤的每个资源上使用javax.annotation.security注释。@RolesAllowed但是,为了使所有这些部分正常工作,我必须将以下内容添加到我的 servlet 中web.xml:
<servlet>
<!-- some other settings and such
... -->
<init-param>
<param-name>com.sun.jersey.spi.container.ResourceFilters</param-name>
<param-value>com.sun.jersey.api.container.filter.RolesAllowedResourceFilterFactory</param-value>
</init-param>
...
</servlet>
您可能会发现 Eric Warriner 在最近感兴趣的帖子中的回答: Jersey, Tomcat and Security Annotations
看看这里,我正在尝试它,但它看起来很有希望:
http://anismiles.wordpress.com/2012/03/02/securing-versioning-and-auditing-rest-jax-rs-jersey-apis/
This example is much simpler than attempting to implement JASPI/JASPIC and gives better granularity to the individual methods (@RolesAllowed, @PermitAll, @DenyAll, etc...).
(I know this is an old thread, but just adding potentially useful information)
当然,您可以为此使用传统的 servlet 过滤器。
将过滤器添加到您的 web.xml,检查您正在使用的任何身份验证标头(基本或摘要),根据这些值执行身份验证逻辑,并将结果存储在会话属性中。在您的 Jersey 资源(可能是 ctor)中,从会话属性中提取身份验证结果并根据这是否是您需要的结果继续处理。
您的 Jersey 资源 ctor 可能如下所示:
protected AbstractResource(@Context ServletContext servletContext,
@Context HttpServletRequest httpServletRequest) {
...
HttpSession session = httpServletRequest.getSession();
// get whatever you put in the session in the auth filter here and compare
}
您可以通过两种方式来实现,或者您编写一个简单的 servlet 过滤器,或者您必须实现一个 ResourceFilterFactory 并在 ContainerRequestFilter 中处理身份验证。详细代码在链接http://neopatel.blogspot.com/2011/11/jesey-writing-authentication-filter.html中。我个人喜欢 servlet 过滤器方法,因为它提供了完整的生命周期控制。但是,如果您需要更具体的东西,例如访问 QueryParams 或 PathParams,那么 ResourceFilterFactory 是您的最佳选择。