2011-08-10 73 views
1

我已经破解了我的头,最近试图找到方法允许在spring-security-core grails插件中使用密码的特殊字符。 任何人都可以向我暗示在哪里看?当我安装spring-security-core,spring-security-ui grails插件并试图注册提供简单密码的用户时,包含[\ w \ d] {,8}我收到错误: Password must have at least one letter, number, and special character: [email protected]#$%^&SpringSecurity密码验证(密码中的特殊字符)

+2

你能说清楚吗?核心插件中的密码没有限制,除了您可以自由编辑的生成的User类中的建议约束。 –

+1

限制来自注册命令对象,其中包含一个密码验证器约束。改变你的想法。 – hitty5

回答

2

类似的问题产生:http://jira.grails.org/browse/GPSPRINGSECURITYUI-25 更好的解决办法是:

class RegisterController extends grails.plugins.springsecurity.ui.RegisterController { 
def index = { 
    [command: new RegisterCommand()] 
} 

static final myPasswordValidator = { String password, command -> 
     if (command.username && command.username.equals(password)) { 
     return 'command.password.error.username' 
    } 

    if (password && password.length() >= 8 && password.length() <= 64 && 
      (!password.matches('^.*\\p{Alpha}.*$') || 
       !password.matches('^.*\\p{Digit}.*$') 
      )) { 
     return 'command.password.error.strength' 
    } 
} 
} 

     class RegisterCommand { 

String username 
String email 
String password 
String password2 

static constraints = { 
    username blank: false, validator: { value, command -> 
    if (value) { 
    def User = AH.application.getDomainClass(
                SpringSecurityUtils.securityConfig.userLookup.userDomainClassName).clazz 
      if (User.findByUsername(value)) { 
       return 'registerCommand.username.unique' 
      } 
     } 
} 
    email blank: false, email: true, validator: { value, command ->                                                   
      if (value) { 
      def User = AH.application.getDomainClass(
                SpringSecurityUtils.securityConfig.userLookup.userDomainClassName).clazz 
      if (User.findByEmail(value)) { 
       return 'registerCommand.email.unique' 
    } 
     }  
} 
    password blank: false, minSize: 8, maxSize: 64, validator: RegisterController.myPasswordValidator 
password2 validator: RegisterController.password2Validator 
} 
} 



class ResetPasswordCommand { 
String username 
String password 
String password2 

static constraints = { 
password blank: false, minSize: 8, maxSize: 64, validator: RegisterController.myPasswordValidator 
password2 validator: RegisterController.password2Validator 
} 
} 
+0

嗨,除了定义一个定制的验证器,为什么这是一个更好的解决方案?谢谢,吉姆。 –

2

我想你应该创建扩展Spring安全UI一个自己RegisterController作为记录在这里:http://burtbeckwith.github.com/grails-spring-security-ui/docs/manual/guide/10%20Customization.html

grails s2ui-override Register com.my.package 

,然后重写passwordValidator方法与你想要的东西替换验证方法:

if (password && password.length() >= 8 && password.length() <= 64 && 
    (!password.matches('^.*\\p{Alpha}.*$') || 
    !password.matches('^.*\\p{Digit}.*$') || { 
    return 'command.password.error.strength' 
} 

如果你不想特殊字符的密码(?!)

吉姆强制执行。

1

什么做这样的事情?

添加到您的Config.groovy:

grails.plugin.springsecurity.ui.password.validationRegex='^([[email protected]*#_%])$' 
grails.plugin.springsecurity.ui.password.minLength=8 
grails.plugin.springsecurity.ui.password.maxLength=64 
0

您可以设置在Config.groovy中的参数。这些将覆盖默认参数。因此,例如:

grails.plugin.springsecurity.ui.password.validationRegex='^([[email protected]*#_%])*$' 
grails.plugin.springsecurity.ui.password.minLength=8 
grails.plugin.springsecurity.ui.password.maxLength=64 

这类似于但杰夫以前提供的答案,在他qnswer,有缺少一个星号,这意味着它只会匹配一个字符串。

(我不能添加这个作为评论,因为我没有足够的声望)