作者 朱兆平

add: 新增对接统一认证资源同步接口

@@ -8,14 +8,14 @@ import com.google.code.kaptcha.impl.DefaultKaptcha; @@ -8,14 +8,14 @@ import com.google.code.kaptcha.impl.DefaultKaptcha;
8 8
9 import com.thoughtworks.xstream.core.util.Base64Encoder; 9 import com.thoughtworks.xstream.core.util.Base64Encoder;
10 import com.tianbo.warehouse.controller.response.ResultJson; 10 import com.tianbo.warehouse.controller.response.ResultJson;
11 -import com.tianbo.warehouse.model.ROLE;  
12 -import com.tianbo.warehouse.model.Token;  
13 -import com.tianbo.warehouse.model.USERS; 11 +import com.tianbo.warehouse.dao.UserRoleMapper;
  12 +import com.tianbo.warehouse.model.*;
14 import com.tianbo.warehouse.security.filter.JwtTokenUtil; 13 import com.tianbo.warehouse.security.filter.JwtTokenUtil;
15 import com.tianbo.warehouse.service.PermissionService; 14 import com.tianbo.warehouse.service.PermissionService;
16 import com.tianbo.warehouse.service.RoleService; 15 import com.tianbo.warehouse.service.RoleService;
17 16
18 import com.tianbo.warehouse.service.UserService; 17 import com.tianbo.warehouse.service.UserService;
  18 +import com.tianbo.warehouse.util.MapToJsonUtil;
19 import com.tianbo.warehouse.util.RedisUtils; 19 import com.tianbo.warehouse.util.RedisUtils;
20 20
21 import io.swagger.annotations.ApiOperation; 21 import io.swagger.annotations.ApiOperation;
@@ -24,12 +24,10 @@ import org.apache.commons.lang.StringUtils; @@ -24,12 +24,10 @@ import org.apache.commons.lang.StringUtils;
24 import org.springframework.beans.factory.annotation.Autowired; 24 import org.springframework.beans.factory.annotation.Autowired;
25 import org.springframework.beans.factory.annotation.Value; 25 import org.springframework.beans.factory.annotation.Value;
26 import org.springframework.http.ResponseEntity; 26 import org.springframework.http.ResponseEntity;
27 -import org.springframework.web.bind.annotation.PostMapping;  
28 -import org.springframework.web.bind.annotation.RequestMapping;  
29 -import org.springframework.web.bind.annotation.RequestParam;  
30 -import org.springframework.web.bind.annotation.RestController; 27 +import org.springframework.web.bind.annotation.*;
31 import org.springframework.web.client.RestTemplate; 28 import org.springframework.web.client.RestTemplate;
32 29
  30 +import javax.annotation.Resource;
