简体   繁体   English

Grails 2.1.1和Spring Security Core插件

[英]Grails 2.1.1 and Spring Security Core plugin

I've been noticing that a lot of the tutorials I'm following use this: 我一直注意到,我正在关注的许多教程都使用此方法:

def springSecurityService

and since I want to get records only by current logged in user I use: 由于我只想由当前登录用户获取记录,因此我使用:

def user = params.id ? User.findByUserId(params.id) : User.get(springSecurityService.principal.id)

and also in my Bootstrap I want to create a username and password, so for instance 而且我想在Bootstrap中创建用户名和密码,例如

def user = new User( username: username, password: springSecurityService.encodePassword("tops3kr17"), enabled: true)

However I noticed that the password is not being created, and Spring Source Tools does not find the method .principal.id or .encodePassword (they stay underlined in STS) and wants to use SpringSecurityService with a capital S when hitting CTL+SPACE (and doesn't complete .principal.id or .encodePassword). 但是我注意到没有创建密码,并且Spring Source Tools找不到方法.principal.id或.encodePassword(它们在STS中始终带有下划线),并且想在击中CTL + SPACE时将SpringSecurityService与大写S一起使用(和未完成.principal.id或.encodePassword)。

So i'm a little lost because it seems that the tutorials are out of date 所以我有点迷茫,因为这些教程似乎已经过时了

So how can I do what I described with what the current supported methods are? 那么,如何使用当前支持的方法来描述我所描述的呢? Or am I missing something really simple? 还是我错过了一些非常简单的事情? : ) :)

class BootStrap {
def springSecurityService

def init = { servletContext ->

    def demo = [
        'jack' : [ fullName: 'Jack Demo Salesman'],
        'jill' : [ fullName: 'Jill Demo Saleswoman']]

    def now = new Date()
    def random = new Random()

    def userRole = SecRole.findByAuthority("ROLE_SALES") ?: new SecRole(authority: "ROLE_SALES").save()
    def adminRole = SecRole.findByAuthority("ROLE_ADMIN") ?: new SecRole(authority: "ROLE_ADMIN").save()

    def users = User.list() ?: []
    if (!users) {
        demo.each { username, password, userAttrs ->
            def user = new User(
                username: username,
                password: springSecurityService.encodePassword('secret'),
                enabled: true)
            if (user.validate()) {
                println "DEBUG: Creating user ${username}..."
                println "DEBUG: and their password is ${password}"
                user.save(flush:true)

                SecUserSecRole.create user, userRole
                users << user
            }
            else {
                println("\n\n\nError in account bootstrap for ${username}!\n\n\n")
                user.errors.each {err -> 
                    println err
                }
            }

Using the injected instance of SpringSecurityService is the right approach. 使用注入的SpringSecurityService实例是正确的方法。

def springSecurityService

def foo() {
   springSecurityService.principal
   springSecurityService.encodePassword('fdsfads')
   ....
}

If the IDE isn't recognizing it, there is an issue with your IDE. 如果IDE无法识别它,则说明您的IDE存在问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM