Browse Source

完成富士签名

魏熙美 6 years ago
parent
commit
c83617f79d

+ 8
- 1
CODE/smart-community/property-api/pom.xml View File

210
             <scope>compile</scope>
210
             <scope>compile</scope>
211
         </dependency>
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
 	<dependencyManagement>
223
 	<dependencyManagement>
217
 		<dependencies>
224
 		<dependencies>

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

21
 
21
 
22
     private String appSecret;
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 View File

1
 package com.community.huiju.common.sign;
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
 import org.apache.commons.lang3.StringUtils;
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
 import java.util.Map;
10
 import java.util.Map;
7
 import java.util.Set;
11
 import java.util.Set;
12
+import java.util.TreeMap;
8
 
13
 
9
 /**
14
 /**
10
  * 大苏签名校验
15
  * 大苏签名校验
11
  */
16
  */
17
+@Slf4j
12
 public class DaSuSign {
18
 public class DaSuSign {
13
 
19
 
14
     /**
20
     /**
17
      * @param val_2
23
      * @param val_2
18
      * @return true 相等  false 不相等
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
         return StringUtils.equals(val_1, val_2);
27
         return StringUtils.equals(val_1, val_2);
22
     }
28
     }
23
 
29
 
29
      * @param appSecret
35
      * @param appSecret
30
      * @param datetime 时间戳
36
      * @param datetime 时间戳
31
      * @param encrypt 加密方式(目前就 MD5)
37
      * @param encrypt 加密方式(目前就 MD5)
32
-     * @param communityid 小区id
38
+     * @param communityId 小区id
33
      * @return
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 View File

11
 import com.community.huiju.model.User;
11
 import com.community.huiju.model.User;
12
 import com.community.huiju.service.IUserService;
12
 import com.community.huiju.service.IUserService;
13
 import com.community.huiju.vo.UserVO;
13
 import com.community.huiju.vo.UserVO;
14
+import com.google.common.collect.Maps;
14
 import io.swagger.annotations.Api;
15
 import io.swagger.annotations.Api;
15
 import io.swagger.annotations.ApiImplicitParam;
16
 import io.swagger.annotations.ApiImplicitParam;
16
 import io.swagger.annotations.ApiImplicitParams;
17
 import io.swagger.annotations.ApiImplicitParams;
20
 import org.springframework.web.bind.annotation.*;
21
 import org.springframework.web.bind.annotation.*;
21
 
22
 
22
 import javax.servlet.http.HttpSession;
23
 import javax.servlet.http.HttpSession;
24
+import java.util.Map;
23
 
25
 
24
 /**
26
 /**
25
  * <p>
27
  * <p>
165
 		return responseBean;
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 View File

84
      */
84
      */
85
     ResponseBean allUserRole(Integer communityId);
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 View File

9
 import com.community.commom.mode.ResponseBean;
9
 import com.community.commom.mode.ResponseBean;
10
 import com.community.commom.session.UserElement;
10
 import com.community.commom.session.UserElement;
11
 import com.community.commom.utils.BeanTools;
11
 import com.community.commom.utils.BeanTools;
12
+import com.community.huiju.common.perproties.DaSuProperties;
13
+import com.community.huiju.common.sign.DaSuSign;
12
 import com.community.huiju.dao.*;
14
 import com.community.huiju.dao.*;
13
 import com.community.huiju.dao.UserMapper;
15
 import com.community.huiju.dao.UserMapper;
14
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
16
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
28
 import org.springframework.stereotype.Service;
30
 import org.springframework.stereotype.Service;
29
 import org.springframework.transaction.annotation.Transactional;
31
 import org.springframework.transaction.annotation.Transactional;
30
 
32
 
31
-import java.time.LocalDateTime;
33
+import java.time.*;
32
 import java.util.*;
34
 import java.util.*;
33
 
35
 
34
 /**
36
 /**
62
     @Autowired
64
     @Autowired
63
     private  TaUserMapper taUserMapper;
65
     private  TaUserMapper taUserMapper;
64
 
66
 
67
+    @Autowired
68
+    private DaSuProperties daSuProperties;
65
 
69
 
66
     @Override
70
     @Override
67
     public ResponseBean login(String phone, Integer communityId, String code) {
71
     public ResponseBean login(String phone, Integer communityId, String code) {
449
         return result;
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 View File

41
 da-su:
41
 da-su:
42
   appid: dssdw2576sd997
42
   appid: dssdw2576sd997
43
   app-secret: fgexhd855sf4
43
   app-secret: fgexhd855sf4
44
+  community-id: 101
44
 
45