33 import javax.imageio.ImageIO; 31 import javax.imageio.ImageIO;
34 32
35 import java.awt.image.BufferedImage; 33 import java.awt.image.BufferedImage;
@@ -66,6 +64,9 @@ public class AnonymousController { @@ -66,6 +64,9 @@ public class AnonymousController {
66 @Autowired 64 @Autowired
67 UserService userService; 65 UserService userService;
68 66
  67 + @Resource
  68 + UserRoleMapper userRoleMapper;
  69 +
69 /** 70 /**
70 * SSO验证服务票据响应属性名 71 * SSO验证服务票据响应属性名
71 */ 72 */
@@ -185,7 +186,8 @@ public class AnonymousController { @@ -185,7 +186,8 @@ public class AnonymousController {
185 user.setUserStatus(loginUser.getUserStatus()); 186 user.setUserStatus(loginUser.getUserStatus());
186 user.setState(loginUser.getState()); 187 user.setState(loginUser.getState());
187 }else { 188 }else {
188 - user.setPassword("sso"); 189 + assert loginUser != null;
  190 + user.setPassword(loginUser.getPassword());
189 user.setUserStatus(2); 191 user.setUserStatus(2);
190 user.setState(true); 192 user.setState(true);
191 userService.insertSelective(user); 193 userService.insertSelective(user);
@@ -235,12 +237,73 @@ public class AnonymousController { @@ -235,12 +237,73 @@ public class AnonymousController {
235 String loginName = attributes.getString(LOGIN_NAME); 237 String loginName = attributes.getString(LOGIN_NAME);
236 String userId = attributes.getString(USER_ID); 238 String userId = attributes.getString(USER_ID);
237 String realName = attributes.getString("USER_NAME"); 239 String realName = attributes.getString("USER_NAME");
  240 + String password = attributes.getString("PWD");
238 log.info("[SSO-AUTH-TICKET-INFO]-用户:{}/{}",loginName,realName); 241 log.info("[SSO-AUTH-TICKET-INFO]-用户:{}/{}",loginName,realName);
239 242
240 user.setUsername(loginName); 243 user.setUsername(loginName);
  244 + user.setPassword(password);
241 // user.setUserId(userId); 245 // user.setUserId(userId);
242 } 246 }
243 } 247 }
244 return user; 248 return user;
245 } 249 }
  250 +
  251 + /**
  252 + * 资源同步
  253 + * @param map
  254 + * @return
  255 + * @throws Exception
  256 + */
  257 + @PostMapping(value = "/userSynchronization")
  258 + @ResponseBody
  259 + public ResultMessage userSynchronization(@RequestBody Map<String, Object> map){
  260 + //获取action的值,判断是push数据还是删除数据
  261 + String action = map.get("action").toString();
  262 + if ("user".equals(map.get("resType").toString())){
  263 + MapToJsonUtil jsonUtil = new MapToJsonUtil();
  264 + List<SSOUserData> list = jsonUtil.mapToList(map, SSOUserData.class, "data");
  265 + // 判断该用户是否存在
  266 + int i = 0;
  267 + for (SSOUserData userData : list){
  268 + log.info("用户信息:{}", userData);
  269 + USERS loginUser = userService.loadByUsername(userData.getLOGIN_NAME());
  270 + USERS users = new USERS();
  271 + users.setUsername(userData.getLOGIN_NAME());
  272 + users.setPassword(userData.getPWD());
  273 + //todo:统一认证的用户ID与我们的数据类型不匹配 暂时不同步
  274 + users.setMobilephone(userData.getMOBILE());
  275 + users.setEmail(userData.getEMAIL());
  276 + users.setRealname(userData.getUSER_NAME());
  277 + users.setCompanyId(73);
  278 + users.setUpdatetime(new Date());
  279 + if (StringUtils.isEmpty(users.getUsername())){
  280 + return new ResultMessage("402","缺少lognin_name字段信息");
  281 + }
  282 + if ("push".equals(action)) {
  283 + if (loginUser != null){
  284 + log.info("通过账号为条件更新");
  285 + i = userService.updateByUsernameSelective(users);
  286 + }else {
  287 + if ( StringUtils.isEmpty(users.getPassword())){
  288 + return new ResultMessage("402","缺少pwd字段信息");
  289 + }
  290 + users.setCreattime(new Date());
  291 + log.info("账号统一认证用户信息不存在可以 [新增]");
  292 + //接口返回userid,roleid73 等于转关运抵管理员
  293 + int userId = userService.insertSelective(users);
  294 + UserRole userRole = new UserRole(userId,73);
  295 + userRoleMapper.insertSelective(userRole);
  296 + //todo: 默认权限分配
  297 + i= userId;
  298 + }
  299 + }else if ("recycle".equals(action)){
  300 + userService.deleteByUsername(users.getUsername());
  301 + }
  302 +
  303 + }
  304 +
  305 + return i > 0 ? new ResultMessage("200","资源同步成功"):new ResultMessage("202", "资源同步失败");
  306 + }
  307 + return new ResultMessage("201","不是用户信息");
  308 + }
