Unable to customize Jersey LoggingFilter to hide user token, getting - PMD violation during build

老子叫甜甜 提交于 2020-07-23 06:39:30

问题


In our project, there is Jersey LoggingFilter being used already.

Currently, a request is logged like this -

    2020-06-08 14:03:09,311 4808881 [XNIO-3 task-3] INFO  [ProfileInfoCacheService.java:155] - Cached profile for user 5024298271678748300
2020-06-08 14:03:09,312 4808882 [XNIO-3 task-3] INFO  [LoggingFilter.java:155] - 37 * Server has received a request on thread XNIO-3 task-3
37 > GET http://project/service/path/to/api
37 > Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
37 > Accept-Encoding: gzip
37 > Authorization: Bearer qsc-2142134-214-1234-1243
37 > Cache-Control: no-cache
37 > Connection: close
37 > Host: sandbox-project.com
37 > Pragma: no-cache
37 > User-Agent: Java/1.8.0_20
37 > X-Forwarded-Host: sandbox-project.com
37 > X-Forwarded-Port: 443
37 > X-Forwarded-Proto: https
37 > X-Forwarded-Server: sandbox-project.com

I would like to just remove/mask the logging of Authorization parameter.

I followed this blog - https://howtodoinjava.com/jersey/jersey-custom-logging-request-and-response-entities-using-filter/#default-logs

In my setup, LoggingFilter is defined as final and cannot be extended.

If I try to define a CustomLoggingFilter and extend it from the classes which LoggingFilter is extending -

public class CustomLoggingFilter implements ContainerRequestFilter, ClientRequestFilter, ContainerResponseFilter,
                                            ClientResponseFilter, WriterInterceptor {

.. Same as LoggingFilter

}

And modify in JerseyConfig -

import javax.ws.rs.ApplicationPath;

import com.citruspay.prepaid.common.CustomLoggingFilter;
import org.glassfish.jersey.filter.LoggingFilter;
import org.glassfish.jersey.media.multipart.MultiPartFeature;
import org.glassfish.jersey.server.ResourceConfig;
import org.springframework.context.annotation.Configuration;

@Configuration
@ApplicationPath("/service")
public class JerseyConfig extends ResourceConfig {

    public JerseyConfig() {
        packages("com.project");
        register(CustomLoggingFilter.class);
        //register(LoggingFilter.class);
        register(MultiPartFeature.class);
    }
}

I get the following error on building -

org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-pmd-plugin:3.5:check (default) on project project-app: You have 1 PMD violation. For more details see: /Users/sandeepan.nath/Desktop/codebase/project/target/pmd.xml
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:212)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:863)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:288)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:199)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: org.apache.maven.plugin.MojoFailureException: You have 1 PMD violation. For more details see: /Users/sandeepan.nath/Desktop/codebase/project/target/pmd.xml
    at org.apache.maven.plugin.pmd.AbstractPmdViolationCheckMojo.executeCheck(AbstractPmdViolationCheckMojo.java:143)
    at org.apache.maven.plugin.pmd.PmdViolationCheckMojo.execute(PmdViolationCheckMojo.java:84)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:207)
    ... 20 more
[ERROR] 

来源:https://stackoverflow.com/questions/62277082/unable-to-customize-jersey-loggingfilter-to-hide-user-token-getting-pmd-viola

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!