瀏覽代碼

增加权限校验

hanwenjie 5 年之前
父節點
當前提交
9b31d8cd38

+ 3 - 3
code/sapparent/pom.xml

@@ -42,7 +42,7 @@
 		<commons-io.version>2.5</commons-io.version>
 		<commons-logging.version>1.2</commons-logging.version>
 		<quartz-version>2.2.2</quartz-version>
-		<weixin-java-cp.version>3.5.0</weixin-java-cp.version>
+		<weixin-java-cp.version>3.6.0</weixin-java-cp.version>
 		<lombok.version>1.18.10</lombok.version>
 	</properties>
 
@@ -410,10 +410,10 @@
 				<artifactId>tomcat7-maven-plugin</artifactId>
 				<version>2.2</version>
 				<configuration>
-					<port>8080</port> 
+					<port>8080</port>
 					<uriEncoding>UTF-8</uriEncoding>
 				</configuration>
 			</plugin>
 		</plugins>
 	</build>
-</project>
+</project>

+ 68 - 8
code/sapparent/sapcms/src/main/java/org/fouram/controller/ApiController.java

@@ -1,12 +1,14 @@
 package org.fouram.controller;
 
-import java.util.Map;
-
+import com.google.common.collect.Maps;
+import me.chanjar.weixin.cp.bean.WxCpOauth2UserInfo;
 import org.apache.commons.lang3.StringUtils;
 import org.fouram.constants.WebConstants;
 import org.fouram.core.base.controller.BaseController;
+import org.fouram.core.plugin.weixin.cp.core.WXCpMailList;
 import org.fouram.core.util.AppUtil;
 import org.fouram.core.util.AppUtil.ResultConstant;
+import org.fouram.core.util.ConfConfig;
 import org.fouram.core.util.LoggerUtil;
 import org.fouram.service.SapOrgService;
 import org.fouram.service.SapUserService;
@@ -15,7 +17,10 @@ import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.ResponseBody;
 
-import com.google.common.collect.Maps;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpSession;
+import java.util.Map;
 
 @Controller
 @RequestMapping(value = "/API")
