泽西岛ContainerRequestFilter未触发

泽西岛ContainerRequestFilter未触发

本文介绍了泽西岛ContainerRequestFilter未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ContainerRequestFilter在基于Tomcat的Jersey应用程序上实施某些身份验证.我遵循了此文档.问题:永远不会触发过滤器

过滤器类:

@Provider
public class AuthFilter implements ContainerRequestFilter {

    @Override
    public void filter(ContainerRequestContext requestContext)
        throws IOException {

        // MY AUTHENTICATION CODE GOES HERE

    }

web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="debate-rest"
    version="3.0">
  <display-name>rest</display-name>
   <servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
        <param-value>com.hck.debate.rest.security.AuthFilter</param-value>
    </init-param>
    <init-param>
      <param-name>jersey.config.server.provider.packages</param-name>
      <param-value>com.hck.debate.rest.controller</param-value>
    </init-param>
    <init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
解决方案

好吧,我不明白jersey.config.server.provider.packages初始化参数不仅需要引用服务类(API端点),还需要引用所有类,包括过滤器. /p>

现在可以使用了:

<init-param>
  <param-name>jersey.config.server.provider.packages</param-name>
  <param-value>com.hck.debate.rest.controller;com.hck.debate.rest.security</param-value>
</init-param>
<init-param>
    <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
    <param-value>com.hck.debate.rest.security.AuthFilter</param-value>
</init-param>

I'm trying to use a ContainerRequestFilter to enforce some authentication on a Tomcat based Jersey application. I followed this document.Problem : the filter is never triggered

The filter class :

@Provider
public class AuthFilter implements ContainerRequestFilter {

    @Override
    public void filter(ContainerRequestContext requestContext)
        throws IOException {

        // MY AUTHENTICATION CODE GOES HERE

    }

The web.xml file :

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="debate-rest"
    version="3.0">
  <display-name>rest</display-name>
   <servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
        <param-value>com.hck.debate.rest.security.AuthFilter</param-value>
    </init-param>
    <init-param>
      <param-name>jersey.config.server.provider.packages</param-name>
      <param-value>com.hck.debate.rest.controller</param-value>
    </init-param>
    <init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
解决方案

Okay, I didn't get that the jersey.config.server.provider.packages init param needs to reference not only service classes (API endpoints) but ALL the classes including filters.

Now it works :

<init-param>
  <param-name>jersey.config.server.provider.packages</param-name>
  <param-value>com.hck.debate.rest.controller;com.hck.debate.rest.security</param-value>
</init-param>
<init-param>
    <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
    <param-value>com.hck.debate.rest.security.AuthFilter</param-value>
</init-param>

这篇关于泽西岛ContainerRequestFilter未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 10:38