【问题标题】:Grails Spring Security Core Create New UserGrails Spring Security Core 创建新用户
【发布时间】:2012-07-26 05:57:45
【问题描述】:

为 grails 2.1.0 安装最新的 spring-security-core 插件,启动 s2-quickstart com.myapp 用户角色,它创建用户角色和用户角色域,然后生成所有 com.myapp.User 和 com.myapp。构建基本 CRUD 的角色。现在我正在尝试使用 User create() 方法创建一个“注册”按钮以允许新用户登录,问题是在创建新用户时,这个没有角色,我正在尝试自动- 将“ROLE_USER”分配给通过“注册”按钮和表单注册的每个新用户。

欢迎任何帮助或建议。

【问题讨论】:

    标签: authentication grails spring-security grails-plugin


    【解决方案1】:

    检查用户创建成功后在控制器动作中添加:

    def roleUser = Role.findByName('ROLE_USER')
    UserRole.create user, roleUser, true
    

    【讨论】:

    • 目前,我通过 view.gps 从用户控制器的默认创建方法提供注册,使用 那么如何和在哪里捕获用户创建以及我应该在哪里拥有新的 UserRole?
    • 听起来您正在使用动态脚手架 - 切换到静态(使用 generate-controllergenerate-all 脚本)并编辑 create 控制器操作。
    • 注意,我必须使用Role.findByAuthority('ROLE_USER') 而不是Role.findByName('ROLE_USER')
    【解决方案2】:

    我一直在我的一个应用程序中做这样的事情。我用过

    生成所有 mypackage.User

    User 是扩展 SecUser(由 Spring Security Core 创建的域对象)的类。 我将此代码添加到 UserController:

    def springSecurityService // injection of Spring Security
    
    def create() {
        def user = springSecurityService.getPrincipal() 
        [userInstance: new User(params)]
    }
    
    def save() {
        def map=params
        def userInstance = new User(params)
        if (!userInstance.save(flush: true)) {
            render(view: "create", model: [userInstance: userInstance])
            return
        }
    
        if (!userInstance.authorities.contains(SecRole.findByAuthority(params.role))) {
            SecUserSecRole.create userInstance, SecRole.findByAuthority(params.role)
        }
    
        flash.message = message(code: 'default.created.message', args: [message(code: 'user.label', default: 'User'), userInstance.id])
        redirect(action: "show", id: userInstance.id)
    }
    

    在 BootStrap 类中我做了一些角色:

    def springSecurityService
    
    def init = { servletContext ->
    
        def userRole = SecRole.findByAuthority('ROLE_USER') ?: new SecRole(authority: 'ROLE_USER').save(failOnError: true)
        def adminRole = SecRole.findByAuthority('ROLE_ADMIN') ?: new SecRole(authority: 'ROLE_ADMIN').save(failOnError: true)
        def moderatorRole = SecRole.findByAuthority('ROLE_MODERATOR') ?: new SecRole(authority: 'ROLE_MODERATOR').save(failOnError: true)
    }
    

    在 user/_form.gsp 模板中,我添加了这个选择来选择角色并将其发送到 params

    <sec:ifAllGranted roles="ROLE_ADMIN">
    <g:select name="role" from="${['ROLE_USER', 'ROLE_MODERATOR', 'ROLE_ADMIN']}" value='ROLE_MODERATOR'/>
    </sec:ifAllGranted>
    
    <sec:ifNotLoggedIn>
    <div class="hidden"><g:textField name="role" value="ROLE_USER"/></div>
    </sec:ifNotLoggedIn>
    

    【讨论】:

      【解决方案3】:

      您可以使用以下代码:
      检查用户创建成功后在控制器动作中添加:

      def userr = user.save flush:true
      
      UserRole.create userr, roleUser.findByAuthority('ROLE_USER'), true
      

      【讨论】:

        猜你喜欢
        • 2015-03-27
        • 2012-09-15
        • 2012-06-08
        • 2012-06-02
        • 2019-05-09
        • 2011-10-31
        • 2017-06-20
        • 2011-09-12
        • 2011-04-22
        相关资源
        最近更新 更多