#126 fix EqualsBuilder.reflectionEquals(Object lhs, Object rhs, String... excludeFields) 的严重误用

This commit is contained in:
Calvin
2012-12-05 00:49:00 +08:00
parent 07f77d85f7
commit c92d2691b0
2 changed files with 36 additions and 12 deletions
@@ -22,8 +22,6 @@ import java.io.Serializable;
import javax.annotation.PostConstruct;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
@@ -39,6 +37,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springside.examples.quickstart.entity.User;
import org.springside.modules.utils.Encodes;
import com.google.common.base.Objects;
public class ShiroDbRealm extends AuthorizingRealm {
protected AccountService accountService;
@@ -115,19 +115,31 @@ public class ShiroDbRealm extends AuthorizingRealm {
}
/**
* 重载equals,只计算loginName;
* 重载hashCode,只计算loginName;
*/
@Override
public int hashCode() {
return HashCodeBuilder.reflectionHashCode(this, "loginName");
return Objects.hashCode(loginName);
}
/**
* 重载equals,只比较loginName
* 重载equals,只计算loginName;
*/
@Override
public boolean equals(Object obj) {
return EqualsBuilder.reflectionEquals(this, obj, "loginName");
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ShiroUser other = (ShiroUser) obj;
if (loginName == null) {
if (other.loginName != null)
return false;
} else if (!loginName.equals(other.loginName))
return false;
return true;
}
}
}
@@ -22,8 +22,6 @@ import java.io.Serializable;
import javax.annotation.PostConstruct;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
@@ -41,6 +39,8 @@ import org.springside.examples.showcase.entity.Role;
import org.springside.examples.showcase.entity.User;
import org.springside.modules.utils.Encodes;
import com.google.common.base.Objects;
public class ShiroDbRealm extends AuthorizingRealm {
protected AccountService accountService;
@@ -125,19 +125,31 @@ public class ShiroDbRealm extends AuthorizingRealm {
}
/**
* 重载equals,只计算loginName;
* 重载hashCode,只计算loginName;
*/
@Override
public int hashCode() {
return HashCodeBuilder.reflectionHashCode(this, "loginName");
return Objects.hashCode(loginName);
}
/**
* 重载equals,只比较loginName
* 重载equals,只计算loginName;
*/
@Override
public boolean equals(Object obj) {
return EqualsBuilder.reflectionEquals(this, obj, "loginName");
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ShiroUser other = (ShiroUser) obj;
if (loginName == null) {
if (other.loginName != null)
return false;
} else if (!loginName.equals(other.loginName))
return false;
return true;
}
}
}