您如何使用新的普通 Grails 3.0 应用程序将用户存储在数据库中?
背景:
- Shiro 和 Spring Security 插件还不能用于 Grails 3.0(听起来 Spring Boot 是 Grails 安全性的未来)。
- 有各种各样的例子展示了如何使用
inMemoryAuthentication()
,但它们似乎完全没有意义,因为密码最终以纯文本形式存储(此外,在 Grails 中创建域模型只需要大约 30 秒的时间)。 - 几乎所有 Grails 应用程序都需要这个功能。
- 我碰巧在使用 MongoDB,但这可能无关紧要。
- 相关:Grails 3 和 Spring Security 插件
我目前inMemoryAuthentication()
正在处理以下内容:
构建.gradle
compile "org.springframework.boot:spring-boot-starter-security"
grails-app/conf/spring/resources.groovy
import com.tincanworks.AppSecurityConfig
beans = {
webSecurityConfiguration(AppSecurityConfig)
}
AppSecurityConfig.groovy
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder
import org.springframework.security.config.annotation.web.builders.HttpSecurity
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter
class AppSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/assets/**").permitAll()
.antMatchers("/admin/**").hasAnyRole("admin")
.anyRequest().authenticated()
.and()
.formLogin().permitAll()
.and()
.logout().permitAll()
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("user").password("123456").roles("user")
.and()
.withUser("admin").password("1qaz2wsx").roles("user", "admin")
}
}
似乎答案可能与JdbcDaoImpl有关,但我不知道如何在 Grails 中连接它。