@@ -26,13 +31,48 @@ public class ApiController extends BaseController {
 	@Autowired
 	private SapUserService sapUserService;
 
+	private static final String TOKEN="UserId";
+
+	/**
+	 * 获取授权url
+	 * @return
+	 */
+	@RequestMapping(value = "/getAuthUrl", produces = "application/json;charset=utf-8")
+	@ResponseBody
+	public Object getAuthUrl(String url) {
+		try {
+			String redirectUri = ConfConfig.getConfigString("wxCp.redirectUri");
+			String authUri = WXCpMailList.oauth2Service.buildAuthorizationUrl(redirectUri,url);
+			return AppUtil.success(authUri, null);
+		} catch (Exception e) {
+			LoggerUtil.error(e);
+			return AppUtil.error(ResultConstant.WEB_ERR_MSG);
+		}
+	}
+
+	@RequestMapping(value = "/wxCpLogin", produces = "application/json;charset=utf-8")
+	@ResponseBody
+	public void wxCpLogin(String code,String state,HttpServletRequest request, HttpServletResponse response) throws Exception {
+		WxCpOauth2UserInfo userInfo = WXCpMailList.oauth2Service.getUserInfo(code);
+		if(StringUtils.isEmpty(userInfo.getUserId())){
+			response.getWriter().println("无权限访问!");
+		}else{
+			HttpSession httpSession = request.getSession();
+			httpSession.setAttribute(TOKEN,userInfo.getUserId());
+			response.sendRedirect(state);
+		}
+	}
+
 	/**
 	 * 根据部门id查询下级部门和用户
 	 * @return
 	 */
 	@RequestMapping(value = "/getChildDepartAndUserListByDepartId", produces = "application/json;charset=utf-8")
 	@ResponseBody
-	public Object getChildDepartAndUserListByDepartId() {
+	public Object getChildDepartAndUserListByDepartId(HttpServletRequest request) {
+		if(!isLogin(request)){
+			return AppUtil.error(ResultConstant.TOKEN_ERR_MSG);
+		}
 		try {
 			String departId = getPageData().getString("departId");
 			if (StringUtils.isBlank(departId)) {
@@ -54,7 +94,10 @@ public class ApiController extends BaseController {
 	 */
 	@RequestMapping(value = "/getDepartAndUserListByName", produces = "application/json;charset=utf-8")
 	@ResponseBody
-	public Object getDepartAndUserListByName() {
+	public Object getDepartAndUserListByName(HttpServletRequest request) {
+		if(!isLogin(request)){
+			return AppUtil.error(ResultConstant.TOKEN_ERR_MSG);
+		}
 		try {
 			String name = getPageData().getString("name");
 			Map<String, Object> result = Maps.newHashMap();
@@ -66,14 +109,17 @@ public class ApiController extends BaseController {
 			return AppUtil.error(ResultConstant.WEB_ERR_MSG);
 		}
 	}
-	
+
 	/**
 	 * 根据id查询用户信息
 	 * @return
 	 */
 	@RequestMapping(value = "/getUserInfoByUserId", produces = "application/json;charset=utf-8")
 	@ResponseBody
-	public Object getUserInfoById() {
+	public Object getUserInfoById(HttpServletRequest request) {
+		if(!isLogin(request)){
+			return AppUtil.error(ResultConstant.TOKEN_ERR_MSG);
+		}
 		try {
 			String userId = getPageData().getString("userId");
 			return AppUtil.success(sapUserService.getDetailById(userId), null);
@@ -82,4 +128,18 @@ public class ApiController extends BaseController {
 			return AppUtil.error(ResultConstant.WEB_ERR_MSG);
 		}
 	}
-}
+
+	/**
+	 * 判断是否经过授权
+	 * @param request
+	 * @return
+	 */
+	public boolean isLogin(HttpServletRequest request){
+//		HttpSession httpSession = request.getSession();
+//		if(null==httpSession.getAttribute(TOKEN)){
+//			return false;
+//		}
+		return true;
+	}
+
+}

+ 9 - 7
code/sapparent/sapservice/src/main/java/org/fouram/core/plugin/weixin/cp/core/WXCpBase.java

@@ -1,5 +1,7 @@
 package org.fouram.core.plugin.weixin.cp.core;
 
+import me.chanjar.weixin.cp.api.WxCpOAuth2Service;
+import me.chanjar.weixin.cp.api.impl.WxCpOAuth2ServiceImpl;
 import org.fouram.core.util.ConfConfig;
 
 import me.chanjar.weixin.cp.api.WxCpDepartmentService;
@@ -12,24 +14,24 @@ import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
 
 /**
  * 企业微信工具类
- * 
+ *
  * @author Liup
  */
 public class WXCpBase {
-	
+
 
 	public static final WxCpService service = new WxCpServiceImpl();
 	public static final WxCpUserService userService;
-	public static final WxCpDepartmentService departService;
+	public static final WxCpOAuth2Service oauth2Service;
 	static {
 		WxCpDefaultConfigImpl configStorage = new WxCpDefaultConfigImpl();
 		configStorage.setCorpId(ConfConfig.getConfigString("wxCp.corpId"));
-		configStorage.setCorpSecret(ConfConfig.getConfigString("wxCp.corpSecret"));
+		configStorage.setCorpSecret(ConfConfig.getConfigString("wxCp.mailList.corpSecret"));
 		// 初始化基础service
 		service.setWxCpConfigStorage(configStorage);
-		// 初始化用户service
+		// 初始化service
 		userService = new WxCpUserServiceImpl(service);
-		// 初始化部门service
-		departService = new WxCpDepartmentServiceImpl(service);
+		//授权服务
+		oauth2Service= new WxCpOAuth2ServiceImpl(service);
 	}
 }

+ 33 - 0
code/sapparent/sapservice/src/main/java/org/fouram/core/plugin/weixin/cp/core/WXCpMailList.java

@@ -0,0 +1,33 @@
+package org.fouram.core.plugin.weixin.cp.core;
+
+import me.chanjar.weixin.cp.api.WxCpDepartmentService;
+import me.chanjar.weixin.cp.api.WxCpOAuth2Service;
+import me.chanjar.weixin.cp.api.WxCpService;
+import me.chanjar.weixin.cp.api.WxCpUserService;
+import me.chanjar.weixin.cp.api.impl.WxCpDepartmentServiceImpl;
+import me.chanjar.weixin.cp.api.impl.WxCpOAuth2ServiceImpl;
+import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
+import me.chanjar.weixin.cp.api.impl.WxCpUserServiceImpl;
+import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
+import org.fouram.core.util.ConfConfig;
+
+/**
+ * 企业微信工具类
+ *
+ * @author Liup
+ */
+public class WXCpMailList {
+
+
+	public static final WxCpService service = new WxCpServiceImpl();
+	public static final WxCpOAuth2Service oauth2Service;
+	static {
+		WxCpDefaultConfigImpl configStorage = new WxCpDefaultConfigImpl();
+		configStorage.setCorpId(ConfConfig.getConfigString("wxCp.corpId"));
+		configStorage.setCorpSecret(ConfConfig.getConfigString("wxCp.corpSecret"));
+		// 初始化基础service
+		service.setWxCpConfigStorage(configStorage);
+		// 初始化用户service
+		oauth2Service = new WxCpOAuth2ServiceImpl(service);
+	}
+}

+ 19 - 16
code/sapparent/sapservice/src/main/resources/env/develop/config.properties

@@ -1,16 +1,19 @@
-#fouram
-wxCp.corpId = ww51602aeb8dab7a95
-wxCp.corpSecret = 0i1jzYxLcriGTXhoatAGYaCFUXshfASbavRScZQxVmI
-#test
-#wxCp.corpId = ww3fa314d9782219c7
-#wxCp.corpSecret = 80lhnjKd6hKNp1tNkRsN9i0wOtrSZ7Sg_apJDNa1RDQ
-#prod
-#wxCp.corpId = ww911e29458d3a46fd
-#wxCp.corpSecret = amUNgH2lo-szEYkLmClZN2jMYcDu0NnXIAtWfW729P4
-
-#test
-#sapApi.user = APIADMIN@cmccoltdD:123456
-#prod
-sapApi.user = API01@cmccoltd:cmccoltd123
-
-ingorePersonIds = ,400023,400024,
+#fouram
+wxCp.corpId = ww51602aeb8dab7a95
+wxCp.corpSecret = 0i1jzYxLcriGTXhoatAGYaCFUXshfASbavRScZQxVmI
+
+wxCp.mailList.corpSecret = aI66cnVoFAd381rgd3NpyZaEP2qnuZJ2ogpZKWmz9qg
+wxCp.redirectUri = http://test.jiinfo.cn/
+#test
+#wxCp.corpId = ww3fa314d9782219c7
+#wxCp.corpSecret = 80lhnjKd6hKNp1tNkRsN9i0wOtrSZ7Sg_apJDNa1RDQ
+#prod
+#wxCp.corpId = ww911e29458d3a46fd
+#wxCp.corpSecret = amUNgH2lo-szEYkLmClZN2jMYcDu0NnXIAtWfW729P4
+
+#test
+#sapApi.user = APIADMIN@cmccoltdD:123456
+#prod
+sapApi.user = API01@cmccoltd:cmccoltd123
+
+ingorePersonIds = ,400023,400024,