Procházet zdrojové kódy

完成富士签名

魏熙美 před 6 roky
rodič
revize
c83617f79d

+ 8
- 1
CODE/smart-community/property-api/pom.xml Zobrazit soubor

@@ -210,8 +210,15 @@
210 210
             <scope>compile</scope>
211 211
         </dependency>
212 212
 
213
+		<!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
214
+		<dependency>
215
+			<groupId>commons-codec</groupId>
216
+			<artifactId>commons-codec</artifactId>
217
+			<version>1.10</version>
218
+		</dependency>
219
+
213 220
 
214
-    </dependencies>
221
+	</dependencies>
215 222
 
216 223
 	<dependencyManagement>
217 224
 		<dependencies>

+ 2
- 0
CODE/smart-community/property-api/src/main/java/com/community/huiju/common/perproties/DaSuProperties.java Zobrazit soubor

@@ -21,4 +21,6 @@ public class DaSuProperties {
21 21
 
22 22
     private String appSecret;
23 23
 
24
+    private String communityId;
25
+
24 26
 }

+ 45
- 13
CODE/smart-community/property-api/src/main/java/com/community/huiju/common/sign/DaSuSign.java Zobrazit soubor

@@ -1,14 +1,20 @@
1 1
 package com.community.huiju.common.sign;
2 2
 
3
+import com.google.common.collect.Maps;
4
+import lombok.extern.slf4j.Slf4j;
5
+import org.apache.commons.codec.digest.DigestUtils;
3 6
 import org.apache.commons.lang3.StringUtils;
4 7
 
5
-import java.util.HashMap;
8
+import java.io.UnsupportedEncodingException;
9
+import java.time.LocalDateTime;
6 10
 import java.util.Map;
7 11
 import java.util.Set;
12
+import java.util.TreeMap;
8 13
 
9 14
 /**
10 15
  * 大苏签名校验
11 16
  */
17
+@Slf4j
12 18
 public class DaSuSign {
13 19
 
14 20
     /**
@@ -17,7 +23,7 @@ public class DaSuSign {
17 23
      * @param val_2
18 24
      * @return true 相等  false 不相等
19 25
      */
20
-    public boolean isMD5Sign(String val_1, String val_2) {
26
+    public static boolean isMD5Sign(String val_1, String val_2) {
21 27
         return StringUtils.equals(val_1, val_2);
22 28
     }
23 29
 
@@ -29,23 +35,49 @@ public class DaSuSign {
29 35
      * @param appSecret
30 36
      * @param datetime 时间戳
31 37
      * @param encrypt 加密方式(目前就 MD5)
32
-     * @param communityid 小区id
38
+     * @param communityId 小区id
33 39
      * @return
34 40
      */
35
-    public String createSign(String appid, String appSecret, String datetime, String encrypt, String communityid) {
36
-        return null;
37
-    }
41
+    public static String createSign(String appid, String appSecret, String datetime, String encrypt, String communityId){
42
+        TreeMap<String ,Object> treeMap = Maps.newTreeMap();
43
+        treeMap.put("appid", appid);
44
+        treeMap.put("datetime", datetime);
45
+        treeMap.put("encrypt", encrypt);
46
+        treeMap.put("communityid", communityId);
38 47
 
48
+        String sign = "";
39 49
 
40
-    public static void main(String[] args) {
41
-        Map<String, Object> map = new HashMap<>();
42
-        map.put("a", "1");
43
-        map.put("d", "4");
44
-        map.put("b", "2");
50
+        StringBuilder sb = new StringBuilder();
51
+        Set<Map.Entry<String, Object>> entries = treeMap.entrySet();
52
+        entries.forEach(e-> {
53
+            sb.append(e.getKey());
54
+            sb.append("=");
55
+            sb.append(e.getValue());
56
+            sb.append("&");
57
+        });
58
+
59
+        sign = sb.toString();
60
+        sign = sign.substring(0, sign.lastIndexOf("&"));
61
+
62
+        log.info("签名之前参数: {}" , sign);
63
+
64
+        try {
65
+            String str = new String(sign.getBytes("UTF-8"), "UTF-8");
66
+            sign = DigestUtils.md5Hex( str + appSecret);
67
+        } catch (UnsupportedEncodingException e) {
68
+            e.printStackTrace();
69
+        }
45 70
 
46
-        Set<Map.Entry<String, Object>> entries = map.entrySet();
47
-        entries.forEach(e -> System.out.println(e.getKey()));
71
+        log.info("签名: {}" , sign);
72
+
73
+        return sign;
74
+    }
75
+
76
+    public static void main(String[] args) {
48 77
 
78
+        Long timeMillis = System.currentTimeMillis();
79
+        System.out.println(timeMillis);
80
+        String sign = createSign("dssdw2576sd997", "fgexhd855sf4", timeMillis + "", "md5", "101");
49 81
     }
50 82
 
51 83
 }

+ 32
- 0
CODE/smart-community/property-api/src/main/java/com/community/huiju/controller/UserController.java Zobrazit soubor

@@ -11,6 +11,7 @@ import com.community.huiju.model.TaUser;
11 11
 import com.community.huiju.model.User;
12 12
 import com.community.huiju.service.IUserService;
13 13
 import com.community.huiju.vo.UserVO;
14
+import com.google.common.collect.Maps;
14 15
 import io.swagger.annotations.Api;
15 16
 import io.swagger.annotations.ApiImplicitParam;
16 17
 import io.swagger.annotations.ApiImplicitParams;
@@ -20,6 +21,7 @@ import org.springframework.cloud.context.config.annotation.RefreshScope;
20 21
 import org.springframework.web.bind.annotation.*;
21 22
 
22 23
 import javax.servlet.http.HttpSession;
24
+import java.util.Map;
23 25
 
24 26
 /**
25 27
  * <p>
@@ -165,4 +167,34 @@ public class UserController extends BaseController {
165 167
 		return responseBean;
166 168
 	}
167 169
 
170
+	@ApiOperation(value = "获取令牌", notes = "获取令牌")
171
+	@ApiImplicitParams({
172
+			@ApiImplicitParam(dataTypeClass = String.class, paramType = "query", name = "appid", value = "提供的appid"),
173
+			@ApiImplicitParam(dataTypeClass = String.class, paramType = "query", name = "datetime", value = "时间戳"),
174
+			@ApiImplicitParam(dataTypeClass = String.class, paramType = "query", name = "encrypt", value = "encrypt加密方式(目前值为md5)"),
175
+			@ApiImplicitParam(dataTypeClass = String.class, paramType = "query", name = "communityId", value = "提供的小区编号"),
176
+			@ApiImplicitParam(dataTypeClass = String.class, paramType = "query", name = "sign", value = "生成的签名"),
177
+	})
178
+	@RequestMapping(value = "/user/token", method = RequestMethod.GET)
179
+	public ResponseBean daSuToken(@RequestParam("appid") String appid,
180
+								  @RequestParam("datetime") String datetime,
181
+								  @RequestParam("encrypt") String encrypt,
182
+								  @RequestParam("communityId") String communityId,
183
+								  @RequestParam("sign") String sign,
184
+								  HttpSession session){
185
+		ResponseBean responseBean = new ResponseBean();
186
+		responseBean = userService.daSuToken(appid, datetime, encrypt, communityId, sign);
187
+		// 为 0 表示成功
188
+		if ("0".equals(responseBean.getCode())) {
189
+			UserVO userVO = (UserVO)responseBean.getData();
190
+			setUserElement(session,userVO);
191
+			userVO.setToken(session.getId());
192
+
193
+			Map<String, Object> map = Maps.newHashMap();
194
+			map.put("token", session.getId());
195
+			responseBean.addSuccess(map);
196
+		}
197
+		return responseBean;
198
+	}
199
+
168 200
 }

+ 10
- 0
CODE/smart-community/property-api/src/main/java/com/community/huiju/service/IUserService.java Zobrazit soubor

@@ -84,4 +84,14 @@ public interface IUserService extends IService<User> {
84 84
      */
85 85
     ResponseBean allUserRole(Integer communityId);
86 86
 
87
+    /**
88
+     * 大苏 token生成
89
+     * @param appid
90
+     * @param datetime 时间戳
91
+     * @param encrypt 加密方式(默认MD5)
92
+     * @param communityId 小区
93
+     * @param sign 传过来的签名
94
+     * @return
95
+     */
96
+    ResponseBean daSuToken(String appid,  String datetime, String encrypt, String communityId, String sign);
87 97
 }

+ 57
- 1
CODE/smart-community/property-api/src/main/java/com/community/huiju/service/impl/UserServiceImpl.java Zobrazit soubor

@@ -9,6 +9,8 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
9 9
 import com.community.commom.mode.ResponseBean;
10 10
 import com.community.commom.session.UserElement;
11 11
 import com.community.commom.utils.BeanTools;
12
+import com.community.huiju.common.perproties.DaSuProperties;
13
+import com.community.huiju.common.sign.DaSuSign;
12 14
 import com.community.huiju.dao.*;
13 15
 import com.community.huiju.dao.UserMapper;
14 16
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
@@ -28,7 +30,7 @@ import org.springframework.beans.factory.annotation.Autowired;
28 30
 import org.springframework.stereotype.Service;
29 31
 import org.springframework.transaction.annotation.Transactional;
30 32
 
31
-import java.time.LocalDateTime;
33
+import java.time.*;
32 34
 import java.util.*;
33 35
 
34 36
 /**
@@ -62,6 +64,8 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IU
62 64
     @Autowired
63 65
     private  TaUserMapper taUserMapper;
64 66
 
67
+    @Autowired
68
+    private DaSuProperties daSuProperties;
65 69
 
66 70
     @Override
67 71
     public ResponseBean login(String phone, Integer communityId, String code) {
@@ -449,4 +453,56 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IU
449 453
         return result;
450 454
     }
451 455
 
456
+
457
+    @Override
458
+    public ResponseBean daSuToken(String appid, String datetime, String encrypt, String communityId, String sign) {
459
+        ResponseBean responseBean = new ResponseBean();
460
+
461
+        LocalDateTime currrenDate = LocalDateTime.now();
462
+        LocalDateTime parameDate = Instant.ofEpochMilli(Long.parseLong(datetime)).atZone(ZoneId.systemDefault()).toLocalDateTime();
463
+
464
+        long minutes = Duration.between(parameDate, currrenDate).toMinutes();
465
+        if (minutes > 10) {
466
+            responseBean.addError("签名已过期!");
467
+            return responseBean;
468
+        }
469
+
470
+
471
+        // server 端签名
472
+        String serverSign = DaSuSign.createSign(daSuProperties.getAppid(), daSuProperties.getAppSecret(), datetime, encrypt, communityId);
473
+        boolean isMd5Sign = DaSuSign.isMD5Sign(serverSign, sign);
474
+        if (!isMd5Sign) {
475
+            responseBean.addError("签名错误!");
476
+            return responseBean;
477
+        }
478
+
479
+        if (!daSuProperties.getCommunityId().equals(communityId)) {
480
+            responseBean.addError("小区不正确!");
481
+            return responseBean;
482
+        }
483
+
484
+        if (!daSuProperties.getAppid().equals(appid)) {
485
+            responseBean.addError("appid 不正确!");
486
+            return responseBean;
487
+        }
488
+
489
+        QueryWrapper<ToCommunities> queryCommunityWrapper = new QueryWrapper<>();
490
+        queryCommunityWrapper.eq("id", communityId);
491
+        ToCommunities toCommunities = toCommunitiesMapper.selectOne(queryCommunityWrapper);
492
+
493
+
494
+        // 查询用户
495
+        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
496
+        queryWrapper.eq("login_name", toCommunities.getLoginName());
497
+        queryWrapper.eq("community_id", communityId);
498
+        User user = userMapper.selectOne(queryWrapper) ;
499
+
500
+        UserVO userVO = new UserVO();
501
+        // 查询该用户的权限
502
+        // 获取用户权限
503
+        getUserRole(user,userVO);
504
+
505
+        responseBean.addSuccess(userVO);
506
+        return responseBean;
507
+    }
452 508
 }

+ 1
- 0
CODE/smart-community/property-api/src/main/resources/application.yml Zobrazit soubor

@@ -41,4 +41,5 @@ fu-shi:
41 41
 da-su:
42 42
   appid: dssdw2576sd997
43 43
   app-secret: fgexhd855sf4
44
+  community-id: 101
44 45