Grails Spring Security默认配置:如何定义默认用户和密码

时间:2015-10-05 16:02:46

标签: grails spring-security

在纯粹的春天,我可以这样做

<security:authentication-manager>
<security:authentication-provider user-service-ref="customUserDetailsService">       
    <security:password-encoder ref="passwordEncoder"/>
</security:authentication-provider>
<security:authentication-provider>
    <security:user-service>
        <security:user name="admin" password="admin" authorities="ROLE_USER, ROLE_ADMIN" />
    </security:user-service>

使用Spring Security插件在Grails中是否可以采用相同的方法?

我可以实现自己的身份验证提供,但在这种情况下,我将存储在课堂上硬编码的凭据。

4 个答案:

答案 0 :(得分:1)

通常,这是在BootStrap.groovy中完成的,例如

import com.foo.Role
import com.foo.User
import com.foo.UserRole

class BootStrap {

   def init = {
      def userRole = Role.findOrSaveByAuthority('ROLE_USER')
      def adminRole = Role.findOrSaveByAuthority('ROLE_ADMIN')

      if (!User.findByUsername('admin')) {
         def admin = new User(username: 'admin', password: 'admin').save()

         UserRole.create admin, userRole
         UserRole.create admin, adminRole

         User.withSession { it.flush() }
      }
   }
}

答案 1 :(得分:1)

您可以使用InMemoryUserDetailsManager(在resources.groovy中)执行此操作:

import org.springframework.security.provisioning.InMemoryUserDetailsManager
import org.springframework.security.core.userdetails.User
import org.springframework.security.core.authority.SimpleGrantedAuthority

beans = {
    userDetailsService(InMemoryUserDetailsManager,
            [new User('admin', 'password hash', [new SimpleGrantedAuthority('ROLE_ADMIN')])])
}

答案 2 :(得分:0)

This link包含一个示例,您可以在其中利用InMemoryUserDetailsManagerBooStrap.groovy创建用户。

这是我在BootStrap.groovy中所拥有的(在这种情况下,我正在从环境变量中加载密码):

import org.springframework.security.provisioning.UserDetailsManager
import org.springframework.security.core.authority.SimpleGrantedAuthority
import org.springframework.security.core.userdetails.User
import org.springframework.security.core.userdetails.UserDetails

class BootStrap {

    UserDetailsManager userDetailsService

    def init = { servletContext ->

        UserDetails user1 = new User('user1', "${System.getenv("USER1_PWD")}", [new SimpleGrantedAuthority('ROLE_USER')])
        userDetailsService.createUser(user1)

        UserDetails user2 = new User('user2', "${System.getenv("USER2_PWD")}", [new SimpleGrantedAuthority('ROLE_USER')])
        userDetailsService.createUser(user2)

    }
    def destroy = {
    }
}

这是我的resources.groovy

import org.springframework.security.provisioning.InMemoryUserDetailsManager
import org.springframework.security.authentication.encoding.PlaintextPasswordEncoder

beans = {

    userDetailsService(InMemoryUserDetailsManager,[])
    passwordEncoder(PlaintextPasswordEncoder)
}

答案 3 :(得分:-1)

您可以使用grails spring security提供的这些自定义标记。

<sec:ifLoggedIn>
<li><a><sec:username /></a></li>
<li><g:link controller="logout">Logout</g:link></li>
</sec:ifLoggedIn>
<sec:ifNotLoggedIn>
<li><g:link controller='controller' action='action'>Login</g:link></li>
<li><g:link controller="controller" action="action">Signup</g:link></li>
</sec:ifNotLoggedIn>