正在显示
19 个修改的文件
包含
1168 行增加
和
54 行删除
@@ -58,6 +58,9 @@ spring: | @@ -58,6 +58,9 @@ spring: | ||
58 | #password=1q2w3e4r | 58 | #password=1q2w3e4r |
59 | #spring datasource mysql,注意编码配置,缺少数据库编码配置容易引起中文入库乱码 | 59 | #spring datasource mysql,注意编码配置,缺少数据库编码配置容易引起中文入库乱码 |
60 | url: jdbc:mysql://118.31.66.166:3306/station?useUnicode=true&characterEncoding=utf8 | 60 | url: jdbc:mysql://118.31.66.166:3306/station?useUnicode=true&characterEncoding=utf8 |
61 | +# url: jdbc:mysql://localhost:3306/station?useUnicode=true&characterEncoding=utf8 | ||
62 | +# username: root | ||
63 | +# password: Long455900 | ||
61 | username: 110 | 64 | username: 110 |
62 | password: QAHqCJf2kFYCLirM | 65 | password: QAHqCJf2kFYCLirM |
63 | driver-class-name: com.mysql.cj.jdbc.Driver | 66 | driver-class-name: com.mysql.cj.jdbc.Driver |
@@ -105,6 +108,7 @@ eureka: | @@ -105,6 +108,7 @@ eureka: | ||
105 | service-url: | 108 | service-url: |
106 | # defaultZone: http://127.0.0.1:12345/eureka/ | 109 | # defaultZone: http://127.0.0.1:12345/eureka/ |
107 | defaultZone: http://192.168.1.53:12345/eureka/ | 110 | defaultZone: http://192.168.1.53:12345/eureka/ |
111 | +# defaultZone: http://127.0.0.1:8004/eureka/ | ||
108 | registry-fetch-interval-seconds: 30 | 112 | registry-fetch-interval-seconds: 30 |
109 | 113 | ||
110 | 114 |
@@ -213,6 +213,13 @@ | @@ -213,6 +213,13 @@ | ||
213 | <!-- </dependency>--> | 213 | <!-- </dependency>--> |
214 | 214 | ||
215 | <dependency> | 215 | <dependency> |
216 | + <groupId>net.sf.json-lib</groupId> | ||
217 | + <artifactId>json-lib</artifactId> | ||
218 | + <version>2.4</version> | ||
219 | + <classifier>jdk15</classifier> | ||
220 | + </dependency> | ||
221 | + | ||
222 | + <dependency> | ||
216 | <groupId>de.codecentric</groupId> | 223 | <groupId>de.codecentric</groupId> |
217 | <artifactId>spring-boot-admin-starter-client</artifactId> | 224 | <artifactId>spring-boot-admin-starter-client</artifactId> |
218 | <version>2.1.6</version> | 225 | <version>2.1.6</version> |
@@ -242,6 +249,35 @@ | @@ -242,6 +249,35 @@ | ||
242 | <version>0.0.9</version> | 249 | <version>0.0.9</version> |
243 | </dependency> | 250 | </dependency> |
244 | 251 | ||
252 | + <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient --> | ||
253 | + <dependency> | ||
254 | + <groupId>org.apache.httpcomponents</groupId> | ||
255 | + <artifactId>httpclient</artifactId> | ||
256 | + <version>4.5.13</version> | ||
257 | + </dependency> | ||
258 | + | ||
259 | + <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson --> | ||
260 | + <dependency> | ||
261 | + <groupId>com.google.code.gson</groupId> | ||
262 | + <artifactId>gson</artifactId> | ||
263 | + <version>2.8.6</version> | ||
264 | + </dependency> | ||
265 | + | ||
266 | + <!-- sm4jar--> | ||
267 | + <!-- https://mvnrepository.com/artifact/org.bouncycastle/bcprov-jdk16 --> | ||
268 | +<!-- <dependency>--> | ||
269 | +<!-- <groupId>org.bouncycastle</groupId>--> | ||
270 | +<!-- <artifactId>bcprov-jdk16</artifactId>--> | ||
271 | +<!-- <version>1.46</version>--> | ||
272 | +<!-- </dependency>--> | ||
273 | + | ||
274 | + <dependency> | ||
275 | + <groupId>commons-httpclient</groupId> | ||
276 | + <artifactId>commons-httpclient</artifactId> | ||
277 | + <version>3.1</version> | ||
278 | + </dependency> | ||
279 | + | ||
280 | + | ||
245 | </dependencies> | 281 | </dependencies> |
246 | 282 | ||
247 | <dependencyManagement> | 283 | <dependencyManagement> |
@@ -4,38 +4,45 @@ import com.alibaba.fastjson.JSON; | @@ -4,38 +4,45 @@ import com.alibaba.fastjson.JSON; | ||
4 | 4 | ||
5 | import com.google.code.kaptcha.impl.DefaultKaptcha; | 5 | import com.google.code.kaptcha.impl.DefaultKaptcha; |
6 | 6 | ||
7 | +import com.google.gson.Gson; | ||
7 | import com.thoughtworks.xstream.core.util.Base64Encoder; | 8 | import com.thoughtworks.xstream.core.util.Base64Encoder; |
9 | +import com.tianbo.warehouse.bean.AuthSuccessResponse; | ||
8 | import com.tianbo.warehouse.controller.response.ResultJson; | 10 | import com.tianbo.warehouse.controller.response.ResultJson; |
11 | +import com.tianbo.warehouse.controller.response.ResultMessage; | ||
12 | +import com.tianbo.warehouse.dao.KakoUserMapper; | ||
13 | +import com.tianbo.warehouse.model.KakoUser; | ||
9 | import com.tianbo.warehouse.model.ROLE; | 14 | import com.tianbo.warehouse.model.ROLE; |
15 | +import com.tianbo.warehouse.model.resource.UserDate; | ||
16 | +import com.tianbo.warehouse.security.filter.JwtTokenUtil; | ||
17 | +import com.tianbo.warehouse.service.PermissionService; | ||
10 | import com.tianbo.warehouse.service.RoleService; | 18 | import com.tianbo.warehouse.service.RoleService; |
11 | 19 | ||
20 | +import com.tianbo.warehouse.service.kakoImp.KakoUserService; | ||
21 | +import com.tianbo.warehouse.util.JsonUtil; | ||
22 | +import com.tianbo.warehouse.util.KIAM.HttpClientUtil; | ||
12 | import com.tianbo.warehouse.util.RedisUtils; | 23 | import com.tianbo.warehouse.util.RedisUtils; |
13 | 24 | ||
14 | import lombok.extern.slf4j.Slf4j; | 25 | import lombok.extern.slf4j.Slf4j; |
15 | import org.springframework.beans.factory.annotation.Autowired; | 26 | import org.springframework.beans.factory.annotation.Autowired; |
16 | -import org.springframework.web.bind.annotation.PostMapping; | ||
17 | -import org.springframework.web.bind.annotation.RequestMapping; | ||
18 | -import org.springframework.web.bind.annotation.RestController; | 27 | +import org.springframework.beans.factory.annotation.Value; |
28 | +import org.springframework.util.StringUtils; | ||
29 | +import org.springframework.web.bind.annotation.*; | ||
19 | 30 | ||
20 | import javax.imageio.ImageIO; | 31 | import javax.imageio.ImageIO; |
21 | 32 | ||
22 | -import javax.servlet.http.Cookie; | ||
23 | -import javax.servlet.http.HttpServletRequest; | ||
24 | -import javax.servlet.http.HttpServletResponse; | ||
25 | -import javax.servlet.http.HttpSession; | ||
26 | import java.awt.image.BufferedImage; | 33 | import java.awt.image.BufferedImage; |
27 | import java.io.ByteArrayOutputStream; | 34 | import java.io.ByteArrayOutputStream; |
28 | import java.io.IOException; | 35 | import java.io.IOException; |
29 | -import java.util.HashMap; | ||
30 | -import java.util.List; | ||
31 | -import java.util.Map; | ||
32 | -import java.util.UUID; | 36 | +import java.util.*; |
33 | 37 | ||
34 | @Slf4j | 38 | @Slf4j |
35 | @RestController() | 39 | @RestController() |
36 | @RequestMapping("/anonymous") | 40 | @RequestMapping("/anonymous") |
37 | public class AnonymousController { | 41 | public class AnonymousController { |
38 | 42 | ||
43 | + @Value("${jwt.max-alive}") | ||
44 | + protected Integer jwtMaxAlive; | ||
45 | + | ||
39 | @Autowired | 46 | @Autowired |
40 | RoleService roleService; | 47 | RoleService roleService; |
41 | 48 | ||
@@ -43,8 +50,32 @@ public class AnonymousController { | @@ -43,8 +50,32 @@ public class AnonymousController { | ||
43 | RedisUtils redisUtils; | 50 | RedisUtils redisUtils; |
44 | 51 | ||
45 | @Autowired | 52 | @Autowired |
53 | + KakoUserMapper kakoUserMapperl; | ||
54 | + | ||
55 | + @Autowired | ||
46 | private DefaultKaptcha captchaProducer; | 56 | private DefaultKaptcha captchaProducer; |
47 | 57 | ||
58 | + @Autowired | ||
59 | + private PermissionService permissionService; | ||
60 | + | ||
61 | + @Autowired | ||
62 | + private KakoUserMapper kakoUserMapper; | ||
63 | + | ||
64 | + @Autowired | ||
65 | + private KakoUserService kakoUserService; | ||
66 | + | ||
67 | + | ||
68 | + | ||
69 | + | ||
70 | + /** | ||
71 | + * 验证服务票据响应属性名 | ||
72 | + */ | ||
73 | + private static final String SERVICE_RESPONESE = "serviceResponse"; | ||
74 | + private static final String AUTHENTICATION_SUCCESS = "authenticationSuccess"; | ||
75 | + private static final String LOGIN_NAME = "LOGIN_NAME"; | ||
76 | + private static final String ATTRIBUTES = "attributes"; | ||
77 | + private static final String USER_ID = "USER_ID"; | ||
78 | + | ||
48 | /** | 79 | /** |
49 | * 配置匿名者可以访问的路由,并更新到redis,匿名者默认可以访问的role_name =ROLE_anonymous | 80 | * 配置匿名者可以访问的路由,并更新到redis,匿名者默认可以访问的role_name =ROLE_anonymous |
50 | * 此方法会将所有符合权限组名=ROLE_anonymous的权限更新到redis中,供gateway调用判断权限 | 81 | * 此方法会将所有符合权限组名=ROLE_anonymous的权限更新到redis中,供gateway调用判断权限 |
@@ -86,4 +117,150 @@ public class AnonymousController { | @@ -86,4 +117,150 @@ public class AnonymousController { | ||
86 | return new ResultJson("200","verify get ok",map,verifyToken); | 117 | return new ResultJson("200","verify get ok",map,verifyToken); |
87 | 118 | ||
88 | } | 119 | } |
120 | + | ||
121 | + /** | ||
122 | + * 验证票据 | ||
123 | + * @param ticket | ||
124 | + * @return | ||
125 | + * @throws Exception | ||
126 | + */ | ||
127 | + @RequestMapping (value = "/ssoLogin") | ||
128 | + @ResponseBody | ||
129 | + public AuthSuccessResponse ssoLogin(String ticket) throws Exception { | ||
130 | + try { | ||
131 | + log.info("票据接受:{}", ticket); | ||
132 | + if (!StringUtils.isEmpty(ticket)){ | ||
133 | + log.info("开始验证票据"); | ||
134 | + String result = HttpClientUtil.httpGet("http://10.5.14.103:27080/sso/p3/serviceValidate?format=json&service="+"http://10.50.7.70:9909/%23/ssologin"+"&ticket="+ticket, "UTF-8"); | ||
135 | + log.info("验证票据返回值:{}", result); | ||
136 | + Map<String, Object> userInfo = resolveUserInfo(result); | ||
137 | + if (userInfo != null){ | ||
138 | + log.info("userInfo:{}", userInfo); | ||
139 | +// KakoUser user = kakoUserMapper.findLonginName(userInfo.get("loginName").toString(), userInfo.get("userId").toString()); | ||
140 | + KakoUser user = kakoUserService.loadByUsername(userInfo.get("loginName").toString()); | ||
141 | + log.info("查询用户信息:{}", user); | ||
142 | + //返回前端登陆成功后的用户信息 | ||
143 | + KakoUser loginedUser = new KakoUser(); | ||
144 | + loginedUser.setLoginName(user.getUsername()); | ||
145 | + loginedUser.setId(user.getId()); | ||
146 | + loginedUser.setName(user.getName()); | ||
147 | + loginedUser.setRoles(user.getRoles()); | ||
148 | + //设置用户的TOKEN的有效时间,时间配置在配置文件中设置 | ||
149 | + String jwtToken = JwtTokenUtil.generateToken(loginedUser.getUsername(), jwtMaxAlive); | ||
150 | + log.info("设置用户token:{}", jwtToken); | ||
151 | + loginedUser.setToken(jwtToken); | ||
152 | + //这里将登录成功的[user]对象数据写入redis缓存,KEY为token value为user的JSON对象 | ||
153 | + String json = JSON.toJSONString(user); | ||
154 | + boolean set = redisUtils.set(jwtToken, json, 3600 * 24 * 7); | ||
155 | + log.info("用户信息存入redis:{}", set); | ||
156 | + Map<String,Object> menuMap = permissionService.getUserMenusKako(user.getId()); | ||
157 | + //返回用户信息和用户可访问的目录列表 | ||
158 | + return new AuthSuccessResponse(loginedUser,menuMap); | ||
159 | + } | ||
160 | + return null; | ||
161 | + }else { | ||
162 | + return null; | ||
163 | + } | ||
164 | + }catch (Exception e){ | ||
165 | + e.printStackTrace(); | ||
166 | + return null; | ||
167 | + } | ||
168 | + | ||
169 | + } | ||
170 | + | ||
171 | + public Map<String, Object> resolveUserInfo(String result){ | ||
172 | + //认证成功,解析用户信息 | ||
173 | + Gson gson = new Gson(); | ||
174 | + Map resultMap = gson.fromJson(result, Map.class); | ||
175 | + Object serviceResponse = resultMap.get(SERVICE_RESPONESE); | ||
176 | + if(serviceResponse != null && serviceResponse instanceof Map){ | ||
177 | + Object authenticationSuccess = ((Map) serviceResponse).get(AUTHENTICATION_SUCCESS); | ||
178 | + if(authenticationSuccess != null && authenticationSuccess instanceof Map){ | ||
179 | + Map<String, Object> userInfo = new HashMap<>(); | ||
180 | + userInfo.put("attributes", ((Map) authenticationSuccess).get(ATTRIBUTES)); | ||
181 | + Object attributes = ((Map) authenticationSuccess).get("attributes"); | ||
182 | + userInfo.put("loginName", ((Map) attributes).get(LOGIN_NAME)); | ||
183 | + userInfo.put("userId", ((Map) attributes).get(USER_ID)); | ||
184 | + return userInfo; | ||
185 | + } | ||
186 | + } | ||
187 | + return null; | ||
188 | + } | ||
189 | + | ||
190 | + /** | ||
191 | + * 资源同步 | ||
192 | + * @param map | ||
193 | + * @return | ||
194 | + * @throws Exception | ||
195 | + */ | ||
196 | + @PostMapping(value = "/userSynchronization") | ||
197 | + @ResponseBody | ||
198 | + public ResultMessage userSynchronization(@RequestBody Map<String, Object> map) throws Exception { | ||
199 | + if ("user".equals(map.get("resType").toString())){ | ||
200 | + JsonUtil jsonUtil = new JsonUtil(); | ||
201 | + List<UserDate> list = jsonUtil.mapToList(map, UserDate.class, "data"); | ||
202 | + // 判断该用户是否存在 | ||
203 | + int i = 0; | ||
204 | + for (UserDate userDate: list){ | ||
205 | + log.info("用户信息:{}", userDate); | ||
206 | + if (kakoUserMapperl.findLonginName(userDate.getLOGIN_NAME(), "") != null){ | ||
207 | + KakoUser kakoUser = new KakoUser(); | ||
208 | + kakoUser.setLoginName(userDate.getLOGIN_NAME()); | ||
209 | + kakoUser.setPassword(userDate.getPWD()); | ||
210 | + kakoUser.setUserSyncId(userDate.getUSER_ID()); | ||
211 | + kakoUser.setMobile(userDate.getMOBILE()); | ||
212 | + kakoUser.setEmail(userDate.getEMAIL()); | ||
213 | + kakoUser.setDelFlag("0"); | ||
214 | + kakoUser.setName(userDate.getUSER_NAME()); | ||
215 | + kakoUser.setCompanyId("1"); | ||
216 | + kakoUser.setOfficeId("5e95a0f081c64fce9f85d59e18cfdc5f"); | ||
217 | + kakoUser.setUpdateBy("tyrz"); | ||
218 | + kakoUser.setLoginFlag("1"); | ||
219 | + kakoUser.setUpdateDate(new Date()); | ||
220 | + | ||
221 | + log.info("通过账号为条件更新"); | ||
222 | + i = kakoUserMapperl.updateSyncUserLoginName(kakoUser); | ||
223 | + }else { | ||
224 | + if (kakoUserMapperl.findLonginName("", userDate.getUSER_ID()) != null){ | ||
225 | + KakoUser kakoUser = new KakoUser(); | ||
226 | + kakoUser.setLoginName(userDate.getLOGIN_NAME()); | ||
227 | + kakoUser.setPassword(userDate.getPWD()); | ||
228 | + kakoUser.setUserSyncId(userDate.getUSER_ID()); | ||
229 | + kakoUser.setMobile(userDate.getMOBILE()); | ||
230 | + kakoUser.setEmail(userDate.getEMAIL()); | ||
231 | + kakoUser.setDelFlag("0"); | ||
232 | + kakoUser.setName(userDate.getUSER_NAME()); | ||
233 | + kakoUser.setCompanyId("1"); | ||
234 | + kakoUser.setOfficeId("5e95a0f081c64fce9f85d59e18cfdc5f"); | ||
235 | + kakoUser.setUpdateBy("tyrz"); | ||
236 | + kakoUser.setLoginFlag("1"); | ||
237 | + kakoUser.setUpdateDate(new Date()); | ||
238 | + log.info("账号不存在用过统一认证id更新"); | ||
239 | + i = kakoUserMapperl.updateSyncUserId(kakoUser); | ||
240 | + }else { | ||
241 | + KakoUser kakoUser = new KakoUser(); | ||
242 | + kakoUser.setId(UUID.randomUUID().toString()); | ||
243 | + kakoUser.setLoginName(userDate.getLOGIN_NAME()); | ||
244 | + kakoUser.setPassword(userDate.getPWD()); | ||
245 | + kakoUser.setUserSyncId(userDate.getUSER_ID()); | ||
246 | + kakoUser.setMobile(userDate.getMOBILE()); | ||
247 | + kakoUser.setEmail(userDate.getEMAIL()); | ||
248 | + kakoUser.setName(userDate.getUSER_NAME()); | ||
249 | + kakoUser.setCompanyId("1"); | ||
250 | + kakoUser.setOfficeId("5e95a0f081c64fce9f85d59e18cfdc5f"); | ||
251 | + kakoUser.setLoginFlag("1"); | ||
252 | + kakoUser.setCreateBy("tyrz"); | ||
253 | + kakoUser.setUpdateBy("tyrz"); | ||
254 | + kakoUser.setCreateDate(new Date()); | ||
255 | + kakoUser.setUpdateDate(new Date()); | ||
256 | + log.info("账号统一认证id都不在 新增"); | ||
257 | + i = kakoUserMapperl.insertSelective(kakoUser); | ||
258 | + } | ||
259 | + } | ||
260 | + } | ||
261 | + | ||
262 | + return i > 0 ? new ResultMessage("200","资源同步成功"):new ResultMessage("202", "资源同步失败"); | ||
263 | + } | ||
264 | + return new ResultMessage("201","不是用户信息"); | ||
265 | + } | ||
89 | } | 266 | } |
@@ -2,49 +2,36 @@ package com.tianbo.warehouse.controller.kako; | @@ -2,49 +2,36 @@ package com.tianbo.warehouse.controller.kako; | ||
2 | 2 | ||
3 | import com.alibaba.fastjson.JSON; | 3 | import com.alibaba.fastjson.JSON; |
4 | import com.github.pagehelper.PageInfo; | 4 | import com.github.pagehelper.PageInfo; |
5 | -import com.google.code.kaptcha.Constants; | ||
6 | -import com.google.code.kaptcha.impl.DefaultKaptcha; | ||
7 | -import com.google.code.kaptcha.util.Config; | ||
8 | import com.tianbo.warehouse.annotation.LogAnnotation; | 5 | import com.tianbo.warehouse.annotation.LogAnnotation; |
9 | import com.tianbo.warehouse.annotation.RequestRequire; | 6 | import com.tianbo.warehouse.annotation.RequestRequire; |
10 | -import com.tianbo.warehouse.annotation.UserPasswordMd5; | ||
11 | import com.tianbo.warehouse.annotation.cache.annotation.RedisCacheDelTarget; | 7 | import com.tianbo.warehouse.annotation.cache.annotation.RedisCacheDelTarget; |
12 | import com.tianbo.warehouse.controller.response.ResultJson; | 8 | import com.tianbo.warehouse.controller.response.ResultJson; |
13 | -import com.tianbo.warehouse.dao.KakoUserMapper; | 9 | +import com.tianbo.warehouse.controller.response.ResultMessage; |
14 | import com.tianbo.warehouse.model.KakoUser; | 10 | import com.tianbo.warehouse.model.KakoUser; |
15 | import com.tianbo.warehouse.model.KakoUserRole; | 11 | import com.tianbo.warehouse.model.KakoUserRole; |
16 | -import com.tianbo.warehouse.model.USERS; | ||
17 | -import com.tianbo.warehouse.model.UserRole; | ||
18 | import com.tianbo.warehouse.security.CustomUserDetailService; | 12 | import com.tianbo.warehouse.security.CustomUserDetailService; |
19 | -import com.tianbo.warehouse.service.UserService; | ||
20 | import com.tianbo.warehouse.service.kakoImp.KakoUserService; | 13 | import com.tianbo.warehouse.service.kakoImp.KakoUserService; |
21 | import com.tianbo.warehouse.service.validated.InsertUser; | 14 | import com.tianbo.warehouse.service.validated.InsertUser; |
22 | import com.tianbo.warehouse.service.validated.UpdateUser; | 15 | import com.tianbo.warehouse.service.validated.UpdateUser; |
16 | +import com.tianbo.warehouse.util.KIAM.HttpClientUtil; | ||
23 | import com.tianbo.warehouse.util.RedisUtils; | 17 | import com.tianbo.warehouse.util.RedisUtils; |
24 | import io.swagger.annotations.Api; | 18 | import io.swagger.annotations.Api; |
25 | import io.swagger.annotations.ApiImplicitParam; | 19 | import io.swagger.annotations.ApiImplicitParam; |
26 | import io.swagger.annotations.ApiImplicitParams; | 20 | import io.swagger.annotations.ApiImplicitParams; |
27 | import io.swagger.annotations.ApiOperation; | 21 | import io.swagger.annotations.ApiOperation; |
28 | import lombok.extern.slf4j.Slf4j; | 22 | import lombok.extern.slf4j.Slf4j; |
23 | +import net.sf.json.JSONObject; | ||
29 | import org.springframework.beans.factory.annotation.Autowired; | 24 | import org.springframework.beans.factory.annotation.Autowired; |
30 | -import org.springframework.context.annotation.Bean; | ||
31 | import org.springframework.security.core.context.SecurityContextHolder; | 25 | import org.springframework.security.core.context.SecurityContextHolder; |
32 | import org.springframework.security.core.userdetails.UserDetails; | 26 | import org.springframework.security.core.userdetails.UserDetails; |
33 | import org.springframework.validation.BindingResult; | 27 | import org.springframework.validation.BindingResult; |
34 | import org.springframework.validation.annotation.Validated; | 28 | import org.springframework.validation.annotation.Validated; |
35 | import org.springframework.web.bind.annotation.*; | 29 | import org.springframework.web.bind.annotation.*; |
36 | - | ||
37 | -import javax.annotation.Resource; | ||
38 | -import javax.imageio.ImageIO; | ||
39 | import javax.servlet.ServletOutputStream; | 30 | import javax.servlet.ServletOutputStream; |
40 | import javax.servlet.http.HttpServletRequest; | 31 | import javax.servlet.http.HttpServletRequest; |
41 | import javax.servlet.http.HttpServletResponse; | 32 | import javax.servlet.http.HttpServletResponse; |
42 | -import javax.servlet.http.HttpSession; | ||
43 | -import java.awt.image.BufferedImage; | ||
44 | -import java.io.IOException; | ||
45 | import java.util.List; | 33 | import java.util.List; |
46 | import java.util.Map; | 34 | import java.util.Map; |
47 | -import java.util.Properties; | ||
48 | 35 | ||
49 | @RestController | 36 | @RestController |
50 | @Slf4j | 37 | @Slf4j |
@@ -175,5 +162,4 @@ public class UserController { | @@ -175,5 +162,4 @@ public class UserController { | ||
175 | } | 162 | } |
176 | return new ResultJson("500","缓存更新失败"); | 163 | return new ResultJson("500","缓存更新失败"); |
177 | } | 164 | } |
178 | - | ||
179 | } | 165 | } |
1 | +package com.tianbo.warehouse.controller.response; | ||
2 | + | ||
3 | +import lombok.Data; | ||
4 | + | ||
5 | +import java.io.Serializable; | ||
6 | + | ||
7 | +/** | ||
8 | + * @author shenhailong | ||
9 | + * <p> 返回统一认证接口 | ||
10 | + * 2020/12/22/16:41 | ||
11 | + */ | ||
12 | +@Data | ||
13 | +public class ResultMessage implements Serializable { | ||
14 | + | ||
15 | + private static final long serialVersionUID = 1L; | ||
16 | + | ||
17 | + private String resultCode; | ||
18 | + | ||
19 | + private String resultMessage; | ||
20 | + | ||
21 | + public ResultMessage(String resultCode, String resultMessage) { | ||
22 | + this.resultCode = resultCode; | ||
23 | + this.resultMessage = resultMessage; | ||
24 | + } | ||
25 | + | ||
26 | +} |
1 | package com.tianbo.warehouse.dao; | 1 | package com.tianbo.warehouse.dao; |
2 | 2 | ||
3 | import com.tianbo.warehouse.model.KakoUser; | 3 | import com.tianbo.warehouse.model.KakoUser; |
4 | +import org.apache.ibatis.annotations.Param; | ||
4 | 5 | ||
5 | import java.util.List; | 6 | import java.util.List; |
6 | 7 | ||
@@ -23,5 +24,12 @@ public interface KakoUserMapper { | @@ -23,5 +24,12 @@ public interface KakoUserMapper { | ||
23 | 24 | ||
24 | int lockUser(KakoUser record); | 25 | int lockUser(KakoUser record); |
25 | 26 | ||
27 | + KakoUser findLonginName(@Param(value = "loginName") String loginName, | ||
28 | + @Param(value = "userSyncId") String userSyncId); | ||
29 | + | ||
30 | + int updateSyncUserLoginName(KakoUser kakoUser); | ||
31 | + int updateSyncUserId(KakoUser kakoUser); | ||
32 | + | ||
33 | + | ||
26 | 34 | ||
27 | } | 35 | } |
@@ -5,6 +5,7 @@ import com.alibaba.fastjson.serializer.SerializerFeature; | @@ -5,6 +5,7 @@ import com.alibaba.fastjson.serializer.SerializerFeature; | ||
5 | import com.tianbo.warehouse.service.validated.InsertUser; | 5 | import com.tianbo.warehouse.service.validated.InsertUser; |
6 | import com.tianbo.warehouse.service.validated.UpdateUser; | 6 | import com.tianbo.warehouse.service.validated.UpdateUser; |
7 | import com.tianbo.warehouse.validate.CheckUserExist; | 7 | import com.tianbo.warehouse.validate.CheckUserExist; |
8 | +import lombok.Data; | ||
8 | import org.hibernate.validator.constraints.Length; | 9 | import org.hibernate.validator.constraints.Length; |
9 | import org.springframework.security.core.GrantedAuthority; | 10 | import org.springframework.security.core.GrantedAuthority; |
10 | import org.springframework.security.core.authority.SimpleGrantedAuthority; | 11 | import org.springframework.security.core.authority.SimpleGrantedAuthority; |
@@ -17,6 +18,7 @@ import java.util.Collection; | @@ -17,6 +18,7 @@ import java.util.Collection; | ||
17 | import java.util.Date; | 18 | import java.util.Date; |
18 | import java.util.List; | 19 | import java.util.List; |
19 | 20 | ||
21 | +@Data | ||
20 | public class KakoUser implements UserDetails { | 22 | public class KakoUser implements UserDetails { |
21 | private String id; | 23 | private String id; |
22 | 24 | ||
@@ -33,6 +35,8 @@ public class KakoUser implements UserDetails { | @@ -33,6 +35,8 @@ public class KakoUser implements UserDetails { | ||
33 | @Length(min = 6, max = 22, message = "密码 长度必须在 {min} - {max} 之间",groups=InsertUser.class) | 35 | @Length(min = 6, max = 22, message = "密码 长度必须在 {min} - {max} 之间",groups=InsertUser.class) |
34 | private String password; | 36 | private String password; |
35 | 37 | ||
38 | + private String oldPassword; | ||
39 | + | ||
36 | private String no; | 40 | private String no; |
37 | 41 | ||
38 | private String name; | 42 | private String name; |
@@ -67,6 +71,8 @@ public class KakoUser implements UserDetails { | @@ -67,6 +71,8 @@ public class KakoUser implements UserDetails { | ||
67 | 71 | ||
68 | private String token; | 72 | private String token; |
69 | 73 | ||
74 | + private String userSyncId; | ||
75 | + | ||
70 | @JSONField(serialzeFeatures= {SerializerFeature.WriteMapNullValue,SerializerFeature.WriteNullStringAsEmpty}) | 76 | @JSONField(serialzeFeatures= {SerializerFeature.WriteMapNullValue,SerializerFeature.WriteNullStringAsEmpty}) |
71 | private List<ROLE> roles; | 77 | private List<ROLE> roles; |
72 | 78 |
1 | +package com.tianbo.warehouse.model.resource; | ||
2 | + | ||
3 | +import lombok.Data; | ||
4 | + | ||
5 | +/** | ||
6 | + * @author shenhailong | ||
7 | + * <p> | ||
8 | + * 2020/12/22/15:10 | ||
9 | + */ | ||
10 | +@Data | ||
11 | +public class UserDate { | ||
12 | + | ||
13 | + private String USER_ID; | ||
14 | + | ||
15 | + private String USER_NAME; | ||
16 | + | ||
17 | + private String USER_NO; | ||
18 | + | ||
19 | + private String MOBILE; | ||
20 | + | ||
21 | + private String IDCARD_NO; | ||
22 | + | ||
23 | + private String COUNTRY; | ||
24 | + | ||
25 | + private String SEX; | ||
26 | + | ||
27 | + private String NATIONALITY; | ||
28 | + | ||
29 | + private String OFFICE_PHONE; | ||
30 | + | ||
31 | + private String OFFICE_ADDRESS; | ||
32 | + | ||
33 | + private String EMAIL; | ||
34 | + | ||
35 | + private String JOB_LEVEL; | ||
36 | + | ||
37 | + private String JOB_TITLE; | ||
38 | + | ||
39 | + private String JOB_TYPE; | ||
40 | + | ||
41 | + private String JOB_STATUS; | ||
42 | + | ||
43 | + private String JOB_POSITION; | ||
44 | + | ||
45 | + private String SECRET_LEVEL; | ||
46 | + | ||
47 | + private String USER_POST; | ||
48 | + | ||
49 | + private String USER_JOB_ID; | ||
50 | + | ||
51 | + private String ORG_ID; | ||
52 | + | ||
53 | + private String USER_TITLE; | ||
54 | + | ||
55 | + private String LOGIN_NAME; | ||
56 | + | ||
57 | + private String SHOW_ORDER; | ||
58 | + | ||
59 | + private String REMARK; | ||
60 | + | ||
61 | + private String NAIL_ID; | ||
62 | + | ||
63 | + private String PK_PSNDOC; | ||
64 | + | ||
65 | + private String PWD; | ||
66 | + | ||
67 | + private String PWD_ENCRYPT; | ||
68 | + | ||
69 | + private String UPDATE_TIME; | ||
70 | + | ||
71 | + | ||
72 | +} |
1 | +package com.tianbo.warehouse.model.resource; | ||
2 | + | ||
3 | +import lombok.Data; | ||
4 | + | ||
5 | +import java.io.Serializable; | ||
6 | +import java.util.List; | ||
7 | + | ||
8 | +/** | ||
9 | + * @author shenhailong | ||
10 | + * <p> | ||
11 | + * 2020/12/22/15:08 | ||
12 | + */ | ||
13 | +@Data | ||
14 | +public class UserSync{ | ||
15 | + | ||
16 | + private String action; | ||
17 | + | ||
18 | + private String resType; | ||
19 | + | ||
20 | + private String syncTime; | ||
21 | + | ||
22 | + private List data; | ||
23 | + | ||
24 | + | ||
25 | +} |
@@ -5,12 +5,15 @@ import com.tianbo.warehouse.model.KakoUser; | @@ -5,12 +5,15 @@ import com.tianbo.warehouse.model.KakoUser; | ||
5 | import com.tianbo.warehouse.model.USERS; | 5 | import com.tianbo.warehouse.model.USERS; |
6 | import com.tianbo.warehouse.security.CustomUserDetailService; | 6 | import com.tianbo.warehouse.security.CustomUserDetailService; |
7 | import com.tianbo.warehouse.service.UserService; | 7 | import com.tianbo.warehouse.service.UserService; |
8 | +import com.tianbo.warehouse.util.KIAM.SM3EncryptUtil; | ||
8 | import com.tianbo.warehouse.util.RedisUtils; | 9 | import com.tianbo.warehouse.util.RedisUtils; |
9 | import org.apache.shiro.codec.CodecException; | 10 | import org.apache.shiro.codec.CodecException; |
10 | import org.apache.shiro.crypto.hash.Hash; | 11 | import org.apache.shiro.crypto.hash.Hash; |
11 | import org.apache.shiro.crypto.hash.SimpleHash; | 12 | import org.apache.shiro.crypto.hash.SimpleHash; |
12 | import org.apache.shiro.util.ByteSource; | 13 | import org.apache.shiro.util.ByteSource; |
14 | +import org.bouncycastle.jcajce.provider.digest.SM3; | ||
13 | import org.springframework.beans.factory.annotation.Autowired; | 15 | import org.springframework.beans.factory.annotation.Autowired; |
16 | +import org.springframework.beans.factory.annotation.Value; | ||
14 | import org.springframework.security.authentication.AuthenticationProvider; | 17 | import org.springframework.security.authentication.AuthenticationProvider; |
15 | import org.springframework.security.authentication.BadCredentialsException; | 18 | import org.springframework.security.authentication.BadCredentialsException; |
16 | import org.springframework.security.authentication.DisabledException; | 19 | import org.springframework.security.authentication.DisabledException; |
@@ -43,6 +46,9 @@ public class MyLoginAuthenticationProvider extends CodecSupport implements Authe | @@ -43,6 +46,9 @@ public class MyLoginAuthenticationProvider extends CodecSupport implements Authe | ||
43 | @Autowired | 46 | @Autowired |
44 | private RedisUtils redisUtils; | 47 | private RedisUtils redisUtils; |
45 | 48 | ||
49 | +// @Value("${http.serverStatus}") | ||
50 | +// private String serverStatus; | ||
51 | + | ||
46 | @Override | 52 | @Override |
47 | public Authentication authenticate(Authentication authentication) throws AuthenticationException { | 53 | public Authentication authenticate(Authentication authentication) throws AuthenticationException { |
48 | // 获取前端表单中输入后返回的用户名、密码 | 54 | // 获取前端表单中输入后返回的用户名、密码 |
@@ -51,7 +57,6 @@ public class MyLoginAuthenticationProvider extends CodecSupport implements Authe | @@ -51,7 +57,6 @@ public class MyLoginAuthenticationProvider extends CodecSupport implements Authe | ||
51 | 57 | ||
52 | UserDetails userInfo = userDetailsService.loadUserByUsername(userName); | 58 | UserDetails userInfo = userDetailsService.loadUserByUsername(userName); |
53 | 59 | ||
54 | - | ||
55 | //验证登录密码是否符合规则,如位数包含的字符等 | 60 | //验证登录密码是否符合规则,如位数包含的字符等 |
56 | boolean isValid = PasswordSaltUtils.isValidPassword(password, userInfo.getPassword(), ""); | 61 | boolean isValid = PasswordSaltUtils.isValidPassword(password, userInfo.getPassword(), ""); |
57 | // 验证密码 | 62 | // 验证密码 |
@@ -64,11 +69,12 @@ public class MyLoginAuthenticationProvider extends CodecSupport implements Authe | @@ -64,11 +69,12 @@ public class MyLoginAuthenticationProvider extends CodecSupport implements Authe | ||
64 | } | 69 | } |
65 | 70 | ||
66 | //取盐规则 | 71 | //取盐规则 |
67 | - byte[] salt = PasswordSaltUtils.getSalt16(userInfo.getPassword()); | 72 | +// byte[] salt = PasswordSaltUtils.getSalt16(userInfo.getPassword()); |
68 | //真实密码 | 73 | //真实密码 |
69 | - String realPass = PasswordSaltUtils.getPassword16(userInfo.getPassword()); | 74 | +// String realPass = PasswordSaltUtils.getPassword16(userInfo.getPassword()); |
70 | //用户登录密码与盐运算 | 75 | //用户登录密码与盐运算 |
71 | - Object tokenHashedCredentials = this.hashProvidedCredentials(password,salt,1024); | 76 | +// Object tokenHashedCredentials = this.hashProvidedCredentials(password,salt,1024); |
77 | + | ||
72 | 78 | ||
73 | try{ | 79 | try{ |
74 | String loginUserLock = redisUtils.get(userName+"-lock"); | 80 | String loginUserLock = redisUtils.get(userName+"-lock"); |
@@ -77,7 +83,9 @@ public class MyLoginAuthenticationProvider extends CodecSupport implements Authe | @@ -77,7 +83,9 @@ public class MyLoginAuthenticationProvider extends CodecSupport implements Authe | ||
77 | loginUserLockNO = Integer.valueOf(loginUserLock); | 83 | loginUserLockNO = Integer.valueOf(loginUserLock); |
78 | } | 84 | } |
79 | //判断密码是否正确 | 85 | //判断密码是否正确 |
80 | - if(!equals(realPass,tokenHashedCredentials.toString())){ | 86 | +// if(!equals(realPass,tokenHashedCredentials.toString())){ |
87 | + // sm3加密 | ||
88 | + if(!equals(SM3EncryptUtil.passwordSm3(password),userInfo.getPassword())){ | ||
81 | redisUtils.set(userName+"-lock", String.valueOf(++loginUserLockNO),120); | 89 | redisUtils.set(userName+"-lock", String.valueOf(++loginUserLockNO),120); |
82 | 90 | ||
83 | //两分钟内错误登录次数超过5次锁定账户 | 91 | //两分钟内错误登录次数超过5次锁定账户 |
@@ -92,8 +100,6 @@ public class MyLoginAuthenticationProvider extends CodecSupport implements Authe | @@ -92,8 +100,6 @@ public class MyLoginAuthenticationProvider extends CodecSupport implements Authe | ||
92 | }catch (Exception e){ | 100 | }catch (Exception e){ |
93 | throw new BadCredentialsException("验证失败!"); | 101 | throw new BadCredentialsException("验证失败!"); |
94 | } | 102 | } |
95 | - | ||
96 | - | ||
97 | // 前后端分离情况下 处理逻辑... | 103 | // 前后端分离情况下 处理逻辑... |
98 | // 更新登录令牌 - 之后访问系统其它接口直接通过token认证用户权限... | 104 | // 更新登录令牌 - 之后访问系统其它接口直接通过token认证用户权限... |
99 | return new UsernamePasswordAuthenticationToken(userInfo, password, userInfo.getAuthorities()); | 105 | return new UsernamePasswordAuthenticationToken(userInfo, password, userInfo.getAuthorities()); |
@@ -3,21 +3,22 @@ package com.tianbo.warehouse.service.kakoImp; | @@ -3,21 +3,22 @@ package com.tianbo.warehouse.service.kakoImp; | ||
3 | import com.github.pagehelper.Page; | 3 | import com.github.pagehelper.Page; |
4 | import com.github.pagehelper.PageHelper; | 4 | import com.github.pagehelper.PageHelper; |
5 | import com.github.pagehelper.PageInfo; | 5 | import com.github.pagehelper.PageInfo; |
6 | +import com.google.gson.Gson; | ||
6 | import com.tianbo.warehouse.dao.KakoUserMapper; | 7 | import com.tianbo.warehouse.dao.KakoUserMapper; |
7 | import com.tianbo.warehouse.dao.ROLEMapper; | 8 | import com.tianbo.warehouse.dao.ROLEMapper; |
8 | import com.tianbo.warehouse.dao.UserRoleMapper; | 9 | import com.tianbo.warehouse.dao.UserRoleMapper; |
9 | import com.tianbo.warehouse.model.*; | 10 | import com.tianbo.warehouse.model.*; |
10 | -import com.tianbo.warehouse.security.handel.kakologin.Digests; | ||
11 | -import com.tianbo.warehouse.security.handel.kakologin.Encodes; | 11 | +import com.tianbo.warehouse.model.resource.ResultPwd; |
12 | import com.tianbo.warehouse.service.PermissionService; | 12 | import com.tianbo.warehouse.service.PermissionService; |
13 | +import com.tianbo.warehouse.util.KIAM.HttpClientUtil; | ||
14 | +import com.tianbo.warehouse.util.KIAM.SM3EncryptUtil; | ||
15 | +import com.tianbo.warehouse.util.KIAM.SM4EncryptUtil; | ||
13 | import org.springframework.beans.factory.annotation.Autowired; | 16 | import org.springframework.beans.factory.annotation.Autowired; |
14 | import org.springframework.stereotype.Service; | 17 | import org.springframework.stereotype.Service; |
15 | import org.springframework.transaction.annotation.Transactional; | 18 | import org.springframework.transaction.annotation.Transactional; |
16 | 19 | ||
17 | import javax.annotation.Resource; | 20 | import javax.annotation.Resource; |
18 | -import java.util.Date; | ||
19 | -import java.util.List; | ||
20 | -import java.util.UUID; | 21 | +import java.util.*; |
21 | 22 | ||
22 | @Service | 23 | @Service |
23 | public class KakoUserServiceImpl implements KakoUserService{ | 24 | public class KakoUserServiceImpl implements KakoUserService{ |
@@ -79,11 +80,28 @@ public class KakoUserServiceImpl implements KakoUserService{ | @@ -79,11 +80,28 @@ public class KakoUserServiceImpl implements KakoUserService{ | ||
79 | public int updateByPrimaryKeySelective(KakoUser record){ | 80 | public int updateByPrimaryKeySelective(KakoUser record){ |
80 | int i = 0; | 81 | int i = 0; |
81 | if(record.getPassword()!=null && !record.getPassword().isEmpty()){ | 82 | if(record.getPassword()!=null && !record.getPassword().isEmpty()){ |
82 | - String entryPassWord = entryptPassword(record.getPassword()); | ||
83 | - record.setPassword(entryPassWord); | ||
84 | - } | ||
85 | - if (record!=null){ | ||
86 | - i = kakoUserMapper.updateByPrimaryKeySelective(record); | 83 | +// String entryPassWord = entryptPassword(record.getPassword()); |
84 | + // 更改运用sm3加密 | ||
85 | + try { | ||
86 | + // 通知统一认证接口 新密码 旧密码 运用sm4加密推送 | ||
87 | + Map<String, Object> map = new HashMap<>(); | ||
88 | + SM4EncryptUtil sm4EncryptUtil = new SM4EncryptUtil(); | ||
89 | + | ||
90 | + String OLD_PWD = sm4EncryptUtil.pwd(record.getOldPassword()); | ||
91 | + map.put("OLD_PWD", OLD_PWD); | ||
92 | + String NEW_PWD = sm4EncryptUtil.pwd(record.getPassword()); | ||
93 | + map.put("NEW_PWD", NEW_PWD); | ||
94 | + String result = HttpClientUtil.httpPost("http://10.5.14.103:28080/urm/service/V3/res/user/updatePwd?userId="+record.getUserSyncId(), map); | ||
95 | + Gson gson = new Gson(); | ||
96 | + ResultPwd resultPwd = gson.fromJson(result, ResultPwd.class); | ||
97 | + if ("true".equals(resultPwd.getResult())){ | ||
98 | + record.setPassword(SM3EncryptUtil.passwordSm3(record.getPassword())); | ||
99 | + i = kakoUserMapper.updateByPrimaryKeySelective(record); | ||
100 | + } | ||
101 | + | ||
102 | + }catch (Exception e){ | ||
103 | + e.printStackTrace(); | ||
104 | + } | ||
87 | } | 105 | } |
88 | return i; | 106 | return i; |
89 | 107 | ||
@@ -100,8 +118,13 @@ public class KakoUserServiceImpl implements KakoUserService{ | @@ -100,8 +118,13 @@ public class KakoUserServiceImpl implements KakoUserService{ | ||
100 | return 0; | 118 | return 0; |
101 | } | 119 | } |
102 | if(record.getPassword()!=null && !record.getPassword().isEmpty()){ | 120 | if(record.getPassword()!=null && !record.getPassword().isEmpty()){ |
103 | - String entryPassWord = entryptPassword(record.getPassword()); | ||
104 | - record.setPassword(entryPassWord); | 121 | +// String entryPassWord = entryptPassword(record.getPassword()); |
122 | + // 更改用户密码加密方式 sm3加密 跟base64 | ||
123 | + try { | ||
124 | + record.setPassword(SM3EncryptUtil.passwordSm3(record.getPassword())); | ||
125 | + }catch (Exception e){ | ||
126 | + e.printStackTrace(); | ||
127 | + } | ||
105 | } | 128 | } |
106 | record.setCreateDate(new Date()); | 129 | record.setCreateDate(new Date()); |
107 | record.setUpdateDate(new Date()); | 130 | record.setUpdateDate(new Date()); |
@@ -113,12 +136,12 @@ public class KakoUserServiceImpl implements KakoUserService{ | @@ -113,12 +136,12 @@ public class KakoUserServiceImpl implements KakoUserService{ | ||
113 | /** | 136 | /** |
114 | * 生成安全的密码,生成随机的16位salt并经过1024次 sha-1 hash | 137 | * 生成安全的密码,生成随机的16位salt并经过1024次 sha-1 hash |
115 | */ | 138 | */ |
116 | - public static String entryptPassword(String plainPassword) { | ||
117 | - String plain = Encodes.unescapeHtml(plainPassword); | ||
118 | - byte[] salt = Digests.generateSalt(SALT_SIZE); | ||
119 | - byte[] hashPassword = Digests.sha1(plain.getBytes(), salt, HASH_INTERATIONS); | ||
120 | - return Encodes.encodeHex(salt)+Encodes.encodeHex(hashPassword); | ||
121 | - } | 139 | +// public static String entryptPassword(String plainPassword) { |
140 | +// String plain = Encodes.unescapeHtml(plainPassword); | ||
141 | +// byte[] salt = Digests.generateSalt(SALT_SIZE); | ||
142 | +// byte[] hashPassword = Digests.sha1(plain.getBytes(), salt, HASH_INTERATIONS); | ||
143 | +// return Encodes.encodeHex(salt)+Encodes.encodeHex(hashPassword); | ||
144 | +// } | ||
122 | 145 | ||
123 | /** | 146 | /** |
124 | * 检查是否存在用户 | 147 | * 检查是否存在用户 |
1 | +package com.tianbo.warehouse.util; | ||
2 | + | ||
3 | +import com.alibaba.fastjson.JSON; | ||
4 | +import com.alibaba.fastjson.JSONArray; | ||
5 | +import com.alibaba.fastjson.JSONObject; | ||
6 | + | ||
7 | +import java.util.List; | ||
8 | +import java.util.Map; | ||
9 | + | ||
10 | +/** | ||
11 | + * @author shenhailong | ||
12 | + * <p> | ||
13 | + * 2020/12/16/16:12 | ||
14 | + */ | ||
15 | +public class JsonUtil<T> { | ||
16 | + | ||
17 | + public static JSONObject mapToJson(Map<String, Object> map) { | ||
18 | + String data = JSON.toJSONString(map); | ||
19 | + return JSON.parseObject(data); | ||
20 | + } | ||
21 | + /** | ||
22 | + * map中取key对应的value | ||
23 | + * @param map | ||
24 | + * @param key | ||
25 | + * @return | ||
26 | + */ | ||
27 | + public String mapToString(Map<String, Object> map, String key) { | ||
28 | + JSONObject jsonObject = mapToJson(map); | ||
29 | + return jsonObject.getString(key); | ||
30 | + } | ||
31 | + /** | ||
32 | + * map中取类对象 | ||
33 | + * @param map | ||
34 | + * @param clazz | ||
35 | + * @param key | ||
36 | + * @param <T> | ||
37 | + * @return | ||
38 | + */ | ||
39 | + public static <T> T mapToObject(Map<String, Object> map, Class<T> clazz, String key) { | ||
40 | + T t = null; | ||
41 | + JSONObject jsonObject = mapToJson(map); | ||
42 | + JSONObject object = jsonObject.getJSONObject(key); | ||
43 | + t = object.toJavaObject(clazz); | ||
44 | + return t; | ||
45 | + } | ||
46 | + /** | ||
47 | + * map中取list | ||
48 | + * @param map | ||
49 | + * @param clazz | ||
50 | + * @param key | ||
51 | + * @return | ||
52 | + */ | ||
53 | + public List<T> mapToList(Map<String, Object> map, Class<T> clazz, String key) { | ||
54 | + List<T> t = null; | ||
55 | + JSONObject jsonObject = mapToJson(map); | ||
56 | + JSONArray array = jsonObject.getJSONArray(key); | ||
57 | + t = array.toJavaList(clazz); | ||
58 | + return t; | ||
59 | + } | ||
60 | + | ||
61 | + | ||
62 | +} |
1 | +package com.tianbo.warehouse.util.KIAM; | ||
2 | + | ||
3 | + | ||
4 | +import lombok.extern.slf4j.Slf4j; | ||
5 | +import org.apache.commons.httpclient.HttpClient; | ||
6 | +import org.apache.commons.httpclient.HttpException; | ||
7 | +import org.apache.commons.httpclient.HttpStatus; | ||
8 | +import org.apache.commons.httpclient.NameValuePair; | ||
9 | +import org.apache.commons.httpclient.methods.GetMethod; | ||
10 | +import org.apache.commons.httpclient.methods.PostMethod; | ||
11 | +import org.apache.commons.httpclient.params.HttpMethodParams; | ||
12 | + | ||
13 | +import java.io.IOException; | ||
14 | +import java.util.Iterator; | ||
15 | +import java.util.Map; | ||
16 | + | ||
17 | +@Slf4j | ||
18 | +public class HttpClientUtil { | ||
19 | + | ||
20 | + | ||
21 | + public static String httpReader(String url, String code) { | ||
22 | + System.out.println("GetPage:" + url); | ||
23 | + | ||
24 | + HttpClient client = new HttpClient(); | ||
25 | + GetMethod method = new GetMethod(url); | ||
26 | + String result = null; | ||
27 | + try { | ||
28 | + client.executeMethod(method); | ||
29 | + int status = method.getStatusCode(); | ||
30 | + if (status == HttpStatus.SC_OK) { | ||
31 | + result = method.getResponseBodyAsString(); | ||
32 | + } else { | ||
33 | + System.out.println("Method failed: " + method.getStatusLine()); | ||
34 | + } | ||
35 | + } catch (HttpException e) { | ||
36 | + // �����������쳣��������Э�鲻�Ի��߷��ص����������� | ||
37 | + System.out.println("Please check your provided http address!"); | ||
38 | + e.printStackTrace(); | ||
39 | + } catch (IOException e) { | ||
40 | + // ���������쳣 | ||
41 | + System.out.println("���������쳣��"); | ||
42 | + e.printStackTrace(); | ||
43 | + } finally { | ||
44 | + // �ͷ����� | ||
45 | + if (method != null) { | ||
46 | + method.releaseConnection(); | ||
47 | + } | ||
48 | + method = null; | ||
49 | + client = null; | ||
50 | + } | ||
51 | + return result; | ||
52 | + } | ||
53 | + | ||
54 | + public static String httpGet(String url, String code) { | ||
55 | + log.info("GetPage:{}"+ url); | ||
56 | + String content = null; | ||
57 | + HttpClient httpClient = new HttpClient(); | ||
58 | + //����header | ||
59 | + httpClient.getParams().setParameter(HttpMethodParams.USER_AGENT, "Mozilla/5.0 (X11; U; Linux i686; zh-CN; rv:1.9.1.2) Gecko/20090803 Fedora/3.5.2-2.fc11 Firefox/3.5.2"); | ||
60 | + GetMethod method = new GetMethod(url); | ||
61 | + try { | ||
62 | + int statusCode = httpClient.executeMethod(method); | ||
63 | + System.out.println("httpClientUtils::statusCode=" + statusCode); | ||
64 | + System.out.println(method.getStatusLine()); | ||
65 | + content = new String(method.getResponseBody(), code); | ||
66 | + | ||
67 | + } catch (Exception e) { | ||
68 | + System.out.println("time out"); | ||
69 | + e.printStackTrace(); | ||
70 | + } finally { | ||
71 | + if (method != null) method.releaseConnection(); | ||
72 | + method = null; | ||
73 | + httpClient = null; | ||
74 | + } | ||
75 | + return content; | ||
76 | + } | ||
77 | + | ||
78 | + public static String httpPost(String url, Map paramMap, String code) { | ||
79 | + System.out.println("GetPage:" + url); | ||
80 | + String content = null; | ||
81 | + if (url == null || url.trim().length() == 0 || paramMap == null | ||
82 | + || paramMap.isEmpty()) { | ||
83 | + return null; | ||
84 | + } | ||
85 | + HttpClient httpClient = new HttpClient(); | ||
86 | + //����header | ||
87 | + httpClient.getParams().setParameter(HttpMethodParams.USER_AGENT, "Mozilla/5.0 (X11; U; Linux i686; zh-CN; rv:1.9.1.2) Gecko/20090803 Fedora/3.5.2-2.fc11 Firefox/3.5.2");// | ||
88 | + | ||
89 | + //�������� | ||
90 | + //httpClient.getHostConfiguration().setProxy("128.128.176.74", 808); | ||
91 | + | ||
92 | + PostMethod method = new PostMethod(url); | ||
93 | + Iterator it = paramMap.keySet().iterator(); | ||
94 | + | ||
95 | + | ||
96 | + while (it.hasNext()) { | ||
97 | + String key = it.next() + ""; | ||
98 | + Object o = paramMap.get(key); | ||
99 | + if (o != null && o instanceof String) { | ||
100 | + method.addParameter(new NameValuePair(key, o.toString())); | ||
101 | + } | ||
102 | + if (o != null && o instanceof String[]) { | ||
103 | + String[] s = (String[]) o; | ||
104 | + if (s != null) { | ||
105 | + for (int i = 0; i < s.length; i++) { | ||
106 | + method.addParameter(new NameValuePair(key, s[i])); | ||
107 | + } | ||
108 | + } | ||
109 | + } | ||
110 | + } | ||
111 | + try { | ||
112 | + | ||
113 | + int statusCode = httpClient.executeMethod(method); | ||
114 | + | ||
115 | + System.out.println("httpClientUtils::statusCode=" + statusCode); | ||
116 | + | ||
117 | + System.out.println(method.getStatusLine()); | ||
118 | + content = new String(method.getResponseBody(), code); | ||
119 | + | ||
120 | + } catch (Exception e) { | ||
121 | + System.out.println("time out"); | ||
122 | + e.printStackTrace(); | ||
123 | + } finally { | ||
124 | + if (method != null) { | ||
125 | + method.releaseConnection(); | ||
126 | + } | ||
127 | + method = null; | ||
128 | + httpClient = null; | ||
129 | + } | ||
130 | + return content; | ||
131 | + | ||
132 | + } | ||
133 | + | ||
134 | + public static String httpPost(String url, Map paramMap) { | ||
135 | + //���룺UTF-8 | ||
136 | + return HttpClientUtil.httpPost(url, paramMap, "UTF-8"); | ||
137 | + } | ||
138 | +} |
1 | +package com.tianbo.warehouse.util.KIAM; | ||
2 | + | ||
3 | +import org.bouncycastle.crypto.digests.SM3Digest; | ||
4 | +import org.bouncycastle.jce.provider.BouncyCastleProvider; | ||
5 | + | ||
6 | +import java.io.UnsupportedEncodingException; | ||
7 | +import java.security.Security; | ||
8 | +import java.util.Base64; | ||
9 | + | ||
10 | +/** | ||
11 | + * sm3生成密码摘要 | ||
12 | + * 需要依赖 Bouncy Castle轻量级密码术包 1.60 | ||
13 | + */ | ||
14 | +public class SM3EncryptUtil { | ||
15 | + | ||
16 | + private static byte[] SECRET_KEY = {101, 87, 99, 10, 34, 45, 77, 76, 98, 13, 12, 18, 73, 84, 91, 93}; | ||
17 | + | ||
18 | + public static byte[] hash(byte[] srcData) { | ||
19 | + SM3Digest digest = new SM3Digest(); | ||
20 | + digest.update(srcData, 0, srcData.length); | ||
21 | + byte[] hash = new byte[digest.getDigestSize()]; | ||
22 | + digest.doFinal(hash, 0); | ||
23 | + return hash; | ||
24 | + } | ||
25 | + | ||
26 | + public static String bytetoString(byte[] digest) { | ||
27 | + String str = ""; | ||
28 | + String tempStr = ""; | ||
29 | + for (int i = 0; i < digest.length; i++) { | ||
30 | + tempStr = (Integer.toHexString(digest[i] & 0xff)); | ||
31 | + if (tempStr.length() == 1) { | ||
32 | + str = str + "0" + tempStr; | ||
33 | + } else { | ||
34 | + str = str + tempStr; | ||
35 | + } | ||
36 | + } | ||
37 | + return str.toLowerCase(); | ||
38 | + } | ||
39 | + | ||
40 | +// public static void main(String[] args) { | ||
41 | +// Security.addProvider(new BouncyCastleProvider()); | ||
42 | +// try { | ||
43 | +// String pwdDigest = passwordSm3("vmvnv1v2VV."); | ||
44 | +// System.out.println(pwdDigest); | ||
45 | +// } catch (Exception e) { | ||
46 | +// e.printStackTrace(); | ||
47 | +// } | ||
48 | +// } | ||
49 | + | ||
50 | + /** | ||
51 | + * 密码SM3加密 | ||
52 | + * @param password | ||
53 | + * @return | ||
54 | + */ | ||
55 | + public static String passwordSm3(String password){ | ||
56 | + Security.addProvider(new BouncyCastleProvider()); | ||
57 | + try { | ||
58 | + byte[] pwdBytes = password.getBytes("UTF-8"); | ||
59 | + byte[] pwdDigest = hash(pwdBytes); | ||
60 | + return bytetoString(pwdDigest); | ||
61 | + } catch (Exception e) { | ||
62 | + e.printStackTrace(); | ||
63 | + return null; | ||
64 | + } | ||
65 | + } | ||
66 | + | ||
67 | +} |
1 | +package com.tianbo.warehouse.util.KIAM; | ||
2 | + | ||
3 | +import com.tianbo.warehouse.model.resource.ResultPwd; | ||
4 | +import org.bouncycastle.jce.provider.BouncyCastleProvider; | ||
5 | + | ||
6 | +import javax.crypto.BadPaddingException; | ||
7 | +import javax.crypto.Cipher; | ||
8 | +import javax.crypto.IllegalBlockSizeException; | ||
9 | +import javax.crypto.NoSuchPaddingException; | ||
10 | +import javax.crypto.spec.SecretKeySpec; | ||
11 | +import java.security.*; | ||
12 | +import java.util.Base64; | ||
13 | + | ||
14 | +/** | ||
15 | + * sm4加密工具 | ||
16 | + * 需要依赖 Bouncy Castle轻量级密码术包 1.60 | ||
17 | + */ | ||
18 | +public class SM4EncryptUtil { | ||
19 | + | ||
20 | + /** | ||
21 | + * 密钥, 禁止修改 | ||
22 | + */ | ||
23 | + private static byte[] SECRET_KEY = {101, 87, 99, 10, 34, 45, 77, 76, 98, 13, 12, 18, 73, 84, 91, 93}; | ||
24 | + | ||
25 | + /** | ||
26 | + * 解密方法 | ||
27 | + */ | ||
28 | + public static byte[] decryptSM4( byte[] cipherText) | ||
29 | + throws IllegalBlockSizeException, BadPaddingException, InvalidKeyException, | ||
30 | + NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException { | ||
31 | + return decryptSM4(SECRET_KEY, cipherText); | ||
32 | + } | ||
33 | + | ||
34 | + public static byte[] decryptSM4(byte[] key, byte[] cipherText) | ||
35 | + throws IllegalBlockSizeException, BadPaddingException, InvalidKeyException, | ||
36 | + NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException { | ||
37 | + Cipher cipher = generateEcbCipher("SM4/ECB/PKCS5Padding", Cipher.DECRYPT_MODE, key); | ||
38 | + return cipher.doFinal(cipherText); | ||
39 | + } | ||
40 | + | ||
41 | + /** | ||
42 | + * 解密方法 | ||
43 | + */ | ||
44 | + public static byte[] encryptSM4( byte[] cipherText) | ||
45 | + throws IllegalBlockSizeException, BadPaddingException, InvalidKeyException, | ||
46 | + NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException { | ||
47 | + return encryptSM4(SECRET_KEY, cipherText); | ||
48 | + } | ||
49 | + | ||
50 | + public static byte[] encryptSM4(byte[] key, byte[] data) | ||
51 | + throws InvalidKeyException, NoSuchAlgorithmException, NoSuchProviderException, | ||
52 | + NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException { | ||
53 | + Cipher cipher = generateEcbCipher("SM4/ECB/PKCS5Padding", Cipher.ENCRYPT_MODE, key); | ||
54 | + return cipher.doFinal(data); | ||
55 | + } | ||
56 | + | ||
57 | + private static Cipher generateEcbCipher(String algorithmName, int mode, byte[] key) | ||
58 | + throws NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException, | ||
59 | + InvalidKeyException { | ||
60 | + Cipher cipher = Cipher.getInstance(algorithmName, "BC"); | ||
61 | + Key sm4Key = new SecretKeySpec(key, "SM4"); | ||
62 | + cipher.init(mode, sm4Key); | ||
63 | + return cipher; | ||
64 | + } | ||
65 | + | ||
66 | + public static void main(String[] args) { | ||
67 | + Security.addProvider(new BouncyCastleProvider()); | ||
68 | + try { | ||
69 | + ResultPwd resultPwd = new ResultPwd(); | ||
70 | + resultPwd.setResult("123456"); | ||
71 | +// String pwd = "vmvnv1v2VV."; | ||
72 | + SM4EncryptUtil sm4EncryptUtil = new SM4EncryptUtil(); | ||
73 | + String pwd = sm4EncryptUtil.pwd("123456"); | ||
74 | + String encryptPwd = new String(Base64.getEncoder().encode(encryptSM4(pwd.getBytes("UTF-8")))); | ||
75 | + System.out.println("加密密码: " +encryptPwd); | ||
76 | + String originPwd = new String(decryptSM4(Base64.getDecoder().decode(encryptPwd))); | ||
77 | + System.out.println("解密密码: "+ originPwd); | ||
78 | + } catch (Exception e) { | ||
79 | + e.printStackTrace(); | ||
80 | + } | ||
81 | + } | ||
82 | + | ||
83 | + public String pwd(String pwd){ | ||
84 | + try { | ||
85 | + String encryptPwd = new String(Base64.getEncoder().encode(encryptSM4(pwd.getBytes("UTF-8")))); | ||
86 | + return encryptPwd; | ||
87 | + } catch (Exception e) { | ||
88 | + e.printStackTrace(); | ||
89 | + return ""; | ||
90 | + } | ||
91 | + } | ||
92 | + | ||
93 | +} |
1 | +package com.tianbo.warehouse.util.KIAM; | ||
2 | + | ||
3 | +import org.apache.http.HttpStatus; | ||
4 | +import org.apache.http.client.ClientProtocolException; | ||
5 | +import org.apache.http.client.config.RequestConfig; | ||
6 | +import org.apache.http.client.methods.CloseableHttpResponse; | ||
7 | +import org.apache.http.client.methods.HttpGet; | ||
8 | +import org.apache.http.client.methods.HttpPut; | ||
9 | +import org.apache.http.entity.StringEntity; | ||
10 | +import org.apache.http.impl.client.CloseableHttpClient; | ||
11 | +import org.apache.http.impl.client.HttpClients; | ||
12 | +import org.apache.http.util.EntityUtils; | ||
13 | +import org.slf4j.Logger; | ||
14 | +import org.slf4j.LoggerFactory; | ||
15 | +import com.google.gson.Gson; | ||
16 | +import java.io.IOException; | ||
17 | +import java.net.URI; | ||
18 | +import java.net.URISyntaxException; | ||
19 | +import java.util.Calendar; | ||
20 | +import java.util.HashMap; | ||
21 | +import java.util.Map; | ||
22 | +import org.apache.commons.lang3.RandomStringUtils; | ||
23 | +import org.apache.commons.lang3.StringUtils; | ||
24 | + | ||
25 | +/** | ||
26 | + * @author shenhailong | ||
27 | + * <p> | ||
28 | + * 2020/12/14/16:24 | ||
29 | + */ | ||
30 | +public class SignatureDemo { | ||
31 | + | ||
32 | + | ||
33 | + | ||
34 | + private static final Logger logger = LoggerFactory.getLogger(SignatureDemo.class); | ||
35 | + // 接口服务地址 | ||
36 | + static String restSever = "http://10.5.14.103:28087/upm/service/V1/auth/userApp"; | ||
37 | + //应用标识 | ||
38 | + static String appKey = "8744334580944896"; | ||
39 | + // 身份系统签发给应用对接的密钥 | ||
40 | + static String appPwd = "c6dddfab2b59d87f98c703d924f3718bb4350f17"; | ||
41 | + public static void doMain() { | ||
42 | + // 时间戳 | ||
43 | + Long ts = Calendar.getInstance().getTime().getTime(); | ||
44 | + // 随机数 | ||
45 | + String once = RandomStringUtils.randomAlphanumeric(32); | ||
46 | + // 接口header中的公共参数 | ||
47 | + String commonParamUrl = String.format("appKey=%s" + "&" + "ts=%s" + "&" + "once=%s", appKey, ts, once); | ||
48 | + // 创建HttpClient对象 | ||
49 | + CloseableHttpClient httpclient = HttpClients.createDefault(); | ||
50 | + /** | ||
51 | + * GET查询接口演示代码 | ||
52 | + */ | ||
53 | + String getQueryParam = "startTime=2019-06-21T08:00:00.000Z"; | ||
54 | + String getFullUrl = restSever + "?" + getQueryParam; | ||
55 | + HttpGet httpGet = new HttpGet(getFullUrl); | ||
56 | + // get请求查询参数,用在URL上的,这里若是通过ID查询的,接口中ID是作为路径存在的,所以需要将ID组合成 | ||
57 | + String getAllParamUrl = commonParamUrl + "&" + getQueryParam; | ||
58 | + // 对参数签名,并放入请求header中的signData参数中 | ||
59 | + try { | ||
60 | + // 签名数据 | ||
61 | + String signData = TokenUtils.getSignature(appPwd, getAllParamUrl); | ||
62 | + //添加header参数 appCode、timestamp、 signatureNonce、signature | ||
63 | + httpGet.addHeader("appKey", appKey); | ||
64 | + httpGet.addHeader("ts", ts.toString()); | ||
65 | + httpGet.addHeader("once", once); | ||
66 | + System.out.println("once:" + once); | ||
67 | + httpGet.addHeader("signData", signData); | ||
68 | + System.out.println("headers:" + httpGet.getAllHeaders()); | ||
69 | + String urlStr = httpGet.getURI().toString(); | ||
70 | + // 公共参数URL | ||
71 | + System.out.println("commonParamter:" + urlStr); | ||
72 | + if (StringUtils.endsWith(urlStr, "/")) { | ||
73 | + urlStr = StringUtils.removeEnd(urlStr, "/"); | ||
74 | + } | ||
75 | + httpGet.setURI(new URI(urlStr)); | ||
76 | + RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(3000).setConnectionRequestTimeout(3000).setSocketTimeout(3000).build(); | ||
77 | + httpGet.setConfig(requestConfig); | ||
78 | + System.out.println("urlStr in request:" + httpGet.getURI().toString()); | ||
79 | + // 执行请求 | ||
80 | + CloseableHttpResponse response = httpclient.execute(httpGet); | ||
81 | + // 取响应的结果 | ||
82 | + int statusCode = response.getStatusLine().getStatusCode(); | ||
83 | + // 打印响应结果 | ||
84 | + if (statusCode == HttpStatus.SC_OK) { | ||
85 | + String resp = EntityUtils.toString(response.getEntity(), "utf-8"); | ||
86 | + System.out.println("status:" + statusCode); | ||
87 | + System.out.println("result:" + resp); | ||
88 | + } | ||
89 | + } catch (URISyntaxException e) { | ||
90 | + logger.error("签名失败:", e); | ||
91 | + } catch (ClientProtocolException e) { | ||
92 | + e.printStackTrace(); | ||
93 | + } catch (IOException e) { | ||
94 | + e.printStackTrace(); | ||
95 | + } | ||
96 | + /** * PUT修改接口的演示代码,POST与PUT类似 */ | ||
97 | + String ID = "aa03a5c692cf480b87887e0ff8cfe566"; | ||
98 | + // 这里若是通过ID查询的,接口中ID是作为路径存在的,所以需要将ID组合成 | ||
99 | + String putQueryParam = "ID=" + ID; | ||
100 | + String putFullUrl = restSever + "/" + ID; | ||
101 | + // 访问用户接口 | ||
102 | + HttpPut httpPut = new HttpPut(putFullUrl); | ||
103 | + // 模拟POST/PUT的body中数据,需转为JSON进行签名。GET则没有这部分内容。 | ||
104 | + Map<String, Object> dataMap = new HashMap<String, Object>(); | ||
105 | + dataMap.put("USER_NAME", "张三"); | ||
106 | + String bodyParam = new Gson().toJson(dataMap); | ||
107 | + String postAllParamUrl = commonParamUrl + "&" + putQueryParam + "&bodyData=" + bodyParam; | ||
108 | + StringEntity bodyData = new StringEntity(bodyParam.toString(), "UTF-8"); | ||
109 | + httpPut.setEntity(bodyData); | ||
110 | + // 对参数签名,并放入请求header中的signData参数中 | ||
111 | + try { | ||
112 | + // 签名数据 | ||
113 | + String signData = TokenUtils.getSignature(appPwd, postAllParamUrl); | ||
114 | + // 添加header参数 appCode、timestamp、 signatureNonce、signature | ||
115 | + httpPut.addHeader("appKey", appKey); | ||
116 | + httpPut.addHeader("ts", ts.toString()); | ||
117 | + httpPut.addHeader("once", once); | ||
118 | + System.out.println("once:" + once); | ||
119 | + httpPut.addHeader("signData", signData); | ||
120 | + System.out.println("headers:" + httpPut.getAllHeaders()); | ||
121 | + String urlStr = httpPut.getURI().toString(); | ||
122 | + // 公共参数URL | ||
123 | + System.out.println("commonParamter:" + urlStr); | ||
124 | + if (StringUtils.endsWith(urlStr, "/")) { | ||
125 | + urlStr = StringUtils.removeEnd(urlStr, "/"); | ||
126 | + } | ||
127 | + httpPut.setURI(new URI(urlStr)); | ||
128 | + RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(3000).setConnectionRequestTimeout(3000).setSocketTimeout(3000).build(); | ||
129 | + httpPut.setConfig(requestConfig); | ||
130 | + System.out.println("urlStr in request:" + httpPut.getURI().toString()); | ||
131 | + // 执行请求 | ||
132 | + CloseableHttpResponse response = httpclient.execute(httpPut); | ||
133 | + // 取响应的结果 | ||
134 | + int statusCode = response.getStatusLine().getStatusCode(); | ||
135 | + // 打印响应结果 | ||
136 | + if (statusCode == HttpStatus.SC_OK) { | ||
137 | + String resp = EntityUtils.toString(response.getEntity(), "utf-8"); | ||
138 | + System.out.println("status:" + statusCode); | ||
139 | + System.out.println("result:" + resp); | ||
140 | + } | ||
141 | + } catch (URISyntaxException e) { | ||
142 | + logger.error("签名失败:", e); | ||
143 | + } catch (ClientProtocolException e) { | ||
144 | + e.printStackTrace(); | ||
145 | + } catch (IOException e) { | ||
146 | + e.printStackTrace(); | ||
147 | + } | ||
148 | + } | ||
149 | +} |
1 | +package com.tianbo.warehouse.util.KIAM; | ||
2 | + | ||
3 | + | ||
4 | +import java.io.UnsupportedEncodingException; | ||
5 | +import java.net.URLDecoder; | ||
6 | +import java.security.MessageDigest; | ||
7 | +import java.security.NoSuchAlgorithmException; | ||
8 | +import java.util.Arrays; | ||
9 | +import org.apache.commons.lang3.ArrayUtils; | ||
10 | +import org.apache.commons.lang3.StringUtils; | ||
11 | +import org.slf4j.Logger; | ||
12 | +import org.slf4j.LoggerFactory; | ||
13 | +/** | ||
14 | + * @author shenhailong | ||
15 | + * <p> | ||
16 | + * 2020/12/14/16:44 | ||
17 | + */ | ||
18 | +public class TokenUtils { | ||
19 | + | ||
20 | + private static final Logger logger = LoggerFactory.getLogger(TokenUtils.class); | ||
21 | + private static String[] hexDigits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e","f" }; | ||
22 | + public static String getSignature(String appPwd, String paramUrl) { | ||
23 | + if (StringUtils.isNotBlank(paramUrl)) { | ||
24 | + try { | ||
25 | + paramUrl = URLDecoder.decode(paramUrl, "UTF-8"); | ||
26 | + } catch (UnsupportedEncodingException e) { | ||
27 | + logger.error("生成signData失败:", e); | ||
28 | + throw new RuntimeException("生成signData失败:", e); | ||
29 | + } | ||
30 | + } | ||
31 | + String[] paraArray = new String[] {}; | ||
32 | + if (StringUtils.isNotBlank(paramUrl)) { | ||
33 | + String[] queryArray = paramUrl.split("&"); | ||
34 | + paraArray = (String[]) ArrayUtils.addAll(queryArray, paraArray); | ||
35 | + } Arrays.sort(paraArray); | ||
36 | + StringBuffer buffer = new StringBuffer(); | ||
37 | + buffer.append(appPwd); | ||
38 | + buffer.append(":"); | ||
39 | + for (int i = 0; i < paraArray.length; i++) { | ||
40 | + buffer.append(paraArray[i]); | ||
41 | + buffer.append("&"); | ||
42 | + } | ||
43 | + buffer.deleteCharAt(buffer.length() - 1); | ||
44 | + buffer.append(":"); | ||
45 | + buffer.append(appPwd); | ||
46 | + MessageDigest md = null; | ||
47 | + try { | ||
48 | + md = MessageDigest.getInstance("SM3"); | ||
49 | + md.update(buffer.toString().getBytes("UTF-8")); | ||
50 | + } catch (NoSuchAlgorithmException e) { | ||
51 | + logger.error("生成signData失败:", e); | ||
52 | + throw new RuntimeException("生成signData失败.", e); | ||
53 | + } catch (UnsupportedEncodingException e) { | ||
54 | + logger.error("生成signData失败:", e); | ||
55 | + throw new RuntimeException("生成signData失败.", e); | ||
56 | + } String encode = byteArrayToHexString(md.digest()); | ||
57 | + return encode; | ||
58 | + } | ||
59 | + private static String byteArrayToHexString(byte[] byteArray) { | ||
60 | + StringBuffer sb = new StringBuffer(); | ||
61 | + for (byte byt : byteArray) { | ||
62 | + sb.append(byteToHexString(byt)); | ||
63 | + } return sb.toString(); | ||
64 | + } | ||
65 | + private static String byteToHexString(byte byt) { | ||
66 | + int n = byt; | ||
67 | + if (n < 0) { | ||
68 | + n = 256 + n; | ||
69 | + } | ||
70 | + return hexDigits[n / 16] + hexDigits[n % 16]; | ||
71 | + } | ||
72 | +} | ||
73 | + |
@@ -23,11 +23,12 @@ | @@ -23,11 +23,12 @@ | ||
23 | <result column="update_date" property="updateDate" jdbcType="TIMESTAMP" /> | 23 | <result column="update_date" property="updateDate" jdbcType="TIMESTAMP" /> |
24 | <result column="remarks" property="remarks" jdbcType="VARCHAR" /> | 24 | <result column="remarks" property="remarks" jdbcType="VARCHAR" /> |
25 | <result column="del_flag" property="delFlag" jdbcType="CHAR" /> | 25 | <result column="del_flag" property="delFlag" jdbcType="CHAR" /> |
26 | + <result column="user_sync_id" property="userSyncId" jdbcType="VARCHAR" /> | ||
26 | </resultMap> | 27 | </resultMap> |
27 | <sql id="Base_Column_List" > | 28 | <sql id="Base_Column_List" > |
28 | id, company_id, office_id, login_name, password, no, name, email, phone, mobile, | 29 | id, company_id, office_id, login_name, password, no, name, email, phone, mobile, |
29 | user_type, photo, login_ip, login_date, login_flag, create_by, create_date, update_by, | 30 | user_type, photo, login_ip, login_date, login_flag, create_by, create_date, update_by, |
30 | - update_date, remarks, del_flag | 31 | + update_date, remarks, del_flag, user_sync_id |
31 | </sql> | 32 | </sql> |
32 | <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" > | 33 | <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" > |
33 | select | 34 | select |
@@ -144,6 +145,9 @@ | @@ -144,6 +145,9 @@ | ||
144 | <if test="delFlag != null" > | 145 | <if test="delFlag != null" > |
145 | del_flag, | 146 | del_flag, |
146 | </if> | 147 | </if> |
148 | + <if test="userSyncId != null" > | ||
149 | + user_sync_id, | ||
150 | + </if> | ||
147 | </trim> | 151 | </trim> |
148 | <trim prefix="values (" suffix=")" suffixOverrides="," > | 152 | <trim prefix="values (" suffix=")" suffixOverrides="," > |
149 | <if test="id != null" > | 153 | <if test="id != null" > |
@@ -308,4 +312,146 @@ | @@ -308,4 +312,146 @@ | ||
308 | login_flag = #{loginFlag,jdbcType=VARCHAR} | 312 | login_flag = #{loginFlag,jdbcType=VARCHAR} |
309 | where login_name = #{loginName,jdbcType=VARCHAR} | 313 | where login_name = #{loginName,jdbcType=VARCHAR} |
310 | </update> | 314 | </update> |
315 | + | ||
316 | + <select id="findLonginName" parameterType="java.lang.String" resultMap="BaseResultMap"> | ||
317 | + select | ||
318 | + <include refid="Base_Column_List"/> | ||
319 | + from | ||
320 | + sys_user | ||
321 | + where | ||
322 | + 1=1 | ||
323 | + <if test="loginName != null and loginName != ''"> | ||
324 | + and login_name = #{loginName,jdbcType=VARCHAR} | ||
325 | + </if> | ||
326 | + <if test="userSyncId != null and userSyncId != ''"> | ||
327 | + and user_sync_id = #{userSyncId,jdbcType=VARCHAR} | ||
328 | + </if> | ||
329 | + </select> | ||
330 | + <update id="updateSyncUserLoginName" parameterType="com.tianbo.warehouse.model.KakoUser" > | ||
331 | + update sys_user | ||
332 | + <set > | ||
333 | + <if test="companyId != null" > | ||
334 | + company_id = #{companyId,jdbcType=VARCHAR}, | ||
335 | + </if> | ||
336 | + <if test="officeId != null" > | ||
337 | + office_id = #{officeId,jdbcType=VARCHAR}, | ||
338 | + </if> | ||
339 | + <if test="password != null" > | ||
340 | + password = #{password,jdbcType=VARCHAR}, | ||
341 | + </if> | ||
342 | + <if test="no != null" > | ||
343 | + no = #{no,jdbcType=VARCHAR}, | ||
344 | + </if> | ||
345 | + <if test="name != null" > | ||
346 | + name = #{name,jdbcType=VARCHAR}, | ||
347 | + </if> | ||
348 | + <if test="email != null" > | ||
349 | + email = #{email,jdbcType=VARCHAR}, | ||
350 | + </if> | ||
351 | + <if test="phone != null" > | ||
352 | + phone = #{phone,jdbcType=VARCHAR}, | ||
353 | + </if> | ||
354 | + <if test="mobile != null" > | ||
355 | + mobile = #{mobile,jdbcType=VARCHAR}, | ||
356 | + </if> | ||
357 | + <if test="userType != null" > | ||
358 | + user_type = #{userType,jdbcType=CHAR}, | ||
359 | + </if> | ||
360 | + <if test="photo != null" > | ||
361 | + photo = #{photo,jdbcType=VARCHAR}, | ||
362 | + </if> | ||
363 | + <if test="loginIp != null" > | ||
364 | + login_ip = #{loginIp,jdbcType=VARCHAR}, | ||
365 | + </if> | ||
366 | + <if test="loginDate != null" > | ||
367 | + login_date = #{loginDate,jdbcType=TIMESTAMP}, | ||
368 | + </if> | ||
369 | + <if test="loginFlag != null" > | ||
370 | + login_flag = #{loginFlag,jdbcType=VARCHAR}, | ||
371 | + </if> | ||
372 | + <if test="createBy != null" > | ||
373 | + create_by = #{createBy,jdbcType=VARCHAR}, | ||
374 | + </if> | ||
375 | + <if test="createDate != null" > | ||
376 | + create_date = #{createDate,jdbcType=TIMESTAMP}, | ||
377 | + </if> | ||
378 | + <if test="updateBy != null" > | ||
379 | + update_by = #{updateBy,jdbcType=VARCHAR}, | ||
380 | + </if> | ||
381 | + <if test="updateDate != null" > | ||
382 | + update_date = #{updateDate,jdbcType=TIMESTAMP}, | ||
383 | + </if> | ||
384 | + <if test="remarks != null" > | ||
385 | + remarks = #{remarks,jdbcType=VARCHAR}, | ||
386 | + </if> | ||
387 | + <if test="delFlag != null" > | ||
388 | + del_flag = #{delFlag,jdbcType=CHAR}, | ||
389 | + </if> | ||
390 | + <if test="userSyncId != null" > | ||
391 | + user_sync_id = #{userSyncId,jdbcType=VARCHAR} | ||
392 | + </if> | ||
393 | + </set> | ||
394 | + where login_name = #{loginName,jdbcType=VARCHAR} | ||
395 | + </update> | ||
396 | + | ||
397 | + <update id="updateSyncUserId" parameterType="com.tianbo.warehouse.model.KakoUser" > | ||
398 | + update sys_user | ||
399 | + <set > | ||
400 | + <if test="companyId != null" > | ||
401 | + company_id = #{companyId,jdbcType=VARCHAR}, | ||
402 | + </if> | ||
403 | + <if test="officeId != null" > | ||
404 | + office_id = #{officeId,jdbcType=VARCHAR}, | ||
405 | + </if> | ||
406 | + <if test="password != null" > | ||
407 | + password = #{password,jdbcType=VARCHAR}, | ||
408 | + </if> | ||
409 | + <if test="no != null" > | ||
410 | + no = #{no,jdbcType=VARCHAR}, | ||
411 | + </if> | ||
412 | + <if test="name != null" > | ||
413 | + name = #{name,jdbcType=VARCHAR}, | ||
414 | + </if> | ||
415 | + <if test="email != null" > | ||
416 | + email = #{email,jdbcType=VARCHAR}, | ||
417 | + </if> | ||
418 | + <if test="phone != null" > | ||
419 | + phone = #{phone,jdbcType=VARCHAR}, | ||
420 | + </if> | ||
421 | + <if test="mobile != null" > | ||
422 | + mobile = #{mobile,jdbcType=VARCHAR}, | ||
423 | + </if> | ||
424 | + <if test="userType != null" > | ||
425 | + user_type = #{userType,jdbcType=CHAR}, | ||
426 | + </if> | ||
427 | + <if test="photo != null" > | ||
428 | + photo = #{photo,jdbcType=VARCHAR}, | ||
429 | + </if> | ||
430 | + <if test="loginIp != null" > | ||
431 | + login_ip = #{loginIp,jdbcType=VARCHAR}, | ||
432 | + </if> | ||
433 | + <if test="loginDate != null" > | ||
434 | + login_date = #{loginDate,jdbcType=TIMESTAMP}, | ||
435 | + </if> | ||
436 | + <if test="loginFlag != null" > | ||
437 | + login_flag = #{loginFlag,jdbcType=VARCHAR}, | ||
438 | + </if> | ||
439 | + <if test="createBy != null" > | ||
440 | + create_by = #{createBy,jdbcType=VARCHAR}, | ||
441 | + </if> | ||
442 | + <if test="createDate != null" > | ||
443 | + create_date = #{createDate,jdbcType=TIMESTAMP}, | ||
444 | + </if> | ||
445 | + <if test="updateBy != null" > | ||
446 | + update_by = #{updateBy,jdbcType=VARCHAR}, | ||
447 | + </if> | ||
448 | + <if test="updateDate != null" > | ||
449 | + update_date = #{updateDate,jdbcType=TIMESTAMP}, | ||
450 | + </if> | ||
451 | + <if test="delFlag != null" > | ||
452 | + del_flag = #{delFlag,jdbcType=CHAR} | ||
453 | + </if> | ||
454 | + </set> | ||
455 | + where user_sync_id = #{userSyncId,jdbcType=VARCHAR} | ||
456 | + </update> | ||
311 | </mapper> | 457 | </mapper> |
-
请 注册 或 登录 后发表评论