Commit f2d7c69d by xieshaohua

sso用户登录日志改造

parent 3e493ed7
...@@ -5,11 +5,13 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; ...@@ -5,11 +5,13 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication @SpringBootApplication
@EnableDiscoveryClient @EnableDiscoveryClient
@EnableFeignClients @EnableFeignClients
@EnableAsync @EnableAsync
@EnableScheduling
public class LoginApplication { public class LoginApplication {
public static void main(String[] args) { public static void main(String[] args) {
......
...@@ -4,6 +4,8 @@ import com.alibaba.fastjson.JSON; ...@@ -4,6 +4,8 @@ import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.keymobile.auth.common.security.CustomizedUserDetailService; import com.keymobile.auth.common.security.CustomizedUserDetailService;
import com.keymobile.login.logging.LogManager; import com.keymobile.login.logging.LogManager;
import com.keymobile.login.persistence.SsoUserRepository;
import com.keymobile.login.persistence.model.SsoUserAbstract;
import com.keymobile.login.service.AuthService; import com.keymobile.login.service.AuthService;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.CloseableHttpResponse;
...@@ -62,6 +64,9 @@ public class LoginManagement { ...@@ -62,6 +64,9 @@ public class LoginManagement {
@Autowired @Autowired
private CustomizedUserDetailService customizedUserDetailService; private CustomizedUserDetailService customizedUserDetailService;
@Autowired
private SsoUserRepository ssoUserRepository;
private static final Logger log = LoggerFactory.getLogger(LoginManagement.class); private static final Logger log = LoggerFactory.getLogger(LoginManagement.class);
...@@ -133,7 +138,8 @@ public class LoginManagement { ...@@ -133,7 +138,8 @@ public class LoginManagement {
HttpSession session = request.getSession(true); HttpSession session = request.getSession(true);
session.setAttribute("SPRING_SECURITY_CONTEXT", SecurityContextHolder.getContext()); session.setAttribute("SPRING_SECURITY_CONTEXT", SecurityContextHolder.getContext());
log.info("单点登录用户:" + userName); log.info("单点登录用户:" + userName);
MDC.put("user", userName + "(" + userDName + ")");
MDC.put("user", getLogUserInfo(userName, userDName));
MDC.put("session", session.getId()); MDC.put("session", session.getId());
LogManager.logInfo(Constants.SSO_API, "登录"); LogManager.logInfo(Constants.SSO_API, "登录");
response.sendRedirect(ssoRedirectUrl); response.sendRedirect(ssoRedirectUrl);
...@@ -313,6 +319,18 @@ public class LoginManagement { ...@@ -313,6 +319,18 @@ public class LoginManagement {
return null; return null;
} }
private String getLogUserInfo(String userName, String userDName) {
SsoUserAbstract user = ssoUserRepository.findById(userName).orElse(null);
if (user != null) {
return userName + "(" + userDName + "/" + user.getOrganizationname() + ")";
} else {
return userName + "(" + userDName + ")";
}
}
private Boolean needUpate(JSONObject ssoUser, Map<String, Object> user) { private Boolean needUpate(JSONObject ssoUser, Map<String, Object> user) {
if (ssoUser == null || user == null) { if (ssoUser == null || user == null) {
return false; return false;
......
...@@ -20,6 +20,7 @@ import org.springframework.data.domain.*; ...@@ -20,6 +20,7 @@ import org.springframework.data.domain.*;
import org.springframework.data.jpa.domain.Specification; import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Predicate;
...@@ -145,7 +146,7 @@ public class PeopleCenterApi { ...@@ -145,7 +146,7 @@ public class PeopleCenterApi {
direction = Sort.Direction.ASC; direction = Sort.Direction.ASC;
} }
Pageable pageable = PageRequest.of(pageNo - 1, pageSize, Sort.by(direction, sortField)); Pageable pageable = PageRequest.of(pageNo - 1, pageSize, Sort.by(direction, sortField));
Specification<SsoUserMcclAbstract> specification = getSpecification(keyword, username, chinesename, employeenumber, organizationid, isalllevel, organizationname); Specification<SsoUserMcclAbstract> specification = getUserMcclSpecification(keyword, username, chinesename, employeenumber, organizationid, isalllevel, organizationname);
Page<SsoUserMcclAbstract> pages = ssoUserMcclRepository.findAll(specification, pageable); Page<SsoUserMcclAbstract> pages = ssoUserMcclRepository.findAll(specification, pageable);
return pages; return pages;
} }
...@@ -173,16 +174,47 @@ public class PeopleCenterApi { ...@@ -173,16 +174,47 @@ public class PeopleCenterApi {
} else { } else {
direction = Sort.Direction.ASC; direction = Sort.Direction.ASC;
} }
Specification<SsoUserMcclAbstract> specification = getSpecification(keyword, username, chinesename, employeenumber, organizationid, isalllevel, organizationname); Specification<SsoUserMcclAbstract> specification = getUserMcclSpecification(keyword, username, chinesename, employeenumber, organizationid, isalllevel, organizationname);
List<SsoUserMcclAbstract> list = ssoUserMcclRepository.findAll(specification, Sort.by(direction, sortField)); List<SsoUserMcclAbstract> list = ssoUserMcclRepository.findAll(specification, Sort.by(direction, sortField));
return list; return list;
} }
@PostMapping(value = "/listUser")
public List<SsoUserAbstract> listUser(@RequestParam(required = false) String keyword,
@RequestParam(required = false) String username,
@RequestParam(required = false) String chinesename,
@RequestParam(required = false) String employeenumber,
@RequestParam(required = false) String organizationid,
@RequestParam(required = false, defaultValue = "true") Boolean isalllevel,
@RequestParam(required = false) String organizationname,
@ApiParam("排序字段") @RequestParam(required = false) String sortingType,
@ApiParam("排序规则 ASC DESC") @RequestParam(required = false) String sortingRule) {
String sortField = "";
if (StringUtils.isNotBlank(sortingType)) {
sortField = sortingType;
} else {
sortField = "employeenumber";
}
Sort.Direction direction = null;
if (StringUtils.equalsIgnoreCase(sortingRule, "DESC")) {
direction = Sort.Direction.DESC;
} else {
direction = Sort.Direction.ASC;
}
Specification<SsoUserAbstract> specification = getUserSpecification(keyword, username, chinesename, employeenumber, organizationid, isalllevel, organizationname);
List<SsoUserAbstract> list = ssoUserRepository.findAll(specification, Sort.by(direction, sortField));
return list;
}
@PostMapping(value = "/syncMccl") @PostMapping(value = "/syncMccl")
public void syncMccl() { public void syncMccl() {
log.info("开始Mccl同步");
try (Connection conn = dataSource.getConnection(); try (Connection conn = dataSource.getConnection();
Statement st = conn.createStatement()) { Statement st = conn.createStatement()) {
String updateAuthUser = "update auth_user a inner join sso_user b on (a.user_name=b.employeenumber and b.adid is not null ) set a.eid =b.adid";
String deleteOrganMccl = "delete from sso_organ_mccl"; String deleteOrganMccl = "delete from sso_organ_mccl";
String deleteUserMccl = "delete from sso_user_mccl"; String deleteUserMccl = "delete from sso_user_mccl";
String insertOrganMccl = "insert into sso_organ_mccl select * from sso_organ so where organizationname like '%MCCL%' or organizationid ='" + organRootId + "'"; String insertOrganMccl = "insert into sso_organ_mccl select * from sso_organ so where organizationname like '%MCCL%' or organizationid ='" + organRootId + "'";
...@@ -203,6 +235,7 @@ public class PeopleCenterApi { ...@@ -203,6 +235,7 @@ public class PeopleCenterApi {
" set a.path=b.path, " + " set a.path=b.path, " +
" a.idpath=b.idpath " + " a.idpath=b.idpath " +
" where a.path is null and b.path is not null"; " where a.path is null and b.path is not null";
st.executeUpdate(updateAuthUser);
st.executeUpdate(deleteOrganMccl); st.executeUpdate(deleteOrganMccl);
st.executeUpdate(deleteUserMccl); st.executeUpdate(deleteUserMccl);
st.executeUpdate(insertOrganMccl); st.executeUpdate(insertOrganMccl);
...@@ -219,8 +252,9 @@ public class PeopleCenterApi { ...@@ -219,8 +252,9 @@ public class PeopleCenterApi {
} }
} }
st.executeUpdate(updateUserPathSql); st.executeUpdate(updateUserPathSql);
} catch (SQLException throwables) { log.info("完成Mccl同步");
throwables.printStackTrace(); } catch (Exception e) {
log.error("mccl同步失败",e);
} }
} }
...@@ -251,15 +285,36 @@ public class PeopleCenterApi { ...@@ -251,15 +285,36 @@ public class PeopleCenterApi {
@RequestMapping(value = "/refreshSyncKey", method = {RequestMethod.POST, RequestMethod.GET}) @RequestMapping(value = "/refreshSyncKey", method = {RequestMethod.POST, RequestMethod.GET})
public void refreshSyncKey(){ public void refreshSyncKey() {
redisTemplate.delete("pcToken"); redisTemplate.delete("pcToken");
} }
@RequestMapping(value = "/userSync", method = {RequestMethod.POST, RequestMethod.GET}) @RequestMapping(value = "/userSync", method = {RequestMethod.POST, RequestMethod.GET})
@Async @Async
public void userSync(HttpServletRequest request, @RequestParam(required = false) Integer startPage, public void userAsync(@RequestParam(required = false) Integer startPage,
@RequestParam(required = false, defaultValue = "false") Boolean isDelete) { @RequestParam(required = false, defaultValue = "true") Boolean isDelete) {
userSync(startPage, isDelete);
}
@RequestMapping(value = "/organSync", method = {RequestMethod.POST, RequestMethod.GET})
@Async
public void organAsync(@RequestParam(required = false) Integer startPage,
@RequestParam(required = false, defaultValue = "true") Boolean isDelete) {
organSync(startPage, isDelete);
}
@RequestMapping(value = "/sync", method = {RequestMethod.POST, RequestMethod.GET})
@Scheduled(cron = "${peopleCenter.cron:0 0 22 ? * 6}")
public void sync() {
log.info("开始用户和机构全量同步");
userSync(null, true);
organSync(null, true);
syncMccl();
log.info("完成用户和机构全量同步");
}
public void userSync(Integer startPage, Boolean isDelete) {
log.info("开始用户同步"); log.info("开始用户同步");
String token = redisTemplate.opsForValue().get("pcToken"); String token = redisTemplate.opsForValue().get("pcToken");
if (StringUtils.isBlank(token)) { if (StringUtils.isBlank(token)) {
...@@ -318,16 +373,10 @@ public class PeopleCenterApi { ...@@ -318,16 +373,10 @@ public class PeopleCenterApi {
} }
} }
log.info("完成用户同步"); log.info("完成用户同步");
} }
@RequestMapping(value = "/organSync", method = {RequestMethod.POST, RequestMethod.GET}) public void organSync(Integer startPage, Boolean isDelete) {
@Async
public void organSync(HttpServletRequest request, @RequestParam(required = false) Integer startPage,
@RequestParam(required = false, defaultValue = "false") Boolean isDelete) {
String token = redisTemplate.opsForValue().get("pcToken"); String token = redisTemplate.opsForValue().get("pcToken");
if (StringUtils.isBlank(token)) { if (StringUtils.isBlank(token)) {
token = getToken(); token = getToken();
...@@ -347,7 +396,6 @@ public class PeopleCenterApi { ...@@ -347,7 +396,6 @@ public class PeopleCenterApi {
log.info("删除旧数据"); log.info("删除旧数据");
ssoOrganRepository.deleteAll(); ssoOrganRepository.deleteAll();
} }
while (true) { while (true) {
Map<String, Object> body = new HashMap<>(); Map<String, Object> body = new HashMap<>();
...@@ -387,7 +435,6 @@ public class PeopleCenterApi { ...@@ -387,7 +435,6 @@ public class PeopleCenterApi {
} }
} }
log.info("完成机构同步"); log.info("完成机构同步");
} }
...@@ -416,7 +463,7 @@ public class PeopleCenterApi { ...@@ -416,7 +463,7 @@ public class PeopleCenterApi {
} }
} }
private Specification<SsoUserMcclAbstract> getSpecification(String keyword, private Specification<SsoUserMcclAbstract> getUserMcclSpecification(String keyword,
String username, String username,
String chinesename, String chinesename,
String employeenumber, String employeenumber,
...@@ -458,4 +505,46 @@ public class PeopleCenterApi { ...@@ -458,4 +505,46 @@ public class PeopleCenterApi {
return specification; return specification;
} }
private Specification<SsoUserAbstract> getUserSpecification(String keyword,
String username,
String chinesename,
String employeenumber,
String organizationid,
Boolean isalllevel,
String organizationname) {
Specification<SsoUserAbstract> specification = (root, cq, cb) -> {
List<Predicate> list = new ArrayList<>();
if (StringUtils.isNotEmpty(username)) {
list.add(cb.equal(root.get("username"), username));
}
if (StringUtils.isNotBlank(keyword)) {
list.add(cb.or(cb.like(root.get("username"), "%" + username + "%"),
cb.like(root.get("chinesename"), "%" + chinesename + "%"),
cb.like(root.get("employeenumber"), "%" + employeenumber + "%")));
}
if (StringUtils.isNotEmpty(chinesename)) {
list.add(cb.like(root.get("chinesename"), "%" + chinesename + "%"));
}
if (StringUtils.isNotEmpty(employeenumber)) {
list.add(cb.equal(root.get("employeenumber"), employeenumber));
}
if (StringUtils.isNotEmpty(organizationid)) {
if (isalllevel) {
list.add(cb.or(cb.like(root.get("idpath"), "%/" + organizationid + "/%"),
cb.like(root.get("idpath"), "%/" + organizationid)));
} else {
list.add(cb.equal(root.get("organizationid"), organizationid));
}
}
if (StringUtils.isNotEmpty(organizationname)) {
list.add(cb.equal(root.get("organizationname"), organizationname));
}
cq.where(cb.and(list.toArray(new Predicate[0])));
return cq.getRestriction();
};
return specification;
}
} }
package com.keymobile.login.persistence; package com.keymobile.login.persistence;
import com.keymobile.login.persistence.model.SsoUserAbstract; import com.keymobile.login.persistence.model.SsoUserAbstract;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.CrudRepository;
import javax.transaction.Transactional; import javax.transaction.Transactional;
...@@ -13,4 +15,7 @@ public interface SsoUserRepository extends CrudRepository<SsoUserAbstract, Strin ...@@ -13,4 +15,7 @@ public interface SsoUserRepository extends CrudRepository<SsoUserAbstract, Strin
List<SsoUserAbstract> getByOrganizationnameLike(String organName); List<SsoUserAbstract> getByOrganizationnameLike(String organName);
List<SsoUserAbstract> findAll(Specification<SsoUserAbstract> spec, Sort sort);
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment