grails spring安全自定义用户注册页面

时间:2016-03-22 09:29:59

标签: grails spring-security

我正在尝试创建一个使用Spring安全核心2.0的应用,并尝试创建自定义注册页面。我认为我的问题出在我的register.gsp中,因为每当我尝试注册一个新用户时,它都会重定向到http://localhost:8080/home/register,这是完全空白的。可能存在这种情况的进一步证据是我在我的注册操作中包含了几个println()s,这些注册操作应该根据注册失败的位置触发,但是它们都不会随时触发。此外,dbconsole中没有创建用户。请看看我的gsp:

<html>
<head>
    <meta name="layout" content="front"/>
    <title>MSK | Register</title>
    <style>
        ...
    </style>
</head>
<body>
    <div class="jumbotron">
        <form action="${postUrl ?: '/home/register'}" method="POST" id="registerForm" class="cssform" autocomplete="off">
            <h2 class="form-signin-heading">Please register</h2>

            <label for="username" class="sr-only">Username</label>
            <input style="margin-bottom: 10px" type="text" id="username" class="form-control" placeholder="Username" name="${usernameParameter ?: 'j_username'}"/>

            <label for="email" class="sr-only">Email</label>
            <input style="margin-bottom: 10px" type="email" id="email" class="form-control" placeholder="Email" name="${emailParameter ?: 'email'}"/>

            <label for="fName" class="sr-only">First Name</label>
            <input style="margin-bottom: 10px" type="text" id="FName" class="form-control" placeholder="First Name" name="${fNameParameter ?: 'fName'}"/>

            <label for="lName" class="sr-only">Last Name</label>
            <input style="margin-bottom: 10px" type="text" id="lName" class="form-control" placeholder="Last Name" name="${lNameParameter ?: 'lName'}"/>

            <label for="password" class="sr-only">Password</label>
            <input style="margin-bottom: 10px" type="password" id="password" class="form-control" placeholder="Password" name="${passwordParameter ?: 'j_password'}"/>

            <label for="confirmPassword" class="sr-only">Confirm Password</label>
            <input style="margin-bottom: 10px" type="password" id="confirmPassword" class="form-control" placeholder="Confirm Password" name="${passwordCParameter ?: 'confirmPassword'}"/>

            <g:actionSubmit style="margin-top: 20px" class="btn btn-lg btn-success btn-block" action="register" value="create"/>
            <button style="margin-top: 20px" class="btn btn-lg btn-success btn-block" type="submit">Register</button>
        </form>
    </div>
</body>

这是控制器和动作,如果有帮助:

package com.mypackage
class HomeController {
    def springSecurityService

    //many actions

    def register(){
        switch(request.getMethod()){
        case 'POST':
            withForm {
                if(params.username && params.email && params.fName && params.lName && params.password && params.confirmPassword){
                    if(params.password == params.confirmPassword){
                        def userRole = new Role(authority: 'ROLE_USER', name: 'User').save(flush: true)
                        def newUser = User.findByUsername(params.id)
                        if(!newUser){
                            newUser.username = params.username
                            newUser.email = params.email
                            newUser.fName = params.fName
                            newUser.lName = params.lName
                            newUser.password = params.password
                            newUser.enabled = true
                            newUser.accountExpired = false
                            newUser.accountLocked = false
                            newUser.passwordExpired = false
                            newUser.authorities ?: userRole
                            if(loginService.register(newUser)){
                                flash.message = "Your account has been created. Welcome " + newUser.fName
                                flash.message_type = 'success'
                                redirect(controller: 'login', action: 'auth')
                            }else{
                                flash.message = "Unable to create your account. Please try again later"
                                flash.message_type = 'danger'
                                redirect(action: 'register')
                                println("saving user")
                            }
                        }else{
                            flash.message = "That username has already been taken. Please choose a new username."
                            flash.message_type = 'danger'
                            println("checking username")
                        }
                    }else{
                        flash.message = "Your passwords don't match. Please try again"
                        flash.message_type = 'danger'
                        println("matching passwords")
                    }
                }else {
                    flash.message = "Please fill out all required fields"
                    flash.message_type = 'danger'
                    println("filling out fields")
                }
            }
            break;
        case 'GET':
            break;
            println("POSTing form")
        }
    }

这里也是loginService的代码:

com.mypackage
//imports
@Transactional
class LoginService {
    def register(newUser){
        try{
            def userRole = new Role(authority: 'ROLE_USER', name: 'User').save(flush: true)
            newUser.save(flush: true, failOnError: true)
            UserRole.create(newUser, userRole, true)

            return true

        }catch(all){
            all.printStackTrace()
            return false
        }
    }
}

如果所有代码都没有正确缩进,我很抱歉,我基本上复制并粘贴了我的实际项目。任何帮助将不胜感激,谢谢!

1 个答案:

答案 0 :(得分:0)

  • 您可以使用spring-security-ui插件
  • 使用命令对象,验证密码
  • 使用资源,这意味着向/ user发帖,将调用保存操作