我正在尝试几个小时来完成主题中解释的机制:Best practice for REST token-based authentication with JAX-RS and Jersey
但调试后似乎我没有停在ContainerRequestFilter中...... 这就是我所拥有的:
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.ws.rs.NameBinding;
@NameBinding
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface Secured { }
我的AuthenticationFilter与主题中所解释的完全相同(复制过去)
我有一个端点:
@Path("/")
@WebService(name="account", targetNamespace="")
public interface ProfileCXFService
{
@GET
@Secured
@Path("/displayProfile")
@Produces({MediaType.APPLICATION_JSON})
@Consumes({MediaType.APPLICATION_JSON})
public Response displayProfile(@QueryParam("token")String token, @QueryParam("profileID")String profileID);
}
这是我的web.xml的内容
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>CxfRestService</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/beans.xml</param-value>
</context-param>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
这是我与邮递员发送的请求(目前令牌在身体内部,但即使我把它放在身份验证标头参数中也不起作用)。问题是端点的代码是直接执行的,没有调用Filter。
GET /BD-BD/rest/account/displayProfile?token=vfE1qPLiiyPx20ymp0C483VxcpCj07HKAB8cTmgXamp;profileID=35 HTTP/1.1
Host: localhost:8080
Content-Type: application/json
Cache-Control: no-cache
Postman-Token: 0908ac3d-17ab-55ef-337a-fabfe7fed387
是否需要配置? 在此先感谢:)
答案 0 :(得分:1)
使用Apache CXF时,必须在cxf.xml
配置文件中注册过滤器和拦截器。此文件必须位于应用程序的类路径中。
这是从注册过滤器时CXF配置文件的CXF documentation中提取的示例:
<beans>
<jaxrs:server id="customerService" address="/">
<jaxrs:serviceBeans>
<bean class="org.CustomerService" />
</jaxrs:serviceBeans>
<jaxrs:providers>
<ref bean="authorizationFilter" />
</jaxrs:providers>
<bean id="authorizationFilter" class="com.bar.providers.AuthorizationFilter">
<!-- authorization bean properties -->
</bean>
</jaxrs:server>
</beans>
有关详细信息,请查看CXF documentation about configuration。