我正在开发一个使用 Jersey 框架的 REST 应用程序。我想知道如何控制用户身份验证。我搜索了很多地方,我找到的最接近的文章是:http://weblogs.java.net/blog/2008/03/07/authentication-jersey .
但是,本文只能与 GlassFish 服务器和附加数据库一起使用。无论如何,我可以在 Jersey 中实现一个接口(interface),并在到达请求的 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 留空 (
关于java - Jersey REST 服务上的用户身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2902427/