解决BUG,新增功能

This commit is contained in:
wenc000 2019-10-26 15:55:49 +08:00
parent d224efd431
commit edca1adbd0
21 changed files with 992 additions and 48 deletions

View File

@ -5,20 +5,20 @@
<parent>
<artifactId>cm-cloud</artifactId>
<groupId>com.cm</groupId>
<version>1.0.0.RELEASE</version>
<version>1.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<description>单点登录客户端使用的通用jar包</description>
<artifactId>cloud-common-plugin-oauth</artifactId>
<version>1.0.0.RELEASE</version>
<version>1.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.cm</groupId>
<artifactId>cloud-common-plugin</artifactId>
<version>1.0.0.RELEASE</version>
<version>1.0.1-SNAPSHOT</version>
</dependency>
</dependencies>

View File

@ -64,6 +64,11 @@ public interface IApiConsts {
*/
String LIST_USER_DEPARTMENT_USER_BY_ROLE = "%s/resource/user/listuserdepartmentuserbyrole/%s/%s";
/**
* App获取用户所在部门用户列表通过角色
*/
String APP_LIST_USER_DEPARTMENT_USER_BY_ROLE = "%s/app/user/listuserdepartmentuserbyrole/%s/%s";
/**
* 获取用户所在部门用户列表通过职位
*/

View File

@ -3,11 +3,13 @@ package com.cm.common.plugin.oauth.service.menu.impl;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cm.common.base.AbstractService;
import com.cm.common.component.OAuthRestTemplateComponent;
import com.cm.common.config.properties.ApiPathProperties;
import com.cm.common.exception.AccessTokenException;
import com.cm.common.exception.SearchException;
import com.cm.common.plugin.IApiConsts;
import com.cm.common.plugin.oauth.service.menu.IMenuService;
import com.cm.common.plugin.oauth.token.ClientTokenManager;
import com.cm.common.plugin.utils.RestTemplateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
@ -31,6 +33,8 @@ public class MenuServiceImpl extends AbstractService implements IMenuService {
@Autowired
private RestTemplateUtil restTemplateUtil;
@Autowired
private OAuthRestTemplateComponent oAuthRestTemplateComponent;
@Autowired
private ApiPathProperties apiPathProperties;
@Override

View File

@ -98,6 +98,15 @@ public interface IUserService {
*/
JSONArray listUserDepartmentUserByRole(Map<String, Object> params) throws AccessTokenException, SearchException;
/**
* APP获取用户所在部门用户列表通过角色
*
* @param token
* @param userParams
* @return
*/
JSONArray listUserDepartmentUserByRole(String token, Map<String, Object> userParams) throws SearchException;
/**
* 获取用户所在部门用户列表通过职位
*
@ -118,4 +127,5 @@ public interface IUserService {
*/
JSONObject getUserDetail(Map<String, Object> params) throws AccessTokenException, SearchException;
JSONArray listRoleUsers(String accessToken, Map<String, Object> params) throws AccessTokenException, SearchException;
}

View File

@ -3,14 +3,13 @@ package com.cm.common.plugin.oauth.service.user.impl;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cm.common.base.AbstractService;
import com.cm.common.component.OAuthRestTemplateComponent;
import com.cm.common.config.properties.ApiPathProperties;
import com.cm.common.exception.AccessTokenException;
import com.cm.common.exception.SearchException;
import com.cm.common.exception.TokenException;
import com.cm.common.plugin.IApiConsts;
import com.cm.common.plugin.oauth.service.user.IUserService;
import com.cm.common.plugin.utils.RestTemplateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@ -33,6 +32,8 @@ public class UserServiceImpl extends AbstractService implements IUserService {
private RestTemplateUtil restTemplateUtil;
@Autowired
private ApiPathProperties apiPathProperties;
@Autowired
private OAuthRestTemplateComponent oAuthRestTemplateComponent;
@Override
public JSONArray listDepartmentUsers(Map<String, Object> params) throws AccessTokenException, SearchException {
@ -130,6 +131,15 @@ public class UserServiceImpl extends AbstractService implements IUserService {
return JSONArray.parseArray(result);
}
@Override
public JSONArray listUserDepartmentUserByRole(String token, Map<String, Object> params) throws SearchException {
String result = restTemplateUtil.doGetFormForApp(token, String.format(IApiConsts.APP_LIST_USER_DEPARTMENT_USER_BY_ROLE, apiPathProperties.getUserCenter(), params.get("userId").toString(), params.get("roleId").toString()), params);
if (result == null || result.isEmpty()) {
throw new SearchException("获取人员列表失败");
}
return JSONArray.parseArray(result);
}
@Override
public JSONArray listUserDepartmentUserByPosition(Map<String, Object> params) throws AccessTokenException, SearchException {
String result = restTemplateUtil.doPostForm(String.format(IApiConsts.LIST_USER_DEPARTMENT_USER_BY_POSITION, apiPathProperties.getUserCenter(), params.get("userId").toString(), params.get("positionId").toString()), params);
@ -154,4 +164,16 @@ public class UserServiceImpl extends AbstractService implements IUserService {
return JSONObject.parseObject(result);
}
@Override
public JSONArray listRoleUsers(String accessToken, Map<String, Object> params) throws AccessTokenException, SearchException {
String result = oAuthRestTemplateComponent.doPostFormForOAuth2Client(accessToken, String.format(IApiConsts.LIST_ROLE_USER, apiPathProperties.getUserCenter(), params.get("roleId").toString()), params);
if (result == null) {
throw new AccessTokenException("认证失败");
}
if (result.isEmpty()) {
throw new SearchException("获取人员列表失败");
}
return JSONArray.parseArray(result);
}
}

View File

@ -0,0 +1,33 @@
package com.cm.common.plugin.oauth.timer;
import com.cm.common.config.properties.OauthClientProperties;
import com.cm.common.config.properties.OauthProperties;
import com.cm.common.plugin.pojo.bos.ClientTokenBO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
/**
* When you feel like quitting. Think about why you started
* 当你想要放弃的时候想想当初你为何开始
*
* @ClassName: ClientTokenTimer
* @Description: 客户端token定时器
* @Author: WangGeng
* @Date: 2019/10/16 12:13 上午
* @Version: 1.0
**/
@Configuration
@EnableScheduling
public class ClientTokenTimer {
@Autowired
private OauthProperties oauthProperties;
@Autowired
private OauthClientProperties oauthClientProperties;
private static ClientTokenBO clientTokenBO;
public void refreshToken() {
}
}

View File

@ -0,0 +1,100 @@
package com.cm.common.plugin.oauth.token;
import com.alibaba.fastjson.JSONObject;
import com.cm.common.config.properties.OauthClientProperties;
import com.cm.common.config.properties.OauthProperties;
import com.cm.common.plugin.pojo.bos.ClientTokenBO;
import com.cm.common.plugin.utils.RestTemplateUtil;
import com.hazelcast.core.Client;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.parameters.P;
import java.util.HashMap;
import java.util.Map;
/**
* When you feel like quitting. Think about why you started
* 当你想要放弃的时候想想当初你为何开始
*
* @ClassName: ClientTokenManager
* @Description: 客户单token
* @Author: WangGeng
* @Date: 2019/10/16 12:22 上午
* @Version: 1.0
**/
public class ClientTokenManager {
private static final Logger LOG = LoggerFactory.getLogger(ClientTokenManager.class);
private static ClientTokenManager clientTokenManager = ClientTokenBuilder.clientTokenManager;
private ClientTokenBO clientToken;
private RestTemplateUtil restTemplateUtil;
private OauthProperties oauthProperties;
private OauthClientProperties oauthClientProperties;
private ClientTokenManager() {
}
public static ClientTokenManager getInstance() {
return clientTokenManager;
}
public static void setClientTokenManager(ClientTokenManager clientTokenManager) {
ClientTokenManager.clientTokenManager = clientTokenManager;
}
public ClientTokenBO getClientToken() {
if (clientToken == null || (System.currentTimeMillis() - clientToken.getCreateTime()) / 1000 >= clientToken.getExpiresIn()) {
LOG.debug("客户端Token失效获取token");
Map<String, Object> params = new HashMap<>(4);
params.put("grant_type", "client_credentials");
params.put("client_id", oauthClientProperties.getClientId());
params.put("client_secret", oauthClientProperties.getClientSecret());
params.put("scope", "all");
String result = restTemplateUtil.doPostFormNormal(String.format("%s/oauth/token", oauthProperties.getOauthServer()), params);
if (StringUtils.isBlank(result)) {
LOG.error("客户端获取token失效");
} else {
JSONObject accessTokenObject = JSONObject.parseObject(result);
clientToken = new ClientTokenBO();
clientToken.setAccessToken(accessTokenObject.getString("access_token"));
clientToken.setTokenType(accessTokenObject.getString("token_type"));
clientToken.setExpiresIn(accessTokenObject.getInteger("expires_in"));
clientToken.setScope(accessTokenObject.getString("scope"));
clientToken.setJti(accessTokenObject.getString("jti"));
clientToken.setCreateTime(System.currentTimeMillis());
}
}
return clientToken;
}
public RestTemplateUtil getRestTemplateUtil() {
return restTemplateUtil;
}
public void setRestTemplateUtil(RestTemplateUtil restTemplateUtil) {
this.restTemplateUtil = restTemplateUtil;
}
public OauthProperties getOauthProperties() {
return oauthProperties;
}
public void setOauthProperties(OauthProperties oauthProperties) {
this.oauthProperties = oauthProperties;
}
public OauthClientProperties getOauthClientProperties() {
return oauthClientProperties;
}
public void setOauthClientProperties(OauthClientProperties oauthClientProperties) {
this.oauthClientProperties = oauthClientProperties;
}
private static class ClientTokenBuilder {
public static ClientTokenManager clientTokenManager = new ClientTokenManager();
}
}

View File

@ -0,0 +1,88 @@
package com.cm.common.plugin.pojo.bos;
/**
* When you feel like quitting. Think about why you started
* 当你想要放弃的时候想想当初你为何开始
*
* @ClassName: ClientTokenBO
* @Description: 客户端token
* @Author: WangGeng
* @Date: 2019/10/16 12:17 上午
* @Version: 1.0
**/
public class ClientTokenBO {
private String accessToken;
private String tokenType;
private int expiresIn;
private String scope;
private String jti;
private long createTime;
public String getAccessToken() {
return accessToken == null ? "" : accessToken.trim();
}
public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}
public String getTokenType() {
return tokenType == null ? "" : tokenType.trim();
}
public void setTokenType(String tokenType) {
this.tokenType = tokenType;
}
public int getExpiresIn() {
return expiresIn;
}
public void setExpiresIn(int expiresIn) {
this.expiresIn = expiresIn;
}
public String getScope() {
return scope == null ? "" : scope.trim();
}
public void setScope(String scope) {
this.scope = scope;
}
public String getJti() {
return jti == null ? "" : jti.trim();
}
public void setJti(String jti) {
this.jti = jti;
}
public long getCreateTime() {
return createTime;
}
public void setCreateTime(long createTime) {
this.createTime = createTime;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("{");
sb.append("\"accessToken\":")
.append("\"").append(accessToken).append("\"");
sb.append(",\"tokenType\":")
.append("\"").append(tokenType).append("\"");
sb.append(",\"expiresIn\":")
.append(expiresIn);
sb.append(",\"scope\":")
.append("\"").append(scope).append("\"");
sb.append(",\"jti\":")
.append("\"").append(jti).append("\"");
sb.append(",\"createTime\":")
.append(createTime);
sb.append('}');
return sb.toString();
}
}

View File

@ -0,0 +1,85 @@
package com.cm.common.plugin.push;
import com.cm.common.component.OAuthRestTemplateComponent;
import com.cm.common.config.properties.ApiPathProperties;
import com.cm.common.config.properties.OauthClientProperties;
import com.cm.common.plugin.utils.RestTemplateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.*;
/**
* When you feel like quitting. Think about why you started
* 当你想要放弃的时候想想当初你为何开始
*
* @ClassName: PushMessage
* @Description:
* @Author: WangGeng
* @Date: 2019/10/14 4:07 下午
* @Version: 1.0
**/
@Component
public class PushMessage {
private static final Logger LOG = LoggerFactory.getLogger(PushMessage.class);
/**
* APP推送的地址
*/
private static final String APP_PUSH_URL = "%s/app/push/pushmessage/%s";
/**
* 客户端推送地址
*/
private static final String RESOURCE_PUSH_CONTENT_URL = "%s/resource/push/pushmessagetouserwithcontent";
@Autowired
private ApiPathProperties apiPathProperties;
@Autowired
private RestTemplateUtil restTemplateUtil;
@Autowired
private OAuthRestTemplateComponent oAuthRestTemplateComponent;
private static Executor executor = new ThreadPoolExecutor(2, 10, 60000, TimeUnit.MILLISECONDS, new LinkedBlockingDeque<>());
/**
* app推送
*
* @param token
* @param userId
* @param title
* @param content
*/
public void appPush(String token, String userId, String title, String content) {
Map<String, Object> params = new HashMap<>(2);
params.put("title", title);
params.put("content", content);
executor.execute(() -> {
String result = restTemplateUtil.doPostJsonForApp(token, String.format(APP_PUSH_URL, apiPathProperties.getUserCenter(), userId), params);
if (result == null) {
LOG.error("消息推送失败");
} else {
LOG.info(result);
}
});
}
/**
* 客户端推送
*
* @param token
* @param params
*/
public void clientPush(String token, Map<String, Object> params) {
executor.execute(() -> {
String result = oAuthRestTemplateComponent.doPostJsonForOAuth2Client(token, String.format(RESOURCE_PUSH_CONTENT_URL, apiPathProperties.getUserCenter()), params);
if (result == null) {
LOG.error("消息推送失败");
} else {
LOG.info(result);
}
});
}
}

View File

@ -2,14 +2,12 @@ package com.cm.common.plugin.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cm.common.config.properties.ApiPathProperties;
import com.cm.common.config.properties.OauthClientProperties;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientProperties;
import org.springframework.http.*;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.security.oauth2.client.OAuth2ClientContext;
@ -19,7 +17,6 @@ import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;
/**
@ -62,6 +59,44 @@ public class RestTemplateUtil {
}
}
/**
* 执行GET请求
*
* @param url
* @param params
* @return
*/
public String doGetFormForApp(String token, String url, Map<String, Object> params) {
RestTemplate restTemplate = getRestTemplate();
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
httpHeaders.add("token", token);
HttpEntity<String> httpEntity = new HttpEntity<>(null, httpHeaders);
try {
return getResponse(restTemplate.exchange(String.format("%s?%s", url, queryParams(params)), HttpMethod.GET, httpEntity, String.class));
} catch (Exception e) {
LOG.error(e.getMessage(), e);
return null;
}
}
/**
* 正常GET的form表单
*
* @param url
* @param params
* @return
*/
public String doGetFormNormal(String url, Map<String, Object> params) {
RestTemplate restTemplate = getRestTemplate();
try {
return getResponse(restTemplate.getForEntity(String.format("%s?%s", url, queryParams(params)), String.class, params));
} catch (Exception e) {
LOG.error(e.getMessage(), e);
return null;
}
}
/**
* post提交 form 数据
*
@ -81,12 +116,69 @@ public class RestTemplateUtil {
MultiValueMap<String, Object> queryParams = new LinkedMultiValueMap<>();
queryParams.add("access_token", accessToken);
return doPost(url, params, httpHeaders, queryParams);
}
/**
* app发送post表单请求
*
* @param token
* @param url
* @param params
* @return
*/
public String doPostFormForApp(String token, String url, Map<String, Object> params) {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
httpHeaders.add("token", token);
MultiValueMap<String, Object> queryParams = new LinkedMultiValueMap<>();
return doPost(url, params, httpHeaders, queryParams);
}
/**
* 正常POST的form表单
*
* @param url
* @param params
* @return
*/
public String doPostFormNormal(String url, Map<String, Object> params) {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, Object> queryParams = new LinkedMultiValueMap<>();
return doPost(url, params, httpHeaders, queryParams);
}
/**
* 发送post请求
*
* @param url
* @param params
* @param httpHeaders
* @param queryParams
* @return
*/
private String doPost(String url, Map<String, Object> params, HttpHeaders httpHeaders, MultiValueMap<String, Object> queryParams) {
for (Map.Entry<String, Object> kv : params.entrySet()) {
queryParams.add(kv.getKey(), kv.getValue());
}
HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(queryParams, httpHeaders);
RestTemplate restTemplate = getRestTemplate();
try {
return getResponse(restTemplate.postForEntity(url, httpEntity, String.class));
} catch (Exception e) {
LOG.error(e.getMessage(), e);
return null;
}
}
public String doPostJsonForApp(String token, String url, Map<String, Object> params) {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
httpHeaders.add("token", token);
HttpEntity<String> httpEntity = new HttpEntity<>(JSONObject.toJSONString(params), httpHeaders);
RestTemplate restTemplate = getRestTemplate();
try {
return getResponse(restTemplate.postForEntity(url, httpEntity, String.class));

View File

@ -5,18 +5,18 @@
<parent>
<artifactId>cm-cloud</artifactId>
<groupId>com.cm</groupId>
<version>1.0.0.RELEASE</version>
<version>1.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-common-plugin</artifactId>
<version>1.0.0.RELEASE</version>
<version>1.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.cm</groupId>
<artifactId>cloud-common</artifactId>
<version>1.0.0.RELEASE</version>
<version>1.0.1-SNAPSHOT</version>
</dependency>
</dependencies>

View File

@ -6,6 +6,7 @@ import com.cm.common.component.SecurityComponent;
import com.cm.common.plugin.pojo.vos.systemlogger.SystemLoggerVO;
import com.cm.common.plugin.service.systemlogger.ISystemLoggerService;
import com.cm.common.pojo.bos.UserInfoBO;
import com.cm.common.utils.DateUtil;
import com.cm.common.utils.RequestUtil;
import com.cm.common.utils.UUIDUtil;
import com.cm.common.utils.WMapUtil;
@ -45,7 +46,7 @@ public class ApiLogAspect {
@Around("apiCutPoint()")
public Object apiLogAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
return logAround(proceedingJoinPoint);
return logAroundForApi(proceedingJoinPoint);
}
/**
@ -57,7 +58,7 @@ public class ApiLogAspect {
@Around("resourceCutPoint()")
public Object resourcesLogAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
return logAround(proceedingJoinPoint);
return logAroundForResource(proceedingJoinPoint);
}
/**
@ -66,7 +67,7 @@ public class ApiLogAspect {
* @param proceedingJoinPoint
* @return
*/
private Object logAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
private Object logAroundForApi(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
HttpServletRequest request = RequestUtil.getRequest();
Signature signature = proceedingJoinPoint.getSignature();
StringBuilder paramValue = new StringBuilder();
@ -92,6 +93,7 @@ public class ApiLogAspect {
result = proceedingJoinPoint.proceed();
long endTime = System.currentTimeMillis();
long usedTime = endTime - startTime;
String currentTime = DateUtil.getTime();
SystemLoggerVO systemLoggerVO = new SystemLoggerVO();
systemLoggerVO.setHost(request.getRemoteAddr());
systemLoggerVO.setPort(request.getLocalPort());
@ -108,8 +110,8 @@ public class ApiLogAspect {
systemLoggerVO.setResponseResult(result == null ? "null" : JSON.toJSONString(result));
Map<String, Object> params = WMapUtil.beanToMapObj(systemLoggerVO);
params.put("systemLoggerId", UUIDUtil.getUUID());
params.put("gmtCreate", usedTime);
params.put("gmtModified", endTime);
params.put("gmtCreate", currentTime);
params.put("gmtModified", currentTime);
UserInfoBO userInfoBO = securityComponent.getCurrentUser();
if (null != userInfoBO) {
String userId = userInfoBO.getUserId();
@ -124,4 +126,54 @@ public class ApiLogAspect {
return result;
}
private Object logAroundForResource(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
HttpServletRequest request = RequestUtil.getRequest();
String accessToken = request.getParameter("accessToken");
Signature signature = proceedingJoinPoint.getSignature();
StringBuilder paramValue = new StringBuilder();
Object[] args = proceedingJoinPoint.getArgs();
for (Object arg : args) {
// 去除无效的参数
if (arg instanceof WebStatFilter.StatHttpServletResponseWrapper) {
continue;
}
if (paramValue.length() > 0) {
paramValue.append("_wg_");
}
try {
paramValue.append(JSON.toJSONString(arg));
} catch (Exception e) {
e.printStackTrace();
}
}
Object result;
long startTime = System.currentTimeMillis();
result = proceedingJoinPoint.proceed();
long endTime = System.currentTimeMillis();
long usedTime = endTime - startTime;
String currentTime = DateUtil.getTime();
SystemLoggerVO systemLoggerVO = new SystemLoggerVO();
systemLoggerVO.setHost(request.getRemoteAddr());
systemLoggerVO.setPort(request.getLocalPort());
systemLoggerVO.setRequestIp(RequestUtil.getRequestIp());
systemLoggerVO.setContextPath(request.getContextPath());
systemLoggerVO.setUri(request.getRequestURI().replace(request.getContextPath(), ""));
systemLoggerVO.setParameterValues(paramValue.toString());
systemLoggerVO.setClassNameFull(signature.getDeclaringTypeName());
systemLoggerVO.setClassName(signature.getDeclaringType().getSimpleName());
systemLoggerVO.setMethodName(signature.getName());
systemLoggerVO.setStartTime(startTime);
systemLoggerVO.setEndTime(endTime);
systemLoggerVO.setUsedTime(usedTime);
systemLoggerVO.setResponseResult(result == null ? "null" : JSON.toJSONString(result));
Map<String, Object> params = WMapUtil.beanToMapObj(systemLoggerVO);
params.put("systemLoggerId", UUIDUtil.getUUID());
params.put("gmtCreate", currentTime);
params.put("gmtModified", currentTime);
params.put("creator", accessToken);
params.put("creatorName", accessToken);
systemLoggerService.saveSystemLogger(params);
return result;
}
}

View File

@ -37,7 +37,6 @@ public class SystemLoggerServiceImpl extends AbstractService implements ISystemL
@Override
public SuccessResult saveSystemLogger(Map<String, Object> params) throws SaveException {
setSaveInfo(params);
systemLoggerDao.saveSystemLogger(params);
return new SuccessResult();
}

View File

@ -5,12 +5,12 @@
<parent>
<artifactId>cm-cloud</artifactId>
<groupId>com.cm</groupId>
<version>1.0.0.RELEASE</version>
<version>1.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-common</artifactId>
<version>1.0.0.RELEASE</version>
<version>1.0.1-SNAPSHOT</version>
<dependencies>

View File

@ -0,0 +1,404 @@
package com.cm.common.component;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cm.common.config.properties.OauthClientProperties;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.*;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.security.oauth2.client.OAuth2ClientContext;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;
/**
* When you feel like quitting. Think about why you started
* 当你想要放弃的时候想想当初你为何开始
*
* @ClassName: OAuthRestTemplateUtil
* @Description: OAuth的Rest请求工具类
* @Author: WangGeng
* @Date: 2019/10/16 10:17 下午
* @Version: 1.0
**/
@Component
public class OAuthRestTemplateComponent {
private static Logger LOG = LoggerFactory.getLogger(OAuthRestTemplateComponent.class);
@Qualifier("oauth2ClientContext")
private OAuth2ClientContext oAuth2ClientContext;
@Autowired
private OauthClientProperties oauthClientProperties;
/**
* GET的FORM表单请求
*
* @param url
* @param params
* @return
*/
public String doGetForm(String url, Map<String, Object> params) {
params = getParams(params);
HttpHeaders httpHeaders = getFormHeader();
return getGetResponse(httpHeaders, url, params);
}
/**
* Oauth2客户端执行GET的FORM表单请求
*
* @param accessToken
* @param url
* @param params
* @return
*/
public String doGetFormForOAuth2Client(String accessToken, String url, Map<String, Object> params) {
params = getParams(params);
params.put("access_token", accessToken);
HttpHeaders httpHeaders = getFormHeader();
return getGetResponse(httpHeaders, url, params);
}
/**
* Oauth2上下文执行GET的FORM表单请求用户登录后
*
* @param url
* @param params
* @return
*/
public String doGetFormForOAuth2Context(String url, Map<String, Object> params) {
params = getParams(params);
refreshOAuth2ContextAccessToken();
String accessToken = getOAuth2ContextAccessToken();
if (StringUtils.isBlank(accessToken)) {
return null;
}
params.put("access_token", accessToken);
HttpHeaders httpHeaders = getFormHeader();
return getGetResponse(httpHeaders, url, params);
}
/**
* APP执行GET的FORM表单请求
*
* @param token
* @param url
* @param params
* @return
*/
public String doGetFormForApp(String token, String url, Map<String, Object> params) {
params = getParams(params);
HttpHeaders httpHeaders = getFormHeader();
httpHeaders.add("token", token);
return getGetResponse(httpHeaders, url, params);
}
/**
* 获取GET的FORM表单请求的结果
*
* @param httpHeaders
* @param url
* @param params
* @return
*/
private String getGetResponse(HttpHeaders httpHeaders, String url, Map<String, Object> params) {
HttpEntity<String> httpEntity = new HttpEntity<>(null, httpHeaders);
RestTemplate restTemplate = getRestTemplate();
try {
return getResponse(restTemplate.exchange(String.format("%s?%s", url, getQueryParams(params)), HttpMethod.GET, httpEntity, String.class));
} catch (Exception e) {
LOG.error(e.getMessage(), e);
return null;
}
}
/**
* POST的FORM表单请求
*
* @param url
* @param params
* @return
*/
public String doPostForm(String url, Map<String, Object> params) {
params = getParams(params);
HttpHeaders httpHeaders = getFormHeader();
return getPostFormResponse(httpHeaders, url, params);
}
/**
* POST的JSON请求
*
* @param url
* @param params
* @return
*/
public String doPostJson(String url, Map<String, Object> params) {
HttpHeaders httpHeaders = getJsonHeader();
return getPostJsonResponse(httpHeaders, url, params);
}
/**
* Oauth2客户端执行POST的FORM表单请求
*
* @param accessToken
* @param url
* @param params
* @return
*/
public String doPostFormForOAuth2Client(String accessToken, String url, Map<String, Object> params) {
params = getParams(params);
HttpHeaders httpHeaders = getFormHeader();
return getPostFormResponse(httpHeaders, String.format("%s?access_token=%s", url, accessToken), params);
}
/**
* Oauth2客户端执行POST的JSON表单请求
*
* @param token
* @param url
* @param params
* @return
*/
public String doPostJsonForOAuth2Client(String accessToken, String url, Map<String, Object> params) {
HttpHeaders httpHeaders = getJsonHeader();
return getPostJsonResponse(httpHeaders, String.format("%s?access_token=%s", url, accessToken), params);
}
/**
* Oauth2上下文执行POST的FORM表单请求用户登录后
*
* @param url
* @param params
* @return
*/
public String doPostFormForOAuth2Context(String url, Map<String, Object> params) {
params = getParams(params);
refreshOAuth2ContextAccessToken();
String accessToken = getOAuth2ContextAccessToken();
if (StringUtils.isBlank(accessToken)) {
return null;
}
HttpHeaders httpHeaders = getFormHeader();
return getPostFormResponse(httpHeaders, String.format("%s?access_token=%s", url, accessToken), params);
}
/**
* Oauth2上下文执行POST的JSON请求用户登录后
*
* @param token
* @param url
* @param params
* @return
*/
public String doPostJsonForOAuth2Context(String token, String url, Map<String, Object> params) {
params = getParams(params);
refreshOAuth2ContextAccessToken();
String accessToken = getOAuth2ContextAccessToken();
if (StringUtils.isBlank(accessToken)) {
return null;
}
HttpHeaders httpHeaders = getJsonHeader();
return getPostJsonResponse(httpHeaders, String.format("%s?access_token=%s", url, accessToken), params);
}
/**
* APP执行POST的FORM表单请求
*
* @param token
* @param url
* @param params
* @return
*/
public String doPostFormForApp(String token, String url, Map<String, Object> params) {
params = getParams(params);
HttpHeaders httpHeaders = getFormHeader();
httpHeaders.add("token", token);
return getPostFormResponse(httpHeaders, url, params);
}
/**
* APP执行POST的JSON请求
*
* @param token
* @param url
* @param params
* @return
*/
public String doPostJsonForApp(String token, String url, Map<String, Object> params) {
HttpHeaders httpHeaders = getJsonHeader();
httpHeaders.add("token", token);
return getPostJsonResponse(httpHeaders, url, params);
}
/**
* 获取POST的FORM表单请求的结果
*
* @param httpHeaders
* @param url
* @param params
* @return
*/
private String getPostFormResponse(HttpHeaders httpHeaders, String url, Map<String, Object> params) {
MultiValueMap<String, Object> queryParams = new LinkedMultiValueMap<>();
for (Map.Entry<String, Object> kv : params.entrySet()) {
queryParams.add(kv.getKey(), kv.getValue());
}
HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(queryParams, httpHeaders);
return getPostResponse(httpEntity, url);
}
/**
* 获取POST的JSON请求的结果
*
* @param httpHeaders
* @param url
* @param params
* @return
*/
private String getPostJsonResponse(HttpHeaders httpHeaders, String url, Map<String, Object> params) {
HttpEntity<String> httpEntity = new HttpEntity<>(JSONObject.toJSONString(params), httpHeaders);
return getPostResponse(httpEntity, url);
}
/**
* 获取POST请求结果
*
* @param httpEntity
* @param url
* @return
*/
private String getPostResponse(HttpEntity httpEntity, String url) {
RestTemplate restTemplate = getRestTemplate();
try {
return getResponse(restTemplate.postForEntity(url, httpEntity, String.class));
} catch (Exception e) {
LOG.error(e.getMessage(), e);
return null;
}
}
/**
* 获取参数
*
* @param params
* @return
*/
private Map<String, Object> getParams(Map<String, Object> params) {
if (params == null) {
return new HashMap<>(0);
}
return params;
}
/**
* 获取form表单请求header
*
* @return
*/
private HttpHeaders getFormHeader() {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
return httpHeaders;
}
/**
* 获取json请求header
*
* @return
*/
private HttpHeaders getJsonHeader() {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
return httpHeaders;
}
/**
* 获取restTemplate
*
* @return
*/
private RestTemplate getRestTemplate() {
SimpleClientHttpRequestFactory simpleClientHttpRequestFactory = new SimpleClientHttpRequestFactory();
simpleClientHttpRequestFactory.setConnectTimeout(20 * 1000);
simpleClientHttpRequestFactory.setReadTimeout(60 * 1000);
return new RestTemplate(simpleClientHttpRequestFactory);
}
/**
* 返回参数
*
* @param responseEntity
* @return
*/
private String getResponse(ResponseEntity<String> responseEntity) {
LOG.debug(">>>> 请求状态:" + responseEntity.getStatusCodeValue());
if (HttpStatus.OK.value() == responseEntity.getStatusCodeValue()) {
return responseEntity.getBody();
} else if (HttpStatus.UNAUTHORIZED.value() == responseEntity.getStatusCodeValue()) {
return null;
}
return "";
}
/**
* 请求参数
*
* @param queryParams
* @return
*/
private String getQueryParams(Map<String, Object> queryParams) {
StringBuilder urlParams = new StringBuilder();
for (Map.Entry<String, Object> kv : queryParams.entrySet()) {
if (urlParams.length() > 0) {
urlParams.append("&");
}
urlParams.append(kv.getKey()).append("=").append(kv.getValue());
}
return urlParams.toString();
}
/**
* 刷新
*/
private void refreshOAuth2ContextAccessToken() {
if (oAuth2ClientContext.getAccessToken().getExpiresIn() < 1800) {
LOG.debug("accessToken时间小于1800s刷新token");
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, Object> queryParams = new LinkedMultiValueMap<>();
queryParams.add("grant_type", "refresh_token");
queryParams.add("refresh_token", oAuth2ClientContext.getAccessToken().getRefreshToken().getValue());
queryParams.add("client_id", oauthClientProperties.getClientId());
queryParams.add("client_secret", oauthClientProperties.getClientSecret());
HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(queryParams, httpHeaders);
RestTemplate restTemplate = getRestTemplate();
try {
String result = getResponse(restTemplate.postForEntity(oauthClientProperties.getAccessTokenUri(), httpEntity, String.class));
if (!StringUtils.isBlank(result)) {
Map<String, String> tokenMap = JSON.parseObject(result, Map.class);
oAuth2ClientContext.setAccessToken(DefaultOAuth2AccessToken.valueOf(tokenMap));
}
} catch (Exception e) {
LOG.error(e.getMessage(), e);
}
}
}
/**
* 获取accessToken
*
* @return
*/
private String getOAuth2ContextAccessToken() {
return oAuth2ClientContext.getAccessToken().getValue();
}
}

View File

@ -0,0 +1,50 @@
package com.cm.common.pojo.vos.push;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
/**
* When you feel like quitting. Think about why you started
* 当你想要放弃的时候想想当初你为何开始
*
* @ClassName: PushMessageDTO
* @Description: 推送消息
* @Author: WangGeng
* @Date: 2019/10/13 7:32 下午
* @Version: 1.0
**/
@ApiModel
public class PushMessageVO {
@ApiModelProperty(name = "title", value = "标题")
private String title;
@ApiModelProperty(name = "content", value = "内容")
private String content;
public String getTitle() {
return title == null ? "" : title.trim();
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content == null ? "" : content.trim();
}
public void setContent(String content) {
this.content = content;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("{");
sb.append("\"title\":")
.append("\"").append(title).append("\"");
sb.append(",\"content\":")
.append("\"").append(content).append("\"");
sb.append('}');
return sb.toString();
}
}

View File

@ -20,9 +20,9 @@ public class AppTokenUser {
private String username;
private String phone;
private String email;
private List<AppTokenUserRole> appTokenUserRoles;
private List<AppTokenUserPosition> appTokenUserPositions;
private List<AppTokenUserDepartment> appTokenUserDepartments;
private List<AppTokenUserRole> roles;
private List<AppTokenUserPosition> positions;
private List<AppTokenUserDepartment> departments;
public String getId() {
return id == null ? "" : id.trim();
@ -72,28 +72,28 @@ public class AppTokenUser {
this.email = email;
}
public List<AppTokenUserRole> getAppTokenUserRoles() {
return appTokenUserRoles;
public List<AppTokenUserRole> getRoles() {
return roles;
}
public void setAppTokenUserRoles(List<AppTokenUserRole> appTokenUserRoles) {
this.appTokenUserRoles = appTokenUserRoles;
public void setRoles(List<AppTokenUserRole> roles) {
this.roles = roles;
}
public List<AppTokenUserPosition> getAppTokenUserPositions() {
return appTokenUserPositions;
public List<AppTokenUserPosition> getPositions() {
return positions;
}
public void setAppTokenUserPositions(List<AppTokenUserPosition> appTokenUserPositions) {
this.appTokenUserPositions = appTokenUserPositions;
public void setPositions(List<AppTokenUserPosition> positions) {
this.positions = positions;
}
public List<AppTokenUserDepartment> getAppTokenUserDepartments() {
return appTokenUserDepartments;
public List<AppTokenUserDepartment> getDepartments() {
return departments;
}
public void setAppTokenUserDepartments(List<AppTokenUserDepartment> appTokenUserDepartments) {
this.appTokenUserDepartments = appTokenUserDepartments;
public void setDepartments(List<AppTokenUserDepartment> departments) {
this.departments = departments;
}
@Override
@ -111,12 +111,12 @@ public class AppTokenUser {
.append("\"").append(phone).append("\"");
sb.append(",\"email\":")
.append("\"").append(email).append("\"");
sb.append(",\"appTokenUserRoles\":")
.append(appTokenUserRoles);
sb.append(",\"appTokenUserPositions\":")
.append(appTokenUserPositions);
sb.append(",\"appTokenUserDepartments\":")
.append(appTokenUserDepartments);
sb.append(",\"roles\":")
.append(roles);
sb.append(",\"positions\":")
.append(positions);
sb.append(",\"departments\":")
.append(departments);
sb.append('}');
return sb.toString();
}

View File

@ -5,12 +5,12 @@
<parent>
<artifactId>cm-cloud</artifactId>
<groupId>com.cm</groupId>
<version>1.0.0.RELEASE</version>
<version>1.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-security</artifactId>
<version>1.0.0.RELEASE</version>
<version>1.0.1-SNAPSHOT</version>
<dependencies>
<dependency>

View File

@ -5,13 +5,13 @@
<parent>
<artifactId>cm-cloud</artifactId>
<groupId>com.cm</groupId>
<version>1.0.0.RELEASE</version>
<version>1.0.1-SNAPSHOT</version>
</parent>
<description>接收系统校验</description>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-token-in</artifactId>
<version>1.0.0.RELEASE</version>
<version>1.0.1-SNAPSHOT</version>
<dependencies>
<dependency>

View File

@ -5,13 +5,13 @@
<parent>
<artifactId>cm-cloud</artifactId>
<groupId>com.cm</groupId>
<version>1.0.0.RELEASE</version>
<version>1.0.1-SNAPSHOT</version>
</parent>
<description>发送系统校验</description>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-token-out</artifactId>
<version>1.0.0.RELEASE</version>
<version>1.0.1-SNAPSHOT</version>
<dependencies>
<dependency>

View File

@ -6,7 +6,7 @@
<groupId>com.cm</groupId>
<artifactId>cm-cloud</artifactId>
<version>1.0.0.RELEASE</version>
<version>1.0.1-SNAPSHOT</version>
<modules>
<module>cloud-common</module>
<module>cloud-security</module>