246 } 309 }
@@ -8,6 +8,8 @@ import java.util.List; @@ -8,6 +8,8 @@ import java.util.List;
8 public interface USERSMapper { 8 public interface USERSMapper {
9 int deleteByPrimaryKey(Integer userId); 9 int deleteByPrimaryKey(Integer userId);
10 10
  11 + int deleteByUsername(String username);
  12 +
11 int insert(USERS record); 13 int insert(USERS record);
12 14
13 int insertSelective(USERS record); 15 int insertSelective(USERS record);
@@ -16,6 +18,8 @@ public interface USERSMapper { @@ -16,6 +18,8 @@ public interface USERSMapper {
16 18
17 int updateByPrimaryKeySelective(USERS record); 19 int updateByPrimaryKeySelective(USERS record);
18 20
  21 + int updateByUsernameSelective(USERS record);
  22 +
19 int updateByPrimaryKey(USERS record); 23 int updateByPrimaryKey(USERS record);
20 24
21 List<USERS> selectByUsername(String userName); 25 List<USERS> selectByUsername(String userName);
  1 +package com.tianbo.warehouse.model;
  2 +
  3 +import lombok.Data;
  4 +
  5 +import java.io.Serializable;
  6 +
  7 +/**
  8 + * 针对统一认证访问的接口返回实体
  9 + */
  10 +@Data
  11 +public class ResultMessage implements Serializable {
  12 +
  13 + private static final long serialVersionUID = 1L;
  14 +
  15 + private String resultCode;
  16 +
  17 + private String resultMessage;
  18 +
  19 + public ResultMessage(String resultCode, String resultMessage) {
  20 + this.resultCode = resultCode;
  21 + this.resultMessage = resultMessage;
  22 + }
  23 +
  24 +}
  1 +package com.tianbo.warehouse.model;
  2 +
  3 +import lombok.Data;
  4 +
  5 +/**
  6 + * 统一认证资源同步实体
  7 + */
  8 +@Data
  9 +public class SSOUserData {
  10 +
  11 + private String USER_ID;
  12 +
  13 + private String USER_NAME;
  14 +
  15 + private String USER_NO;
  16 +
  17 + private String MOBILE;
  18 +
  19 + private String IDCARD_NO;
  20 +
  21 + private String COUNTRY;
  22 +
  23 + private String SEX;
  24 +
  25 + private String NATIONALITY;
  26 +
  27 + private String OFFICE_PHONE;
  28 +
  29 + private String OFFICE_ADDRESS;
  30 +
  31 + private String EMAIL;
  32 +
  33 + private String JOB_LEVEL;
  34 +
  35 + private String JOB_TITLE;
  36 +
  37 + private String JOB_TYPE;
  38 +
  39 + private String JOB_STATUS;
  40 +
  41 + private String JOB_POSITION;
  42 +
  43 + private String SECRET_LEVEL;
  44 +
  45 + private String USER_POST;
  46 +
  47 + private String USER_JOB_ID;
  48 +
  49 + private String ORG_ID;
  50 +
  51 + private String USER_TITLE;
  52 +
  53 + private String LOGIN_NAME;
  54 +
  55 + private String SHOW_ORDER;
  56 +
  57 + private String REMARK;
  58 +
  59 + private String NAIL_ID;
  60 +
  61 + private String PK_PSNDOC;
  62 +
  63 + private String PWD;
  64 +
  65 + private String PWD_ENCRYPT;
  66 +
  67 + private String UPDATE_TIME;
  68 +}
@@ -14,10 +14,14 @@ public interface UserService { @@ -14,10 +14,14 @@ public interface UserService {
14 14
15 int updateByPrimaryKeySelective(USERS record); 15 int updateByPrimaryKeySelective(USERS record);
16 16
  17 + int updateByUsernameSelective(USERS record);
  18 +
17 int insertSelective(USERS record); 19 int insertSelective(USERS record);
18 20
19 int deleteByPrimaryKey(Integer userId); 21 int deleteByPrimaryKey(Integer userId);
20 22
  23 + int deleteByUsername(String username);
  24 +
21 int setUserRole(UserRole userRole); 25 int setUserRole(UserRole userRole);
22 26
23 ROLE getUserCompany(Integer company_id); 27 ROLE getUserCompany(Integer company_id);
@@ -134,6 +134,16 @@ public class UserServiceImpl implements UserService{ @@ -134,6 +134,16 @@ public class UserServiceImpl implements UserService{
134 } 134 }
135 135
136 @Override 136 @Override
  137 + public int updateByUsernameSelective(USERS record){
  138 + int i = 0;
  139 + if (record!=null){
  140 + i = usersMapper.updateByUsernameSelective(record);
  141 + }
  142 + return i;
  143 +
  144 + }
  145 +
  146 + @Override
137 public int insertSelective(USERS record){ 147 public int insertSelective(USERS record){
138 if (null!=record){ 148 if (null!=record){
139 return usersMapper.insertSelective(record); 149 return usersMapper.insertSelective(record);
@@ -151,6 +161,15 @@ public class UserServiceImpl implements UserService{ @@ -151,6 +161,15 @@ public class UserServiceImpl implements UserService{
151 } 161 }
152 } 162 }
153 163
  164 + @Override
  165 + public int deleteByUsername(String username){
  166 + if (StringUtils.isNotBlank(username)){
  167 + return usersMapper.deleteByUsername(username);
  168 + }else {
  169 + return 0;
  170 + }
  171 + }
  172 +
154 /**重置用户的权限,每次设置用户权限关系前,先删除用户所有权限,再重新分配用户权限 173 /**重置用户的权限,每次设置用户权限关系前,先删除用户所有权限,再重新分配用户权限
155 *这里需要开启事务,删除 或者插入不生效就回滚 174 *这里需要开启事务,删除 或者插入不生效就回滚
156 * @param userRole 175 * @param userRole
  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 MapToJsonUtil<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 +}
@@ -194,6 +194,10 @@ @@ -194,6 +194,10 @@
194 delete from users 194 delete from users
195 where user_id = #{userId,jdbcType=INTEGER} 195 where user_id = #{userId,jdbcType=INTEGER}
196 </delete> 196 </delete>
  197 + <delete id="deleteByUsername" parameterType="java.lang.String" >
  198 + delete from users
  199 + where username = #{username,jdbcType=VARCHAR}
  200 + </delete>
197 <insert id="insert" parameterType="com.tianbo.warehouse.model.USERS" > 201 <insert id="insert" parameterType="com.tianbo.warehouse.model.USERS" >
198 insert into users (user_id, username, password, 202 insert into users (user_id, username, password,
199 birthday, sex, address, 203 birthday, sex, address,
@@ -206,7 +210,7 @@ @@ -206,7 +210,7 @@
206 #{updatetime,jdbcType=TIMESTAMP}, #{userface,jdbcType=VARCHAR}, #{realname,jdbcType=VARCHAR}, 210 #{updatetime,jdbcType=TIMESTAMP}, #{userface,jdbcType=VARCHAR}, #{realname,jdbcType=VARCHAR},
207 #{email,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER},#{createBy,jdbcType=VARCHAR}) 211 #{email,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER},#{createBy,jdbcType=VARCHAR})
208 </insert> 212 </insert>
209 - <insert id="insertSelective" parameterType="com.tianbo.warehouse.model.USERS" > 213 + <insert id="insertSelective" parameterType="com.tianbo.warehouse.model.USERS" useGeneratedKeys="true" keyProperty="userId">
210 insert into users 214 insert into users
211 <trim prefix="(" suffix=")" suffixOverrides="," > 215 <trim prefix="(" suffix=")" suffixOverrides="," >
212 <if test="userId != null" > 216 <if test="userId != null" >
@@ -360,6 +364,57 @@ @@ -360,6 +364,57 @@
360 </set> 364 </set>
361 where user_id = #{userId,jdbcType=INTEGER} 365 where user_id = #{userId,jdbcType=INTEGER}
362 </update> 366 </update>
  367 + <update id="updateByUsernameSelective" parameterType="com.tianbo.warehouse.model.USERS" >
  368 + update users
  369 + <set >
  370 + <if test="password != null" >
  371 + password = #{password,jdbcType=VARCHAR},
  372 + </if>
  373 + <if test="birthday != null" >
  374 + birthday = #{birthday,jdbcType=TIMESTAMP},
  375 + </if>
  376 + <if test="sex != null" >
  377 + sex = #{sex,jdbcType=CHAR},
  378 + </if>
  379 + <if test="address != null" >
  380 + address = #{address,jdbcType=VARCHAR},
  381 + </if>
  382 + <if test="state != null" >
  383 + state = #{state,jdbcType=BIT},
  384 + </if>
  385 + <if test="mobilephone != null" >
  386 + mobilePhone = #{mobilephone,jdbcType=VARCHAR},
  387 + </if>
  388 + <if test="creattime != null" >
  389 + creatTime = #{creattime,jdbcType=TIMESTAMP},
  390 + </if>
  391 + <if test="updatetime != null" >
  392 + updateTime = #{updatetime,jdbcType=TIMESTAMP},
  393 + </if>
  394 + <if test="userface != null" >
  395 + userFace = #{userface,jdbcType=VARCHAR},
  396 + </if>
  397 + <if test="realname != null" >
  398 + realname = #{realname,jdbcType=VARCHAR},
  399 + </if>
  400 + <if test="email != null" >
  401 + email = #{email,jdbcType=VARCHAR},
  402 + </if>
  403 + <if test="age != null" >
  404 + age = #{age,jdbcType=INTEGER},
  405 + </if>
  406 + <if test="companyId != null" >
  407 + company_id = #{companyId,jdbcType=INTEGER},
  408 + </if>
  409 + <if test="userStatus != null" >
  410 + user_status = #{userStatus,jdbcType=INTEGER},
  411 + </if>
  412 + <if test="loginDate != null" >
  413 + login_date = #{loginDate,jdbcType=TIMESTAMP},
  414 + </if>
  415 + </set>
  416 + where username = #{username,jdbcType=VARCHAR}
  417 + </update>
363 <update id="updateByPrimaryKey" parameterType="com.tianbo.warehouse.model.USERS" > 418 <update id="updateByPrimaryKey" parameterType="com.tianbo.warehouse.model.USERS" >
364 update users 419 update users
365 set password = #{password,jdbcType=VARCHAR}, 420 set password = #{password,jdbcType=VARCHAR},