...
|
...
|
@@ -4,38 +4,45 @@ import com.alibaba.fastjson.JSON; |
|
|
|
|
|
import com.google.code.kaptcha.impl.DefaultKaptcha;
|
|
|
|
|
|
import com.google.gson.Gson;
|
|
|
import com.thoughtworks.xstream.core.util.Base64Encoder;
|
|
|
import com.tianbo.warehouse.bean.AuthSuccessResponse;
|
|
|
import com.tianbo.warehouse.controller.response.ResultJson;
|
|
|
import com.tianbo.warehouse.controller.response.ResultMessage;
|
|
|
import com.tianbo.warehouse.dao.KakoUserMapper;
|
|
|
import com.tianbo.warehouse.model.KakoUser;
|
|
|
import com.tianbo.warehouse.model.ROLE;
|
|
|
import com.tianbo.warehouse.model.resource.UserDate;
|
|
|
import com.tianbo.warehouse.security.filter.JwtTokenUtil;
|
|
|
import com.tianbo.warehouse.service.PermissionService;
|
|
|
import com.tianbo.warehouse.service.RoleService;
|
|
|
|
|
|
import com.tianbo.warehouse.service.kakoImp.KakoUserService;
|
|
|
import com.tianbo.warehouse.util.JsonUtil;
|
|
|
import com.tianbo.warehouse.util.KIAM.HttpClientUtil;
|
|
|
import com.tianbo.warehouse.util.RedisUtils;
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
import javax.imageio.ImageIO;
|
|
|
|
|
|
import javax.servlet.http.Cookie;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import javax.servlet.http.HttpSession;
|
|
|
import java.awt.image.BufferedImage;
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
import java.io.IOException;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.UUID;
|
|
|
import java.util.*;
|
|
|
|
|
|
@Slf4j
|
|
|
@RestController()
|
|
|
@RequestMapping("/anonymous")
|
|
|
public class AnonymousController {
|
|
|
|
|
|
@Value("${jwt.max-alive}")
|
|
|
protected Integer jwtMaxAlive;
|
|
|
|
|
|
@Autowired
|
|
|
RoleService roleService;
|
|
|
|
...
|
...
|
@@ -43,8 +50,32 @@ public class AnonymousController { |
|
|
RedisUtils redisUtils;
|
|
|
|
|
|
@Autowired
|
|
|
KakoUserMapper kakoUserMapperl;
|
|
|
|
|
|
@Autowired
|
|
|
private DefaultKaptcha captchaProducer;
|
|
|
|
|
|
@Autowired
|
|
|
private PermissionService permissionService;
|
|
|
|
|
|
@Autowired
|
|
|
private KakoUserMapper kakoUserMapper;
|
|
|
|
|
|
@Autowired
|
|
|
private KakoUserService kakoUserService;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 验证服务票据响应属性名
|
|
|
*/
|
|
|
private static final String SERVICE_RESPONESE = "serviceResponse";
|
|
|
private static final String AUTHENTICATION_SUCCESS = "authenticationSuccess";
|
|
|
private static final String LOGIN_NAME = "LOGIN_NAME";
|
|
|
private static final String ATTRIBUTES = "attributes";
|
|
|
private static final String USER_ID = "USER_ID";
|
|
|
|
|
|
/**
|
|
|
* 配置匿名者可以访问的路由,并更新到redis,匿名者默认可以访问的role_name =ROLE_anonymous
|
|
|
* 此方法会将所有符合权限组名=ROLE_anonymous的权限更新到redis中,供gateway调用判断权限
|
...
|
...
|
@@ -86,4 +117,150 @@ public class AnonymousController { |
|
|
return new ResultJson("200","verify get ok",map,verifyToken);
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 验证票据
|
|
|
* @param ticket
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
@RequestMapping (value = "/ssoLogin")
|
|
|
@ResponseBody
|
|
|
public AuthSuccessResponse ssoLogin(String ticket) throws Exception {
|
|
|
try {
|
|
|
log.info("票据接受:{}", ticket);
|
|
|
if (!StringUtils.isEmpty(ticket)){
|
|
|
log.info("开始验证票据");
|
|
|
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");
|
|
|
log.info("验证票据返回值:{}", result);
|
|
|
Map<String, Object> userInfo = resolveUserInfo(result);
|
|
|
if (userInfo != null){
|
|
|
log.info("userInfo:{}", userInfo);
|
|
|
// KakoUser user = kakoUserMapper.findLonginName(userInfo.get("loginName").toString(), userInfo.get("userId").toString());
|
|
|
KakoUser user = kakoUserService.loadByUsername(userInfo.get("loginName").toString());
|
|
|
log.info("查询用户信息:{}", user);
|
|
|
//返回前端登陆成功后的用户信息
|
|
|
KakoUser loginedUser = new KakoUser();
|
|
|
loginedUser.setLoginName(user.getUsername());
|
|
|
loginedUser.setId(user.getId());
|
|
|
loginedUser.setName(user.getName());
|
|
|
loginedUser.setRoles(user.getRoles());
|
|
|
//设置用户的TOKEN的有效时间,时间配置在配置文件中设置
|
|
|
String jwtToken = JwtTokenUtil.generateToken(loginedUser.getUsername(), jwtMaxAlive);
|
|
|
log.info("设置用户token:{}", jwtToken);
|
|
|
loginedUser.setToken(jwtToken);
|
|
|
//这里将登录成功的[user]对象数据写入redis缓存,KEY为token value为user的JSON对象
|
|
|
String json = JSON.toJSONString(user);
|
|
|
boolean set = redisUtils.set(jwtToken, json, 3600 * 24 * 7);
|
|
|
log.info("用户信息存入redis:{}", set);
|
|
|
Map<String,Object> menuMap = permissionService.getUserMenusKako(user.getId());
|
|
|
//返回用户信息和用户可访问的目录列表
|
|
|
return new AuthSuccessResponse(loginedUser,menuMap);
|
|
|
}
|
|
|
return null;
|
|
|
}else {
|
|
|
return null;
|
|
|
}
|
|
|
}catch (Exception e){
|
|
|
e.printStackTrace();
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
public Map<String, Object> resolveUserInfo(String result){
|
|
|
//认证成功,解析用户信息
|
|
|
Gson gson = new Gson();
|
|
|
Map resultMap = gson.fromJson(result, Map.class);
|
|
|
Object serviceResponse = resultMap.get(SERVICE_RESPONESE);
|
|
|
if(serviceResponse != null && serviceResponse instanceof Map){
|
|
|
Object authenticationSuccess = ((Map) serviceResponse).get(AUTHENTICATION_SUCCESS);
|
|
|
if(authenticationSuccess != null && authenticationSuccess instanceof Map){
|
|
|
Map<String, Object> userInfo = new HashMap<>();
|
|
|
userInfo.put("attributes", ((Map) authenticationSuccess).get(ATTRIBUTES));
|
|
|
Object attributes = ((Map) authenticationSuccess).get("attributes");
|
|
|
userInfo.put("loginName", ((Map) attributes).get(LOGIN_NAME));
|
|
|
userInfo.put("userId", ((Map) attributes).get(USER_ID));
|
|
|
return userInfo;
|
|
|
}
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 资源同步
|
|
|
* @param map
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
@PostMapping(value = "/userSynchronization")
|
|
|
@ResponseBody
|
|
|
public ResultMessage userSynchronization(@RequestBody Map<String, Object> map) throws Exception {
|
|
|
if ("user".equals(map.get("resType").toString())){
|
|
|
JsonUtil jsonUtil = new JsonUtil();
|
|
|
List<UserDate> list = jsonUtil.mapToList(map, UserDate.class, "data");
|
|
|
// 判断该用户是否存在
|
|
|
int i = 0;
|
|
|
for (UserDate userDate: list){
|
|
|
log.info("用户信息:{}", userDate);
|
|
|
if (kakoUserMapperl.findLonginName(userDate.getLOGIN_NAME(), "") != null){
|
|
|
KakoUser kakoUser = new KakoUser();
|
|
|
kakoUser.setLoginName(userDate.getLOGIN_NAME());
|
|
|
kakoUser.setPassword(userDate.getPWD());
|
|
|
kakoUser.setUserSyncId(userDate.getUSER_ID());
|
|
|
kakoUser.setMobile(userDate.getMOBILE());
|
|
|
kakoUser.setEmail(userDate.getEMAIL());
|
|
|
kakoUser.setDelFlag("0");
|
|
|
kakoUser.setName(userDate.getUSER_NAME());
|
|
|
kakoUser.setCompanyId("1");
|
|
|
kakoUser.setOfficeId("5e95a0f081c64fce9f85d59e18cfdc5f");
|
|
|
kakoUser.setUpdateBy("tyrz");
|
|
|
kakoUser.setLoginFlag("1");
|
|
|
kakoUser.setUpdateDate(new Date());
|
|
|
|
|
|
log.info("通过账号为条件更新");
|
|
|
i = kakoUserMapperl.updateSyncUserLoginName(kakoUser);
|
|
|
}else {
|
|
|
if (kakoUserMapperl.findLonginName("", userDate.getUSER_ID()) != null){
|
|
|
KakoUser kakoUser = new KakoUser();
|
|
|
kakoUser.setLoginName(userDate.getLOGIN_NAME());
|
|
|
kakoUser.setPassword(userDate.getPWD());
|
|
|
kakoUser.setUserSyncId(userDate.getUSER_ID());
|
|
|
kakoUser.setMobile(userDate.getMOBILE());
|
|
|
kakoUser.setEmail(userDate.getEMAIL());
|
|
|
kakoUser.setDelFlag("0");
|
|
|
kakoUser.setName(userDate.getUSER_NAME());
|
|
|
kakoUser.setCompanyId("1");
|
|
|
kakoUser.setOfficeId("5e95a0f081c64fce9f85d59e18cfdc5f");
|
|
|
kakoUser.setUpdateBy("tyrz");
|
|
|
kakoUser.setLoginFlag("1");
|
|
|
kakoUser.setUpdateDate(new Date());
|
|
|
log.info("账号不存在用过统一认证id更新");
|
|
|
i = kakoUserMapperl.updateSyncUserId(kakoUser);
|
|
|
}else {
|
|
|
KakoUser kakoUser = new KakoUser();
|
|
|
kakoUser.setId(UUID.randomUUID().toString());
|
|
|
kakoUser.setLoginName(userDate.getLOGIN_NAME());
|
|
|
kakoUser.setPassword(userDate.getPWD());
|
|
|
kakoUser.setUserSyncId(userDate.getUSER_ID());
|
|
|
kakoUser.setMobile(userDate.getMOBILE());
|
|
|
kakoUser.setEmail(userDate.getEMAIL());
|
|
|
kakoUser.setName(userDate.getUSER_NAME());
|
|
|
kakoUser.setCompanyId("1");
|
|
|
kakoUser.setOfficeId("5e95a0f081c64fce9f85d59e18cfdc5f");
|
|
|
kakoUser.setLoginFlag("1");
|
|
|
kakoUser.setCreateBy("tyrz");
|
|
|
kakoUser.setUpdateBy("tyrz");
|
|
|
kakoUser.setCreateDate(new Date());
|
|
|
kakoUser.setUpdateDate(new Date());
|
|
|
log.info("账号统一认证id都不在 新增");
|
|
|
i = kakoUserMapperl.insertSelective(kakoUser);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return i > 0 ? new ResultMessage("200","资源同步成功"):new ResultMessage("202", "资源同步失败");
|
|
|
}
|
|
|
return new ResultMessage("201","不是用户信息");
|
|
|
}
|
|
|
} |
...
|
...
|
|