Commits

Liang Yong Rui  committed 5b02b2c

增加在taobao用户第一次登录的处理机制:LocalTaobaoAccountNotFoundHandler

  • Participants
  • Parent commits b58c2e0

Comments (0)

Files changed (3)

File grails-app/conf/DefaultTaobaoSecurityConfig.groovy

         userLookup {
             accountsPropertyName = 'taobaoAccounts'
             nickPropertyName = 'nick'
+            accountOwnerPropertyName = 'owner'
         }   
     }
 }

File src/groovy/com/mingidea/security/taobao/TaobaoAuthenticationUserDetailsService.groovy

 
 class TaobaoAuthenticationUserDetailsService extends GormUserDetailsService implements AuthenticationUserDetailsService {
     GrailsApplication grailsApplication
-
+    LocalTaobaoAccountNotFoundHandler localTaobaoAccountNotFoundHandler
     @Override
     public UserDetails loadUserDetails(Authentication auth) throws UsernameNotFoundException {
         def conf = SpringSecurityUtils.securityConfig
         TaobaoAccount.withTransaction { status ->
             def nickPropertyName = conf.taobao.userLookup.nickPropertyName
             def taobaoAccount = TaobaoAccount.findWhere((nickPropertyName) : taobaoNick)
-            if(!taobaoAccount) {
+            def user
+            if(taobaoAccount) {
+                def accountOwnerPropertyName = conf.taobao.userLookup.accountOwnerPropertyName
+                user = taobaoAccount."$accountOwnerPropertyName"
+            } else {
+                if(localTaobaoAccountNotFoundHandler) {
+                    log.debug "The local taobao account not found: $taobaoNick,try LocalTaobaoAccountNotFoundHandler"
+                    user = localTaobaoAccountNotFoundHandler.handle(taobaoNick, auth.credentials.session)
+                }
+            }
+
+            if(!user) {
                 log.warn "Taobao account not found: $taobaoNick"
                 throw new UsernameNotFoundException('Taobao account not found', taobaoNick)
             }
-            def user = taobaoAccount.owner
+            
+
 
             def usernamePropertyName = conf.userLookup.usernamePropertyName
             Collection<GrantedAuthority> authorities = loadAuthorities(user, user."$usernamePropertyName", true)

File src/java/com/mingidea/security/taobao/LocalTaobaoAccountNotFoundHandler.java

+package com.mingidea.security.taobao;
+
+/**
+ * 
+ * 主要用于Taobao用户第一次登录,在本地数据库没有数据的情况。
+ * @author Simon Leung
+ * @since 0.1
+ */
+public interface LocalTaobaoAccountNotFoundHandler {
+    /**
+     * 
+     * @param taobaoNick the nickname of taobao user
+     * @param session taobao api session key
+     * @return the user object
+     */
+    public Object handle(String taobaoNick, String session);
+}