明点软件 avatar 明点软件 committed 1e92ec3

支持R1 R2 W1 W2分级别授权

Comments (0)

Files changed (6)

SpringSecurityTaobaoGrailsPlugin.groovy

 */
 class SpringSecurityTaobaoGrailsPlugin {
     // the plugin version
-    def version = "1.0.3"
+    def version = "1.0.4"
     // the version or versions of Grails the plugin is designed for
     def grailsVersion = "2.1 > *"
     // the other plugins this plugin depends on

application.properties

 #Grails Metadata file
-#Sun Aug 19 15:12:55 CST 2012
-app.grails.version=2.0.0
+#Wed Aug 22 22:30:25 CST 2012
+app.grails.version=2.1.0
 app.name=spring-security-taobao
-plugins.hibernate=2.0.0
+plugins.hibernate=2.1.0
 plugins.release=2.0.4
 plugins.spring-security-core=1.2.7.3
-plugins.tomcat=2.0.0
+plugins.tomcat=2.1.0
-<plugin name='spring-security-taobao' version='1.0.0' grailsVersion='2.1 &gt; *'>
+<plugin name='spring-security-taobao' version='1.0.4' grailsVersion='2.1 &gt; *'>
   <author>Simon Leung</author>
   <authorEmail>simon.r.leung@gmail.com</authorEmail>
   <title>Spring Security Taobao Plugin</title>

src/java/com/mingidea/security/taobao/TaobaoAuthenticationProvider.java

         if (taobaoUser == null) {
             log.debug("create taobao user {}", nickname);
             taobaoUser = new TaobaoUser(nickname, appKey, session);
+            taobaoUser.setExpiresIn(credentials.getExpiresIn());
+            taobaoUser.setR1ExpiresIn(credentials.getR1ExpiresIn());
+            taobaoUser.setR2ExpiresIn(credentials.getR2ExpiresIn());
+            taobaoUser.setW1ExpiresIn(credentials.getW1ExpiresIn());
+            taobaoUser.setW2ExpiresIn(credentials.getR2ExpiresIn());
+            taobaoUser.setRefreshToken(credentials.getRefreshToken());
+            taobaoUser.setReExpiresIn(credentials.getReExpiresIn());
             taobaoAuthenticationDao.create(taobaoUser);
             applicationContext.publishEvent(new TaobaoUserCreatedEvent(this, taobaoUser));
         } else {

src/java/com/mingidea/security/taobao/TaobaoCredentials.java

  * @since 0.1
  */
 public class TaobaoCredentials implements Serializable {
-    private static final long serialVersionUID = 5346807960373241353L;
-    
+    private static final long serialVersionUID = 1L;
+
     private String appKey;
-    private String session; 
+    private String session;
     private String parameters;
     private String sign;
-    
+
     //上下文参数,从top_parameters中解析所得
     private long timestamp;
     private String iframe;
     private String visitorNick;
     private String visitorRole;
 
-    private int expiresIn;
-    private int r1ExpiresIn;
-    private int r2ExpiresIn;
-    private int w1ExpiresIn;
-    private int w2ExpiresIn;
-    private int reExpiresIn;
+    private long expiresIn;
+    private long r1ExpiresIn;
+    private long r2ExpiresIn;
+    private long w1ExpiresIn;
+    private long w2ExpiresIn;
+    private long reExpiresIn;
 
     private String refreshToken;
     private String subTaobaoUserId;
     private String subTaobaoUserNick;
-    
+
     private TaobaoCredentials() {
         //Can not be instantiated directly,use #build()
     }
-    
+
     /**
      * Build a TaobaoCredentials instance by HTTP request.
+     *
      * @param request
      * @return
      */
     public static TaobaoCredentials build(HttpServletRequest request) {
         Assert.notNull(request, "The request can't be null");
         TaobaoCredentials credentials = new TaobaoCredentials();
-        credentials.appKey =  request.getParameter("top_appkey");
-        credentials.session =  request.getParameter("top_session");
+        credentials.appKey = request.getParameter("top_appkey");
+        credentials.session = request.getParameter("top_session");
         //base64 encoded
         String parameters = request.getParameter("top_parameters");
-        credentials.parameters =  parameters;
-        credentials.sign =  request.getParameter("top_sign");
-        
-        if(parameters != null) {
+        credentials.parameters = parameters;
+        credentials.sign = request.getParameter("top_sign");
+
+        if (parameters != null) {
             String charsetParameter = request.getParameter("encode");
             String charset = charsetParameter == null ? "GBK" : charsetParameter;
             String decodedParameters = null;
             } catch (UnsupportedEncodingException e) {
                 //ignore
             }
-            Map<String,String> parameterMap = parseParameters(decodedParameters);
+            Map<String, String> parameterMap = parseParameters(decodedParameters);
             credentials.timestamp = Long.parseLong(parameterMap.get("ts"));
             credentials.iframe = parameterMap.get("iframe");
             credentials.visitorId = parameterMap.get("visitor_id");
             credentials.visitorNick = parameterMap.get("visitor_nick");
             credentials.visitorRole = parameterMap.get("visitor_role");
 
-            credentials.expiresIn = Integer.parseInt(parameterMap.get("expires_in"));
-            credentials.r1ExpiresIn = Integer.parseInt(parameterMap.get("r1_expires_in"));
-            credentials.r2ExpiresIn = Integer.parseInt(parameterMap.get("r2_expires_in"));
-            credentials.w1ExpiresIn = Integer.parseInt(parameterMap.get("w1_expires_in"));
-            credentials.w2ExpiresIn = Integer.parseInt(parameterMap.get("w2_expires_in"));
-            credentials.reExpiresIn = Integer.parseInt(parameterMap.get("re_expires_in"));
+            credentials.expiresIn = toLong(parameterMap.get("expires_in"));
+            credentials.r1ExpiresIn = toLong(parameterMap.get("r1_expires_in"));
+            credentials.r2ExpiresIn = toLong(parameterMap.get("r2_expires_in"));
+            credentials.w1ExpiresIn = toLong(parameterMap.get("w1_expires_in"));
+            credentials.w2ExpiresIn = toLong(parameterMap.get("w2_expires_in"));
+            credentials.reExpiresIn = toLong(parameterMap.get("re_expires_in"));
 
             credentials.refreshToken = parameterMap.get("refresh_token");
             credentials.subTaobaoUserId = parameterMap.get("sub_taobao_user_id");
             credentials.subTaobaoUserNick = parameterMap.get("sub_taobao_user_nick");
         }
-   
+
         return credentials;
     }
 
+    private static long toLong(String str) {
+        if (str == null) {
+            return 0L;
+        }
+        try {
+            return Long.parseLong(str);
+        } catch (NumberFormatException nfe) {
+            return 0L;
+        }
+    }
+
     /**
      * 将URL参数解析成Map
-     * 
+     *
      * @param parameters like:key1=value1&key2=value2……
      * @return key value map
      */
-    private static Map<String,String> parseParameters(String parameters) {
-        Map<String,String> map = new HashMap<String,String>(5);
+    private static Map<String, String> parseParameters(String parameters) {
+        Map<String, String> map = new HashMap<String, String>();
         String[] pairs = StringUtils.split(parameters, "&");
-        for(String pair : pairs) {
+        for (String pair : pairs) {
             String[] keyValue = StringUtils.split(pair, "=");
             map.put(keyValue[0], keyValue[1]);
         }
         return map;
     }
-    
+
     public String getParameters() {
-		return parameters;
-	}
+        return parameters;
+    }
 
-	public String getSession() {
+    public String getSession() {
         return session;
     }
 
         return visitorRole;
     }
 
-    public int getR1ExpiresIn() {
+    public long getExpiresIn() {
+        return expiresIn;
+    }
+
+    public void setExpiresIn(long expiresIn) {
+        this.expiresIn = expiresIn;
+    }
+
+    public long getR1ExpiresIn() {
         return r1ExpiresIn;
     }
 
-    public int getR2ExpiresIn() {
+    public void setR1ExpiresIn(long r1ExpiresIn) {
+        this.r1ExpiresIn = r1ExpiresIn;
+    }
+
+    public long getR2ExpiresIn() {
         return r2ExpiresIn;
     }
 
-    public int getW1ExpiresIn() {
+    public void setR2ExpiresIn(long r2ExpiresIn) {
+        this.r2ExpiresIn = r2ExpiresIn;
+    }
+
+    public long getW1ExpiresIn() {
         return w1ExpiresIn;
     }
 
-    public int getW2ExpiresIn() {
+    public void setW1ExpiresIn(long w1ExpiresIn) {
+        this.w1ExpiresIn = w1ExpiresIn;
+    }
+
+    public long getW2ExpiresIn() {
         return w2ExpiresIn;
     }
 
-    public int getReExpiresIn() {
+    public void setW2ExpiresIn(long w2ExpiresIn) {
+        this.w2ExpiresIn = w2ExpiresIn;
+    }
+
+    public long getReExpiresIn() {
         return reExpiresIn;
     }
 
+    public void setReExpiresIn(long reExpiresIn) {
+        this.reExpiresIn = reExpiresIn;
+    }
+
     public String getRefreshToken() {
         return refreshToken;
     }
 
+    public void setRefreshToken(String refreshToken) {
+        this.refreshToken = refreshToken;
+    }
+
     public String getSubTaobaoUserId() {
         return subTaobaoUserId;
     }
 
+    public void setSubTaobaoUserId(String subTaobaoUserId) {
+        this.subTaobaoUserId = subTaobaoUserId;
+    }
+
     public String getSubTaobaoUserNick() {
         return subTaobaoUserNick;
     }
 
-    public int getExpiresIn() {
-        return expiresIn;
+    public void setSubTaobaoUserNick(String subTaobaoUserNick) {
+        this.subTaobaoUserNick = subTaobaoUserNick;
     }
 }

src/java/com/mingidea/security/taobao/TaobaoUser.java

 package com.mingidea.security.taobao;
 
 public class TaobaoUser {
-    /** 昵称(如果该用户是子账号则为子账号,否则则与主账号相同) */
+    /**
+     * 昵称(如果该用户是子账号则为子账号,否则则与主账号相同)
+     */
     private String nickname;
+    private String appKey;
+    private String session;
 
-	private String appKey;
-	private String session;
+    private long timestamp;
+    private long expiresIn;
+    private long r1ExpiresIn;
+    private long r2ExpiresIn;
+    private long w1ExpiresIn;
+    private long w2ExpiresIn;
+    private long reExpiresIn;
 
-	public TaobaoUser(String nickname, String appKey, String session) {
+    private String refreshToken;
+
+    public TaobaoUser(String nickname, String appKey, String session) {
         this.nickname = nickname;
-		this.appKey = appKey;
-		this.session = session;
-	}
+        this.appKey = appKey;
+        this.session = session;
+    }
 
     public void setNickname(String nickname) {
         this.nickname = nickname;
     }
 
     public String getAppKey() {
-		return appKey;
-	}
+        return appKey;
+    }
 
-	public void setAppKey(String appKey) {
-		this.appKey = appKey;
-	}
+    public void setAppKey(String appKey) {
+        this.appKey = appKey;
+    }
 
-	public String getSession() {
-		return session;
-	}
+    public String getSession() {
+        return session;
+    }
 
-	public void setSession(String session) {
-		this.session = session;
-	}
+    public void setSession(String session) {
+        this.session = session;
+    }
+
+    public long getTimestamp() {
+        return timestamp;
+    }
+
+    public void setTimestamp(long timestamp) {
+        this.timestamp = timestamp;
+    }
+
+    public long getExpiresIn() {
+        return expiresIn;
+    }
+
+    public void setExpiresIn(long expiresIn) {
+        this.expiresIn = expiresIn;
+    }
+
+    public long getR1ExpiresIn() {
+        return r1ExpiresIn;
+    }
+
+    public void setR1ExpiresIn(long r1ExpiresIn) {
+        this.r1ExpiresIn = r1ExpiresIn;
+    }
+
+    public long getR2ExpiresIn() {
+        return r2ExpiresIn;
+    }
+
+    public void setR2ExpiresIn(long r2ExpiresIn) {
+        this.r2ExpiresIn = r2ExpiresIn;
+    }
+
+    public long getW1ExpiresIn() {
+        return w1ExpiresIn;
+    }
+
+    public void setW1ExpiresIn(long w1ExpiresIn) {
+        this.w1ExpiresIn = w1ExpiresIn;
+    }
+
+    public long getW2ExpiresIn() {
+        return w2ExpiresIn;
+    }
+
+    public void setW2ExpiresIn(long w2ExpiresIn) {
+        this.w2ExpiresIn = w2ExpiresIn;
+    }
+
+    public long getReExpiresIn() {
+        return reExpiresIn;
+    }
+
+    public void setReExpiresIn(long reExpiresIn) {
+        this.reExpiresIn = reExpiresIn;
+    }
+
+    public String getRefreshToken() {
+        return refreshToken;
+    }
+
+    public void setRefreshToken(String refreshToken) {
+        this.refreshToken = refreshToken;
+    }
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.