You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2021/06/23 11:32:08 UTC

[tomcat] branch 8.5.x updated: Fix some serialization warnings

This is an automated email from the ASF dual-hosted git repository.

markt pushed a commit to branch 8.5.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/8.5.x by this push:
     new 8b85b67  Fix some serialization warnings
8b85b67 is described below

commit 8b85b67325a7748836a734018fe32e53853d51c5
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Wed Jun 23 12:28:43 2021 +0100

    Fix some serialization warnings
---
 .../apache/catalina/realm/UserDatabaseRealm.java    | 21 +++++++++++++--------
 .../catalina/users/MemoryUserDatabaseTests.java     |  3 +--
 webapps/docs/changelog.xml                          |  4 ++++
 3 files changed, 18 insertions(+), 10 deletions(-)

diff --git a/java/org/apache/catalina/realm/UserDatabaseRealm.java b/java/org/apache/catalina/realm/UserDatabaseRealm.java
index 8622975..ed8e69e 100644
--- a/java/org/apache/catalina/realm/UserDatabaseRealm.java
+++ b/java/org/apache/catalina/realm/UserDatabaseRealm.java
@@ -16,6 +16,7 @@
  */
 package org.apache.catalina.realm;
 
+import java.io.ObjectStreamException;
 import java.security.Principal;
 import java.util.Arrays;
 import java.util.HashSet;
@@ -172,7 +173,7 @@ public class UserDatabaseRealm extends RealmBase {
         if (user == null) {
             return null;
         } else {
-            return new UserDatabasePrincipal(user);
+            return new UserDatabasePrincipal(user, database);
         }
     }
 
@@ -246,13 +247,15 @@ public class UserDatabaseRealm extends RealmBase {
     }
 
 
-    public final class UserDatabasePrincipal extends GenericPrincipal {
+    public static final class UserDatabasePrincipal extends GenericPrincipal {
         private static final long serialVersionUID = 1L;
-        private final User user;
+        private final transient User user;
+        private final transient UserDatabase database;
 
-        public UserDatabasePrincipal(User user) {
+        public UserDatabasePrincipal(User user, UserDatabase database) {
             super(user.getName(), null, null);
             this.user = user;
+            this.database = database;
         }
 
         @Override
@@ -282,7 +285,6 @@ public class UserDatabaseRealm extends RealmBase {
             } else if (role == null) {
                 return false;
             }
-            UserDatabase database = getUserDatabase();
             if (database == null) {
                 return super.hasRole(role);
             }
@@ -303,11 +305,14 @@ public class UserDatabaseRealm extends RealmBase {
             return false;
         }
 
-        private Object writeReplace() {
+        /**
+         * Magic method from {@link java.io.Serializable}.
+         *
+         * @throws ObjectStreamException Not thrown by this implementation
+         */
+        private Object writeReplace() throws ObjectStreamException {
             // Replace with a static principal disconnected from the database
             return new GenericPrincipal(getName(), null, Arrays.asList(getRoles()));
         }
-
     }
-
 }
diff --git a/test/org/apache/catalina/users/MemoryUserDatabaseTests.java b/test/org/apache/catalina/users/MemoryUserDatabaseTests.java
index f8e168f..5724ac7 100644
--- a/test/org/apache/catalina/users/MemoryUserDatabaseTests.java
+++ b/test/org/apache/catalina/users/MemoryUserDatabaseTests.java
@@ -189,9 +189,8 @@ public class MemoryUserDatabaseTests {
     @Test
     public void testSerializePrincipal()
         throws Exception {
-        UserDatabaseRealm realm = new UserDatabaseRealm();
         User user = db.findUser("admin");
-        GenericPrincipal gpIn = realm.new UserDatabasePrincipal(user);
+        GenericPrincipal gpIn = new UserDatabaseRealm.UserDatabasePrincipal(user, db);
         ByteArrayOutputStream bos = new ByteArrayOutputStream();
         ObjectOutputStream oos = new ObjectOutputStream(bos);
         oos.writeObject(gpIn);
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index 83e8174..923bc60 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -111,6 +111,10 @@
         Refactor the <code>RemoteIpValve</code> to use the common utility method
         for list to comma separated string conversion. (markt)
       </scode>
+      <fix>
+        Fix serialization warnings in <code>UserDatabasePrincipal</code>
+        reported by SpotBugs. (markt)
+      </fix>
     </changelog>
   </subsection>
   <subsection name="Coyote">

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org