如何使用新的普通vanilla Grails 3.0應用程序將用戶存儲在數據庫中?使用Grails 3.0將Spring Boot用戶存儲在數據庫中
背景:
- 四郎和Spring安全插件尚未公佈Grails的3.0(這聽起來像春天的引導是未來Grails的安全性)。
- 有很多示例顯示如何使用
inMemoryAuthentication()
,但它們看起來完全沒有意義,因爲密碼最終以純文本形式存儲(此外,在Grails中只需花費大約30秒的時間來創建域模型)。 - 幾乎所有Grails應用程序都需要此功能。
- 我碰巧在使用MongoDB,但這可能是無關緊要的。
- 相關:Grails 3 and Spring Security Plugin
我現在有inMemoryAuthentication()
具有以下工作:
的build.gradle
compile "org.springframework.boot:spring-boot-starter-security"
的grails-app/conf目錄/春/資源。 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中。
此解決方案可能是期望的某些情況下(特別是更成熟的應用),但大多數開發商不會想報名參加這種服務只是爲了創造自己的初步應用。對於那些誰想要這個解決方案,這裏的計劃:https://開頭stormpath。com/pricing/ –
其實我認爲Stormpath是簡單應用程序的一個很好的起點。 Stormpath不僅從頭開始使用非常簡單,而且如果每月不超過10萬次API調用,它也將完全免費 – mario