作者 朱兆平

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

... ... @@ -8,14 +8,14 @@ import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.thoughtworks.xstream.core.util.Base64Encoder;
import com.tianbo.warehouse.controller.response.ResultJson;
import com.tianbo.warehouse.model.ROLE;
import com.tianbo.warehouse.model.Token;
import com.tianbo.warehouse.model.USERS;
import com.tianbo.warehouse.dao.UserRoleMapper;
import com.tianbo.warehouse.model.*;
import com.tianbo.warehouse.security.filter.JwtTokenUtil;
import com.tianbo.warehouse.service.PermissionService;
import com.tianbo.warehouse.service.RoleService;
import com.tianbo.warehouse.service.UserService;
import com.tianbo.warehouse.util.MapToJsonUtil;
import com.tianbo.warehouse.util.RedisUtils;
import io.swagger.annotations.ApiOperation;
... ... @@ -24,12 +24,10 @@ import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
... ... @@ -66,6 +64,9 @@ public class AnonymousController {
@Autowired
UserService userService;
@Resource
UserRoleMapper userRoleMapper;
/**
* SSO验证服务票据响应属性名
*/
... ... @@ -185,7 +186,8 @@ public class AnonymousController {
user.setUserStatus(loginUser.getUserStatus());
user.setState(loginUser.getState());
}else {
user.setPassword("sso");
assert loginUser != null;
user.setPassword(loginUser.getPassword());
user.setUserStatus(2);
user.setState(true);
userService.insertSelective(user);
... ... @@ -235,12 +237,73 @@ public class AnonymousController {
String loginName = attributes.getString(LOGIN_NAME);
String userId = attributes.getString(USER_ID);
String realName = attributes.getString("USER_NAME");
String password = attributes.getString("PWD");
log.info("[SSO-AUTH-TICKET-INFO]-用户:{}/{}",loginName,realName);
user.setUsername(loginName);
user.setPassword(password);
// user.setUserId(userId);
}
}
return user;
}
/**
* 资源同步
* @param map
* @return
* @throws Exception
*/
@PostMapping(value = "/userSynchronization")
@ResponseBody
public ResultMessage userSynchronization(@RequestBody Map<String, Object> map){
//获取action的值,判断是push数据还是删除数据
String action = map.get("action").toString();
if ("user".equals(map.get("resType").toString())){
MapToJsonUtil jsonUtil = new MapToJsonUtil();
List<SSOUserData> list = jsonUtil.mapToList(map, SSOUserData.class, "data");
// 判断该用户是否存在
int i = 0;
for (SSOUserData userData : list){
log.info("用户信息:{}", userData);
USERS loginUser = userService.loadByUsername(userData.getLOGIN_NAME());
USERS users = new USERS();
users.setUsername(userData.getLOGIN_NAME());
users.setPassword(userData.getPWD());
//todo:统一认证的用户ID与我们的数据类型不匹配 暂时不同步
users.setMobilephone(userData.getMOBILE());
users.setEmail(userData.getEMAIL());
users.setRealname(userData.getUSER_NAME());
users.setCompanyId(73);
users.setUpdatetime(new Date());
if (StringUtils.isEmpty(users.getUsername())){
return new ResultMessage("402","缺少lognin_name字段信息");
}
if ("push".equals(action)) {
if (loginUser != null){
log.info("通过账号为条件更新");
i = userService.updateByUsernameSelective(users);
}else {
if ( StringUtils.isEmpty(users.getPassword())){
return new ResultMessage("402","缺少pwd字段信息");
}
users.setCreattime(new Date());
log.info("账号统一认证用户信息不存在可以 [新增]");
//接口返回userid,roleid73 等于转关运抵管理员
int userId = userService.insertSelective(users);
UserRole userRole = new UserRole(userId,73);
userRoleMapper.insertSelective(userRole);
//todo: 默认权限分配
i= userId;
}
}else if ("recycle".equals(action)){
userService.deleteByUsername(users.getUsername());
}
}
return i > 0 ? new ResultMessage("200","资源同步成功"):new ResultMessage("202", "资源同步失败");
}
return new ResultMessage("201","不是用户信息");
}
}
... ...
... ... @@ -8,6 +8,8 @@ import java.util.List;
public interface USERSMapper {
int deleteByPrimaryKey(Integer userId);
int deleteByUsername(String username);
int insert(USERS record);
int insertSelective(USERS record);
... ... @@ -16,6 +18,8 @@ public interface USERSMapper {
int updateByPrimaryKeySelective(USERS record);
int updateByUsernameSelective(USERS record);
int updateByPrimaryKey(USERS record);
List<USERS> selectByUsername(String userName);
... ...
package com.tianbo.warehouse.model;
import lombok.Data;
import java.io.Serializable;
/**
* 针对统一认证访问的接口返回实体
*/
@Data
public class ResultMessage implements Serializable {
private static final long serialVersionUID = 1L;
private String resultCode;
private String resultMessage;
public ResultMessage(String resultCode, String resultMessage) {
this.resultCode = resultCode;
this.resultMessage = resultMessage;
}
}
... ...
package com.tianbo.warehouse.model;
import lombok.Data;
/**
* 统一认证资源同步实体
*/
@Data
public class SSOUserData {
private String USER_ID;
private String USER_NAME;
private String USER_NO;
private String MOBILE;
private String IDCARD_NO;
private String COUNTRY;
private String SEX;
private String NATIONALITY;
private String OFFICE_PHONE;
private String OFFICE_ADDRESS;
private String EMAIL;
private String JOB_LEVEL;
private String JOB_TITLE;
private String JOB_TYPE;
private String JOB_STATUS;
private String JOB_POSITION;
private String SECRET_LEVEL;
private String USER_POST;
private String USER_JOB_ID;
private String ORG_ID;
private String USER_TITLE;
private String LOGIN_NAME;
private String SHOW_ORDER;
private String REMARK;
private String NAIL_ID;
private String PK_PSNDOC;
private String PWD;
private String PWD_ENCRYPT;
private String UPDATE_TIME;
}
... ...
... ... @@ -14,10 +14,14 @@ public interface UserService {
int updateByPrimaryKeySelective(USERS record);
int updateByUsernameSelective(USERS record);
int insertSelective(USERS record);
int deleteByPrimaryKey(Integer userId);
int deleteByUsername(String username);
int setUserRole(UserRole userRole);
ROLE getUserCompany(Integer company_id);
... ...
... ... @@ -134,6 +134,16 @@ public class UserServiceImpl implements UserService{
}
@Override
public int updateByUsernameSelective(USERS record){
int i = 0;
if (record!=null){
i = usersMapper.updateByUsernameSelective(record);
}
return i;
}
@Override
public int insertSelective(USERS record){
if (null!=record){
return usersMapper.insertSelective(record);
... ... @@ -151,6 +161,15 @@ public class UserServiceImpl implements UserService{
}
}
@Override
public int deleteByUsername(String username){
if (StringUtils.isNotBlank(username)){
return usersMapper.deleteByUsername(username);
}else {
return 0;
}
}
/**重置用户的权限,每次设置用户权限关系前,先删除用户所有权限,再重新分配用户权限
*这里需要开启事务,删除 或者插入不生效就回滚
* @param userRole
... ...
package com.tianbo.warehouse.util;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.List;
import java.util.Map;
/**
* @author shenhailong
* <p>
* 2020/12/16/16:12
*/
public class MapToJsonUtil<T> {
public static JSONObject mapToJson(Map<String, Object> map) {
String data = JSON.toJSONString(map);
return JSON.parseObject(data);
}
/**
* map中取key对应的value
* @param map
* @param key
* @return
*/
public String mapToString(Map<String, Object> map, String key) {
JSONObject jsonObject = mapToJson(map);
return jsonObject.getString(key);
}
/**
* map中取类对象
* @param map
* @param clazz
* @param key
* @param <T>
* @return
*/
public static <T> T mapToObject(Map<String, Object> map, Class<T> clazz, String key) {
T t = null;
JSONObject jsonObject = mapToJson(map);
JSONObject object = jsonObject.getJSONObject(key);
t = object.toJavaObject(clazz);
return t;
}
/**
* map中取list
* @param map
* @param clazz
* @param key
* @return
*/
public List<T> mapToList(Map<String, Object> map, Class<T> clazz, String key) {
List<T> t = null;
JSONObject jsonObject = mapToJson(map);
JSONArray array = jsonObject.getJSONArray(key);
t = array.toJavaList(clazz);
return t;
}
}
... ...
... ... @@ -194,6 +194,10 @@
delete from users
where user_id = #{userId,jdbcType=INTEGER}
</delete>
<delete id="deleteByUsername" parameterType="java.lang.String" >
delete from users
where username = #{username,jdbcType=VARCHAR}
</delete>
<insert id="insert" parameterType="com.tianbo.warehouse.model.USERS" >
insert into users (user_id, username, password,
birthday, sex, address,
... ... @@ -206,7 +210,7 @@
#{updatetime,jdbcType=TIMESTAMP}, #{userface,jdbcType=VARCHAR}, #{realname,jdbcType=VARCHAR},
#{email,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER},#{createBy,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" parameterType="com.tianbo.warehouse.model.USERS" >
<insert id="insertSelective" parameterType="com.tianbo.warehouse.model.USERS" useGeneratedKeys="true" keyProperty="userId">
insert into users
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="userId != null" >
... ... @@ -360,6 +364,57 @@
</set>
where user_id = #{userId,jdbcType=INTEGER}
</update>
<update id="updateByUsernameSelective" parameterType="com.tianbo.warehouse.model.USERS" >
update users
<set >
<if test="password != null" >
password = #{password,jdbcType=VARCHAR},
</if>
<if test="birthday != null" >
birthday = #{birthday,jdbcType=TIMESTAMP},
</if>
<if test="sex != null" >
sex = #{sex,jdbcType=CHAR},
</if>
<if test="address != null" >
address = #{address,jdbcType=VARCHAR},
</if>
<if test="state != null" >
state = #{state,jdbcType=BIT},
</if>
<if test="mobilephone != null" >
mobilePhone = #{mobilephone,jdbcType=VARCHAR},
</if>
<if test="creattime != null" >
creatTime = #{creattime,jdbcType=TIMESTAMP},
</if>
<if test="updatetime != null" >
updateTime = #{updatetime,jdbcType=TIMESTAMP},
</if>
<if test="userface != null" >
userFace = #{userface,jdbcType=VARCHAR},
</if>
<if test="realname != null" >
realname = #{realname,jdbcType=VARCHAR},
</if>
<if test="email != null" >
email = #{email,jdbcType=VARCHAR},
</if>
<if test="age != null" >
age = #{age,jdbcType=INTEGER},
</if>
<if test="companyId != null" >
company_id = #{companyId,jdbcType=INTEGER},
</if>
<if test="userStatus != null" >
user_status = #{userStatus,jdbcType=INTEGER},
</if>
<if test="loginDate != null" >
login_date = #{loginDate,jdbcType=TIMESTAMP},
</if>
</set>
where username = #{username,jdbcType=VARCHAR}
</update>
<update id="updateByPrimaryKey" parameterType="com.tianbo.warehouse.model.USERS" >
update users
set password = #{password,jdbcType=VARCHAR},
... ...