SEC-125: Provide hashCode() method for AbstractAuthenticationToken.
This commit is contained in:
parent
a0aef26e21
commit
e5c538d1a5
|
@ -1,4 +1,4 @@
|
||||||
/* Copyright 2004, 2005 Acegi Technology Pty Limited
|
/* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -16,6 +16,7 @@
|
||||||
package org.acegisecurity.providers;
|
package org.acegisecurity.providers;
|
||||||
|
|
||||||
import org.acegisecurity.Authentication;
|
import org.acegisecurity.Authentication;
|
||||||
|
|
||||||
import org.acegisecurity.userdetails.UserDetails;
|
import org.acegisecurity.userdetails.UserDetails;
|
||||||
|
|
||||||
|
|
||||||
|
@ -28,24 +29,6 @@ import org.acegisecurity.userdetails.UserDetails;
|
||||||
public abstract class AbstractAuthenticationToken implements Authentication {
|
public abstract class AbstractAuthenticationToken implements Authentication {
|
||||||
//~ Methods ================================================================
|
//~ Methods ================================================================
|
||||||
|
|
||||||
/**
|
|
||||||
* Subclasses should override if they wish to provide additional details
|
|
||||||
* about the authentication event.
|
|
||||||
*
|
|
||||||
* @return always <code>null</code>
|
|
||||||
*/
|
|
||||||
public Object getDetails() {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
if (this.getPrincipal() instanceof UserDetails) {
|
|
||||||
return ((UserDetails) this.getPrincipal()).getUsername();
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.getPrincipal().toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean equals(Object obj) {
|
public boolean equals(Object obj) {
|
||||||
if (obj instanceof AbstractAuthenticationToken) {
|
if (obj instanceof AbstractAuthenticationToken) {
|
||||||
AbstractAuthenticationToken test = (AbstractAuthenticationToken) obj;
|
AbstractAuthenticationToken test = (AbstractAuthenticationToken) obj;
|
||||||
|
@ -77,6 +60,48 @@ public abstract class AbstractAuthenticationToken implements Authentication {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Subclasses should override if they wish to provide additional details
|
||||||
|
* about the authentication event.
|
||||||
|
*
|
||||||
|
* @return always <code>null</code>
|
||||||
|
*/
|
||||||
|
public Object getDetails() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
if (this.getPrincipal() instanceof UserDetails) {
|
||||||
|
return ((UserDetails) this.getPrincipal()).getUsername();
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.getPrincipal().toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int hashCode() {
|
||||||
|
int code = 2305;
|
||||||
|
|
||||||
|
if (this.getAuthorities() != null) {
|
||||||
|
for (int i = 0; i < this.getAuthorities().length; i++) {
|
||||||
|
code = code * (this.getAuthorities()[i].hashCode() % 7);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.getPrincipal() != null) {
|
||||||
|
code = code * (this.getPrincipal().hashCode() % 7);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.getCredentials() != null) {
|
||||||
|
code = code * (this.getCredentials().hashCode() % 7);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.isAuthenticated()) {
|
||||||
|
code = code * -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return code;
|
||||||
|
}
|
||||||
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
StringBuffer sb = new StringBuffer();
|
StringBuffer sb = new StringBuffer();
|
||||||
sb.append(super.toString()).append(": ");
|
sb.append(super.toString()).append(": ");
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
/* Copyright 2004 Acegi Technology Pty Limited
|
/* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -40,14 +40,14 @@ public class AbstractAuthenticationTokenTests extends TestCase {
|
||||||
|
|
||||||
//~ Methods ================================================================
|
//~ Methods ================================================================
|
||||||
|
|
||||||
public final void setUp() throws Exception {
|
|
||||||
super.setUp();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
junit.textui.TestRunner.run(AbstractAuthenticationTokenTests.class);
|
junit.textui.TestRunner.run(AbstractAuthenticationTokenTests.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public final void setUp() throws Exception {
|
||||||
|
super.setUp();
|
||||||
|
}
|
||||||
|
|
||||||
public void testGetters() throws Exception {
|
public void testGetters() throws Exception {
|
||||||
MockAuthenticationImpl token = new MockAuthenticationImpl("Test",
|
MockAuthenticationImpl token = new MockAuthenticationImpl("Test",
|
||||||
"Password",
|
"Password",
|
||||||
|
@ -58,6 +58,25 @@ public class AbstractAuthenticationTokenTests extends TestCase {
|
||||||
assertEquals("Test", token.getName());
|
assertEquals("Test", token.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testHashCode() throws Exception {
|
||||||
|
MockAuthenticationImpl token1 = new MockAuthenticationImpl("Test",
|
||||||
|
"Password",
|
||||||
|
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
|
||||||
|
"ROLE_TWO")});
|
||||||
|
MockAuthenticationImpl token2 = new MockAuthenticationImpl("Test",
|
||||||
|
"Password",
|
||||||
|
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
|
||||||
|
"ROLE_TWO")});
|
||||||
|
MockAuthenticationImpl token3 = new MockAuthenticationImpl(null, null,
|
||||||
|
new GrantedAuthority[] {});
|
||||||
|
assertEquals(token1.hashCode(), token2.hashCode());
|
||||||
|
assertTrue(token1.hashCode() != token3.hashCode());
|
||||||
|
|
||||||
|
token2.setAuthenticated(true);
|
||||||
|
|
||||||
|
assertTrue(token1.hashCode() != token2.hashCode());
|
||||||
|
}
|
||||||
|
|
||||||
public void testObjectsEquals() throws Exception {
|
public void testObjectsEquals() throws Exception {
|
||||||
MockAuthenticationImpl token1 = new MockAuthenticationImpl("Test",
|
MockAuthenticationImpl token1 = new MockAuthenticationImpl("Test",
|
||||||
"Password",
|
"Password",
|
||||||
|
@ -143,14 +162,6 @@ public class AbstractAuthenticationTokenTests extends TestCase {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAuthenticated(boolean isAuthenticated) {
|
|
||||||
this.authenticated = isAuthenticated;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isAuthenticated() {
|
|
||||||
return this.authenticated;
|
|
||||||
}
|
|
||||||
|
|
||||||
public GrantedAuthority[] getAuthorities() {
|
public GrantedAuthority[] getAuthorities() {
|
||||||
return this.authorities;
|
return this.authorities;
|
||||||
}
|
}
|
||||||
|
@ -162,5 +173,13 @@ public class AbstractAuthenticationTokenTests extends TestCase {
|
||||||
public Object getPrincipal() {
|
public Object getPrincipal() {
|
||||||
return this.principal;
|
return this.principal;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isAuthenticated() {
|
||||||
|
return this.authenticated;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAuthenticated(boolean isAuthenticated) {
|
||||||
|
this.authenticated = isAuthenticated;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue