2016-07-12 19 views
0

に方法loadUserByToken()を呼び出すことはできません私はGrailsのフレームワークの中で春のセキュリティプラグイン(2.0.0)と春認証休憩プラグイン(1.5.3)を使用して、トークンベースの認証を実装しようとしています(2.5 .0)。私は、ヘッダーフィールド "x-auth-token"をトークンに設定し、ターゲットコントローラのURLにポストします。しかし、IDE(のIntelliJ IDEAは)私はこのloadUserByToken()メソッドをチェックしGrailsの、春のセキュリティ残りのプラグイン、ヌルオブジェクト

| Error 2016-07-12 15:58:27,864 [http-bio-8080-exec-10] ERROR [/hello_world]. 
[default] - Servlet.service() for servlet [default] in context with path [/hello_world] threw exception 
Message: Cannot invoke method loadUserByToken() on null object 
Line | Method 
->> 55 | authenticate in grails.plugin.springsecurity.rest.RestAuthenticationProvider 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|  75 | doFilter  in grails.plugin.springsecurity.rest.RestTokenValidationFilter 
|  53 | doFilter . . in grails.plugin.springsecurity.web.filter.GrailsAnonymousAuthenticationFilter 
| 143 | doFilter  in grails.plugin.springsecurity.rest.RestAuthenticationFilter 
|  62 | doFilter . . in grails.plugin.springsecurity.web.authentication.logout.MutableLogoutFilter 
|  80 | doFilter  in grails.plugin.springsecurity.rest.RestLogoutFilter 
|  59 | doFilter . . in grails.plugin.springsecurity.web.SecurityRequestHolderFilter 
|  82 | doFilter  in com.brandseye.cors.CorsFilter 
| 1142 | runWorker . in java.util.concurrent.ThreadPoolExecutor 
| 617 | run   in java.util.concurrent.ThreadPoolExecutor$Worker 
^ 745 | run . . . . in java.lang.Thread 

このエラーメッセージを飛び出し、それがtokenStorageServiceに呼び出されます。なぜこのtokenStorageServiceがnullオブジェクトであるのかわかりません。次のように春のセキュリティと春のセキュリティプラグインが設定されています

Config.groovy

// Added by the Spring Security Core plugin: 
grails.plugin.springsecurity.userLookup.userDomainClassName = 'hello_world.User' 
grails.plugin.springsecurity.userLookup.authorityJoinClassName = 'hello_world.UserRole' 
grails.plugin.springsecurity.authority.className = 'hello_world.Role' 
grails.plugin.springsecurity.controllerAnnotations.staticRules = [ 
    '/':    ['permitAll'], 
    '/index':   ['permitAll'], 
    '/index.gsp':  ['permitAll'], 
    '/assets/**':  ['permitAll'], 
    '/**/js/**':  ['permitAll'], 
    '/**/css/**':  ['permitAll'], 
    '/**/images/**': ['permitAll'], 
    '/**/favicon.ico': ['permitAll'], 
    '/api/login':  ['permitAll'] 
] 

grails { 
    plugin { 
     springsecurity { 

      filterChain.chainMap = [ 
        '/api/guest/**': 'anonymousAuthenticationFilter,restTokenValidationFilter,restExceptionTranslationFilter,filterInvocationInterceptor', 
        '/api/**': 'JOINED_FILTERS,-exceptionTranslationFilter,-authenticationProcessingFilter,-securityContextPersistenceFilter,-rememberMeAuthenticationFilter', // Stateless chain 
        '/**': 'JOINED_FILTERS,-restTokenValidationFilter,-restExceptionTranslationFilter'                   // Traditional chain 
      ] 

      providerNames = ['restAuthenticationProvider','daoAuthenticationProvider', 'rememberMeAuthenticationProvider'] 

      auth.loginFormUrl = '/login/auth' 

      useSecurityEventListener = true 

      onAuthenticationSuccessEvent = { e, appCtx -> 
       // handle AuthenticationSuccessEvent 

       System.out.println("Authentication Succeeded"); 
      } 

      onAuthenticationSwitchUserEvent = { e, appCtx -> 
       // handle AuthenticationSwitchUserEvent 
      } 

      onAuthorizationEvent = { e, appCtx -> 
       // handle AuthorizationEvent 
      } 

      onRestTokenCreationEvent = { e, appCtx -> 

       System.out.println("Token Created") 
      } 

      apf { 
       filterProcessesUrl = '/api/login' 
       allowSessionCreation = false 
//    usernamePropertyName = 'username' 
//    passwordPropertyName = 'password' 
      } 

      rest { 

       active = true 

       login { 
        active = true 
        endpointUrl = '/api/login' 
        failureStatusCode = 401 
        useJsonCredentials = true 
        usernamePropertyName = 'username' 
        passwordPropertyName = 'password' 
       } 

       token { 

        validation { 
         active = true 
         endpointUrl = '/api/validate' 
         headerName = 'x-auth-token' 
         useBearerToken = false 
         tokenPropertyName = 'access_token' 
         enableAnonymousAccess = true 
        } 

        generation { 
         active = true 
         useSecureRandom = true 
         useUUID = false 
        } 

        rendering { 
         usernamePropertyName = 'username' 
         authoritiesPropertyName = 'roles' 
         tokenPropertyName = 'token' 
        } 

        storage { 
         active = true 
         useGorm = true 

         gorm { 
          tokenDomainClassName = 'hello_world.AuthenticationToken' 
          tokenValuePropertyName = 'tokenValue' 
          usernamePropertyName = 'username' 
         } 
        } 
       } 
      } 
     } 
    } 
} 

resources.groovy

import grails.plugin.springsecurity.rest.RestAuthenticationProvider 
beans = { 
    restAuthenticationProvider(RestAuthenticationProvider); 
} 

と私はトークンがauthentication_tokenテーブルに格納され、データベースをチェックしています。私はgrailsには新しく、時間を探していて何の手がかりもしていません。誰でも助けてくれますか?とても有難い。

他に必要な場合は、お知らせください。

答えて

1

同じ問題を抱えている人のために、いくつかの試行の後、私はこれを最終的に考え出しました。私はresources.groovyでrestAuthenticationProviderを宣言すべきではないと思われ、config.groovyのgrails.plugin.springsecurity.providerNamesにrestAuthenticationProviderを追加しないでください。以下のように、ばねセキュリティコアとバネのセキュリティ・休息のための完全なコンフィグが記載されています:

config.groovy

grails { 
    plugin { 
     springsecurity { 

      useSecurityEventListener = true 

      filterChain { 
       chainMap = [ 
         '/api/**': 'JOINED_FILTERS,-exceptionTranslationFilter,-authenticationProcessingFilter,-securityContextPersistenceFilter,-rememberMeAuthenticationFilter', // Stateless chain 
         '/**': 'JOINED_FILTERS,-restTokenValidationFilter,-restExceptionTranslationFilter'                   // Traditional chain 
       ] 
      } //filterChain 

      apf { 
       filterProcessesUrl = '/api/login' 
      } //apf 

      rest { 

       login { 
        active = true 
        useRequestParamsCredentials = false 
        useJsonCredentials = true 
        usernamePropertyName = 'j_username' 
        passwordPropertyName = 'j_password' 
        endpointUrl = '/api/login' 
       } //login 

       logout { 

       } //logout 

       token { 

        validation { 
         active = true 
         endpointUrl = '/api/validate' 
         useBearerToken = false 
         headername = 'X-Auth-Token' 
        } //validation 

        generation { 
//      active = true 
//      useSecureRandom = true; 
//      useUUID = false; 
        } 

        rendering { 
         usernamePropertyName = 'username' 
         authoritiesPropertyName = 'roles' 
         tokenPropertyName = 'token' 
        } 

        storage { 
//      useJWT = true; 
        } //storage 
       } //token 
      } //rest 

      cors.headers = ['Access-Control-Allow-Headers': 'Content-Type, Authorization, X-Auth-Token'] 
     } //springsecurity 
    } //plugin 
} //grails 

あなたは "フィールド「j_username」とJSON形式でユーザー名とパスワードを送信しなければなりませんj_password "を返し、json形式のトークンを返します。次に、このトークンとともにヘッダーフィールド "X-Auth-Token"内のリクエストを、照会したいapiに送信します。

春・セキュリティ・コアプラグインを初期化するための、私のコードのhttp://grails-plugins.github.io/grails-spring-security-core/v2/guide/single.html#tutorials

完全に相談してくださいgithubのに利用可能である:私のコードを実行する前にhttps://github.com/xixinhe/api_token_authentication

、オラクルのMySQL 5

をインストールしてください

スタックオーバーフローのルールに違反したと書いたことがある場合は、私にお知らせします。私は変更されます。

英語のネイティブスピーカーではありません。私は愚かな英語を許してください。

おかげで、

関連する問題