2015-05-07 122 views
3

如何使用新的普通vanilla Grails 3.0應用程序將用戶存儲在數據庫中?使用Grails 3.0將Spring Boot用戶存儲在數據庫中

背景:

  1. 四郎和Spring安全插件尚未公佈Grails的3.0(這聽起來像春天的引導是未來Grails的安全性)。
  2. 有很多示例顯示如何使用inMemoryAuthentication(),但它們看起來完全沒有意義,因爲密碼最終以純文本形式存儲(此外,在Grails中只需花費大約30秒的時間來創建域模型)。
  3. 幾乎所有Grails應用程序都需要此功能。
  4. 我碰巧在使用MongoDB,但這可能是無關緊要的。
  5. 相關: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中。

回答

2

格姆基於

我寫了兩個博客文章介紹如何使用彈簧起動器的安全性和GORM在一個Grails 3應用程序(part 1 - In Memory Authpart 2 - Gorm-based Auth)。我還創建了用彈簧起動安全github repo with a working Grails 3應用。

基於JDBC - 未經檢驗

另外,如果你想使用標準的基於JDBC的認證,你可以使用下面的SQL腳本

剛剛創建數據庫表HSQLDB

http://docs.spring.io/spring-security/site/docs/3.0.x/reference/appendix-schema.html

create table users(
     username varchar_ignorecase(50) not null primary key, 
     password varchar_ignorecase(50) not null, 
     enabled boolean not null); 

    create table authorities (
     username varchar_ignorecase(50) not null, 
     authority varchar_ignorecase(50) not null, 
     constraint fk_authorities_users foreign key(username) references users(username)); 
     create unique index ix_auth_username on authorities (username,authority); 

的MySQL

這來自http://justinrodenbostel.com/2014/05/30/part-5-integrating-spring-security-with-spring-boot-web/

create table users (
    username varchar(50) not null primary key, 
    password varchar(255) not null, 
    enabled boolean not null) engine = InnoDb; 

create table authorities (
    username varchar(50) not null, 
    authority varchar(50) not null, 
    foreign key (username) references users (username), 
    unique index authorities_idx_1 (username, authority)) engine = InnoDb; 

,然後改變configureGlobal方法

@Autowired //not sure if this is needed as you have the AppSecurityConfig bean referenced in resources.groovy 
def datasource //bean injected by Grails 

@Autowired 
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 
    auth 
    .jdbcAuthentication() 
    .dataSource(datasource) 
} 
0

如果你想避免用一個數據庫從頭開始構建一個完整的用戶管理層,你可以考慮Stormpath

其中,它們提供Spring Security Plugin,它使用Stormpath作爲身份驗證和授權提供程序。他們也有一個sample Spring Security app顯示如何使用插件。由於您使用的是Java註釋(而不是xml配置),請參閱this branch

因此,簡言之,你需要定義的關鍵部分是這些:

  1. 的Stormpath客戶端Bean將通過Stormpath Java SDK提供Stormpath快速和安全的通信:

    //Let's create the Stormpath client using the apiKey.properties file from the User's home folder. 
    @Bean 
    ClientFactory stormpathClient(CacheManager cacheManager) { 
        ClientFactory clientFactory = new ClientFactory(); 
        clientFactory.setApiKeyFileLocation(System.getProperty("user.home") + File.separator + ".stormpath" + File.separator + "apiKey.properties"); 
        clientFactory.setCacheManager(cacheManager); 
        return clientFactory; 
    } 
    
  2. 您需要定義Stormpath驗證提供商所以Spring Security可以透明地Stormpath通信,以便認證和授權用戶:

    @Bean 
    @Autowired 
    public StormpathAuthenticationProvider stormpathAuthenticationProvider(Client client, String applicationRestUrl) throws Exception { 
        StormpathAuthenticationProvider stormpathAuthenticationProvider = new StormpathAuthenticationProvider(); 
        stormpathAuthenticationProvider.setClient(client); 
        stormpathAuthenticationProvider.setApplicationRestUrl(applicationRestUrl); 
        return stormpathAuthenticationProvider; 
    } 
    
  3. applicationRestUrl需要指向Stormpath應用中的所有用戶/組將存在:

    @Bean 
    public String getApplicationRestUrl() { 
        return "https://api.stormpath.com/v1/applications/9TqbyZ2po73eDP4gYo2H92"; 
    } 
    
  4. 你的春季安全配置需要被配置爲使用Stormpath驗證提供:

    //Let's add the StormpathAuthenticationProvider to the `AuthenticationProvider` 
    @Override 
    protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
        auth.authenticationProvider(stormpathAuthenticationProvider); 
    } 
    
  5. 最後,爲了限制通過角色訪問資源,你需要定義的角色。例如:

    //The access control settings are defined here 
    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
        http 
         .formLogin() 
          .and() 
         .authorizeRequests() 
          .accessDecisionManager(accessDecisionManager()) 
          .antMatchers("/account/*").hasAuthority("https://api.stormpath.com/v1/groups/36O9eBTN2oLtjoMSWLdnwL") //you are giving access to "/account/*" to users' that belong to the group univocally identified by this href value 
          .and() 
         .logout() 
          .logoutUrl("/logout") 
          .logoutSuccessUrl("/index.jsp") 
          .and() 
         .httpBasic() 
         .and() 
         .csrf().disable(); 
    } 
    

聲明,我是一個積極Stormpath貢獻者。

+1

此解決方案可能是期望的某些情況下(特別是更成熟的應用),但大多數開發商不會想報名參加這種服務只是爲了創造自己的初步應用。對於那些誰想要這個解決方案,這裏的計劃:https://開頭stormpath。com/pricing/ –

+0

其實我認爲Stormpath是簡單應用程序的一個很好的起點。 Stormpath不僅從頭開始使用非常簡單,而且如果每月不超過10萬次API調用,它也將完全免費 – mario

相關問題