File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\security\NotSerilizableSecurityContext.java.html

1 /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 package hudson.security;
16
17 import org.acegisecurity.context.SecurityContext;
18 import org.acegisecurity.context.SecurityContextImpl;
19 import org.acegisecurity.Authentication;
20 import org.acegisecurity.userdetails.UserDetails;
21
22 import javax.servlet.http.HttpSession;
23
24 /**
25 * The same as {@link SecurityContextImpl} but doesn't serialize {@link Authentication}.
26 *
27 * <p>
28 * {@link Authentication} often contains {@link UserDetails} implemented by a plugin,
29 * but when it's persisted as a part of {@link HttpSession}, such instance will never
30 * de-serialize correctly because the container isn't aware of additional classloading
31 * in Hudson.
32 *
33 * <p>
34 * Jenkins doesn't work with a clustering anyway, and so it's better to just not persist
35 * Authentication at all.
36 *
37 * See http://www.nabble.com/ActiveDirectory-Plugin%3A-ClassNotFoundException-while-loading--persisted-sessions%3A-td22085140.html
38 * for the problem report.
39 *
40 * @author Kohsuke Kawaguchi
41 * @see HttpSessionContextIntegrationFilter2
42 */
43 public class NotSerilizableSecurityContext implements SecurityContext {
44 private transient Authentication authentication;
45
46 public NotSerilizableSecurityContext() {
47 }
48
49 public NotSerilizableSecurityContext(Authentication authentication) {
50 this.authentication = authentication;
51 }
52
53 @Override
54 public boolean equals(Object obj) {
55 if (obj instanceof SecurityContext) {
56 SecurityContext test = (SecurityContext) obj;
57
58 if ((this.getAuthentication() == null) && (test.getAuthentication() == null)) {
59 return true;
60 }
61
62 if ((this.getAuthentication() != null) && (test.getAuthentication() != null)
63 && this.getAuthentication().equals(test.getAuthentication())) {
64 return true;
65 }
66 }
67
68 return false;
69 }
70
71 public Authentication getAuthentication() {
72 return authentication;
73 }
74
75 @Override
76 public int hashCode() {
77 if (this.authentication == null) {
78 return -1;
79 } else {
80 return this.authentication.hashCode();
81 }
82 }
83
84 public void setAuthentication(Authentication authentication) {
85 this.authentication = authentication;
86 }
87
88 @Override
89 public String toString() {
90 StringBuilder sb = new StringBuilder();
91 sb.append(super.toString());
92
93 if (this.authentication == null) {
94 sb.append(": Null authentication");
95 } else {
96 sb.append(": Authentication: ").append(this.authentication);
97 }
98
99 return sb.toString();
100 }
101 }
102