【发布时间】:2015-10-19 15:51:58
【问题描述】:
我正在使用 Grails Spring 安全插件。我有一个要求,对于一种用户,不需要密码。所以我覆盖了默认用户域中的密码约束:
class SecUser {
...
static constraints = {
username blank: false, unique: true
password blank: false, nullable: true
}
...
}
但这会导致很多问题:
-
用户域中的
beforeInsert中断,因为springSecurityService.encodePassword(password)不能接受空值;然后我覆盖beforeInsert:def beforeInsert() { if (someCondition) { super.beforeInsert() passwordChangeDate = new Date() } } -
UserDetails类中断,因为构造函数不能接受密码作为空值,所以我覆盖了UserDetails:import grails.plugin.springsecurity.userdetails.GrailsUser import org.springframework.security.core.GrantedAuthority import org.springframework.security.core.userdetails.User.AuthorityComparator import org.springframework.util.Assert class ILUserDetails extends GrailsUser { final String name ILUserDetails(String username, String password, boolean enabled, boolean accountNonExpired, boolean credentialsNonExpired, boolean accountNonLocked, Collection<GrantedAuthority> authorities, long id, String name) { //Override User if (((username == null) || "".equals(username))) { // Allow null for password throw new IllegalArgumentException( "Cannot pass null or empty values to constructor"); } this.username = username; this.password = password; this.enabled = enabled; this.accountNonExpired = accountNonExpired; this.credentialsNonExpired = credentialsNonExpired; this.accountNonLocked = accountNonLocked; this.authorities = Collections.unmodifiableSet(sortAuthorities(authorities)); this.id=id this.name = name } private static SortedSet<GrantedAuthority> sortAuthorities( Collection<? extends GrantedAuthority> authorities) { Assert.notNull(authorities, "Cannot pass a null GrantedAuthority collection"); // Ensure array iteration order is predictable (as per // UserDetails.getAuthorities() contract and SEC-717) SortedSet<GrantedAuthority> sortedAuthorities = new TreeSet<GrantedAuthority>( new AuthorityComparator()); for (GrantedAuthority grantedAuthority : authorities) { Assert.notNull(grantedAuthority, "GrantedAuthority list cannot contain any null elements"); sortedAuthorities.add(grantedAuthority); } return sortedAuthorities; } } -
然后我覆盖
beforeInsert和UserDetails,一个奇怪的bug发生了:java.lang.NoSuchMethodError: grails.plugin.springsecurity.userdetails.GrailsUser: method <init>()V not found com.app.security.ILUserDetails.<init>(ILUserDetails.groovy) sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) java.lang.reflect.Constructor.newInstance(Constructor.java:526) org.springsource.loaded.ri.ReflectiveInterceptor.jlrConstructorNewInstance(ReflectiveInterceptor.java:986) org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:77) org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrapNoCoerce.callConstructor(ConstructorSite.java:102) org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:57)
现在,我不确定让密码可以为空是否是个好主意。有人能给我一些建议吗?
【问题讨论】: