SerialFilterChecker.java

  1. /*
  2.  *    Copyright 2009-2024 the original author or authors.
  3.  *
  4.  *    Licensed under the Apache License, Version 2.0 (the "License");
  5.  *    you may not use this file except in compliance with the License.
  6.  *    You may obtain a copy of the License at
  7.  *
  8.  *       https://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  *    Unless required by applicable law or agreed to in writing, software
  11.  *    distributed under the License is distributed on an "AS IS" BASIS,
  12.  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  *    See the License for the specific language governing permissions and
  14.  *    limitations under the License.
  15.  */
  16. package org.apache.ibatis.io;

  17. import java.security.Security;

  18. import org.apache.ibatis.logging.Log;
  19. import org.apache.ibatis.logging.LogFactory;

  20. public final class SerialFilterChecker {
  21.   private static final Log log = LogFactory.getLog(SerialFilterChecker.class);
  22.   /* Property key for the JEP-290 serialization filters */
  23.   private static final String JDK_SERIAL_FILTER = "jdk.serialFilter";
  24.   private static final boolean SERIAL_FILTER_MISSING;
  25.   private static boolean firstInvocation = true;

  26.   static {
  27.     Object serialFilter;
  28.     try {
  29.       Class<?> objectFilterConfig = Class.forName("java.io.ObjectInputFilter$Config");
  30.       serialFilter = objectFilterConfig.getMethod("getSerialFilter").invoke(null);
  31.     } catch (ReflectiveOperationException e) {
  32.       // Java 1.8
  33.       serialFilter = System.getProperty(JDK_SERIAL_FILTER, Security.getProperty(JDK_SERIAL_FILTER));
  34.     }
  35.     SERIAL_FILTER_MISSING = serialFilter == null;
  36.   }

  37.   public static void check() {
  38.     if (firstInvocation && SERIAL_FILTER_MISSING) {
  39.       firstInvocation = false;
  40.       log.warn(
  41.           "As you are using functionality that deserializes object streams, it is recommended to define the JEP-290 serial filter. "
  42.               + "Please refer to https://docs.oracle.com/pls/topic/lookup?ctx=javase15&id=GUID-8296D8E8-2B93-4B9A-856E-0A65AF9B8C66");
  43.     }
  44.   }

  45.   private SerialFilterChecker() {
  46.   }
  47. }