123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- package com.bowintek.practice.controller;
- import com.alibaba.druid.support.logging.Log;
- import com.alibaba.druid.support.logging.LogFactory;
- import com.alibaba.fastjson.JSONObject;
- import com.bowintek.practice.AppConfig;
- import com.bowintek.practice.filter.exception.*;
- import com.bowintek.practice.model.SysUser;
- import com.bowintek.practice.services.service.AccountService;
- import com.bowintek.practice.services.service.UserService;
- import com.bowintek.practice.services.service.system.LogService;
- import com.bowintek.practice.util.*;
- import com.bowintek.practice.vo.user.UserModel;
- import com.fasterxml.jackson.databind.JsonNode;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- import java.net.URLDecoder;
- import java.net.URLEncoder;
- import java.time.LocalDate;
- import java.time.LocalDateTime;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.Map;
- @RestController
- @RequestMapping(value = "/api/oauth")
- @Slf4j
- public class OAuthController {
- @Autowired
- private AppConfig appConfig;
- @Autowired
- private RemoteHelper remoteHelper;
- @Autowired
- private JsonMapper jsonMapper;
- @Autowired
- private StringUtils stringUtils;
- /*@Autowired
- private StudentService studentService;*/
- @Autowired
- private AccountService accountService;
- @Autowired
- private UserService userService;
- @Autowired
- private DateUtils dateUtils;
- @Autowired
- private DesUtils desUtils;
- @Autowired
- private LogService logService;
- @GetMapping("/getOAuthUrl")
- public BaseResponse<String> getOAuthUrl(String appType, String oauthType) {
- String url = "";
- /* String serviceUrl = "web".equals(appType) ? appConfig.oauthconfig_webServiceUrl : appConfig.oauthconfig_mobileServiceUrl;
- if ("oauth".equals(oauthType)) {
- url = appConfig.oauthconfig_oauthUrl + "/auth/oauth/authorize?";
- url += "response_type=code";
- url += "&client_id=" + appConfig.oauthconfig_client_id;
- url += "&redirect_uri=" + serviceUrl;
- url += "&state=login";
- log.info("getOAuthUrl-oauth:" + url);
- } else {
- url = appConfig.oauthconfig_oauthUrl + "/auth/cas/login?";
- if ("web".equals(appType))
- url += "client_id=" + appConfig.oauthconfig_client_id;
- else
- url += "service=" + serviceUrl;
- log.info("getOAuthUrl-cas:" + "appType:" + appType + ",url:" + url);
- }*/
- return RespGenerstor.success(url);
- }
- @GetMapping("/getToken")
- public BaseResponse<String> getToken(String code) {
- JsonNode result;
- String reData;
- Map<String, String> getParams = new HashMap<>();
- getParams.put("client_id", appConfig.oauthconfig_client_id);
- getParams.put("client_secret", appConfig.oauthconfig_client_secret);
- getParams.put("code", code);
- // "{\"code\":1,\"data\":{\"access_token\":\"dd48df1557d39e1dc80f25285835a199ceb87aa9\",\"expires_in\":2592000,\"token_type\":\"bearer\",\"scope\":\"default,js_api\",\"refresh_token\":\"999bcbceba23582f6056e8dff1d3d8a04274cd5e\"},\"message\":\"操作成功\",\"timestamp\":1556107384}"
- reData = remoteHelper.post(getParams, appConfig.oauthconfig_oauthUrl + "/auth/oauth/access_token", "UTF-8");
- log.info("getToken:" + reData);
- result = jsonMapper.jsonToObject(reData, JsonNode.class);
- if (result != null && result.get("code") != null && "1".equals(result.get("code").toString()) && result.get("data") != null)
- return RespGenerstor.success(result.get("data").get("access_token"));
- else
- return RespGenerstor.success("");
- }
- @GetMapping("/oauthLogin")
- public BaseResponse<String> oauthLogin(String type, String token, String tm) {
- log.info("oauthLogin:ticket , tm " + tm + " , type " + type);
- String userCode = "";
- String reData;
- Map<String, String> getParams = new HashMap<>();
- getParams.put("Authorization", "Bearer ${token}");
- reData = remoteHelper.getJson( new HashMap<>(), appConfig.oauthconfig_oauthUrl + "/api/v2/sys/user/currentuser", "UTF-8", getParams);
- log.info("getUserInfo-cas:result " + reData);
- JSONObject jsonData = JSONObject.parseObject(reData);
- if (!"success".equals(jsonData.getString("code"))) {
- log.info("登录失败:" + jsonData.getString("msg"));
- return RespGenerstor.success(false);
- }
- userCode = jsonData.getJSONObject("data").getString("id");
- log.info("oauthLogin:token " + token + " , userCode " + userCode);
- if (stringUtils.IsNullOrEmpty(userCode))
- return RespGenerstor.success(false);
- SysUser sysUser = userService.getUserByLoginID(userCode);
- String userID = sysUser != null ? sysUser.getUserID() : null;
- if (stringUtils.IsNullOrEmpty(userID))
- return RespGenerstor.success(false);
- UserModel user = accountService.getUserByUserID(userID);
- user.token = TokenUtils.sign(user.getUserId() + '|' + user.getUserTypeId());
- user.dataRangeList =new ArrayList<>();
- user.permissionList = accountService.getUserPerms(user.getUserId());
- logService.save("登录", "", "单点登录", user.userId);
- return RespGenerstor.success(user);
- }
- }
|