|
@@ -1,136 +1,116 @@
|
|
|
package com.uas.platform.warehouse.online.api;
|
|
|
|
|
|
-import com.alibaba.fastjson.JSON;
|
|
|
import com.uas.platform.warehouse.online.dao.AppAuthenticationDao;
|
|
|
import com.uas.platform.warehouse.online.entity.AppAuthentication;
|
|
|
import com.uas.platform.warehouse.online.util.AppKeyUtil;
|
|
|
import com.uas.platform.warehouse.online.util.HttpUtil;
|
|
|
-import com.uas.sso.AuthToken;
|
|
|
-import com.uas.sso.SSOConfig;
|
|
|
-import com.uas.sso.SSOHelper;
|
|
|
-import com.uas.sso.SSOToken;
|
|
|
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
|
|
|
+import org.apache.http.client.methods.HttpGet;
|
|
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
+import org.apache.http.impl.client.HttpClients;
|
|
|
import org.apache.log4j.Logger;
|
|
|
import org.joda.time.DateTime;
|
|
|
+import org.junit.Test;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.ui.ModelMap;
|
|
|
-import org.springframework.util.StringUtils;
|
|
|
-import org.springframework.web.bind.annotation.*;
|
|
|
-import org.springframework.web.servlet.ModelAndView;
|
|
|
+import org.springframework.http.MediaType;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.*;
|
|
|
-
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.MalformedURLException;
|
|
|
+import java.net.URL;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
/**
|
|
|
* Created by luhg on 2017/11/10.
|
|
|
* User login.
|
|
|
*/
|
|
|
@RestController
|
|
|
-public class LoginController{
|
|
|
+public class LoginController {
|
|
|
private static final Logger logger = Logger.getLogger(HttpUtil.class);
|
|
|
+ private static final String uid = "uid=4sjrtiHZOu3sG71-9lOyB1jEPXOxBgJ_3jjuug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10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.89 Safari/537.36";
|
|
|
|
|
|
@Autowired
|
|
|
private AppAuthenticationDao authenticationDao;
|
|
|
|
|
|
- @Autowired
|
|
|
- private HttpServletRequest request;
|
|
|
- /**
|
|
|
- * 登录接口
|
|
|
- * @param type 登录类型
|
|
|
- * user:用户
|
|
|
- * provider:供应商
|
|
|
- * @return 返回登录代理页面
|
|
|
- */
|
|
|
- @RequestMapping(value = "/login/{type}", method = RequestMethod.GET)
|
|
|
- @ResponseBody
|
|
|
- public ModelAndView login(@PathVariable String type) {
|
|
|
- request.getSession().setAttribute("type", type);
|
|
|
- return new ModelAndView("/views/proxyLogin.html");
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 在跨域确认账户中心登录之后调用接口,写入登录cookie
|
|
|
- * @param replyTxt 由账户中心返回的数据
|
|
|
- * @return 设置cookie是否成功
|
|
|
- */
|
|
|
- @RequestMapping(value = "/login/crossAfter", method = RequestMethod.POST)
|
|
|
- @ResponseBody
|
|
|
- public ModelMap getAuthenticationByUUAndType(HttpServletResponse response, String replyTxt){
|
|
|
- if (!StringUtils.isEmpty(replyTxt)) {
|
|
|
- SSOConfig config = SSOHelper.getSSOService().getConfig();
|
|
|
- AuthToken token = SSOHelper.ok(request, response, replyTxt, config.getClientPublicKey(), config.getCenterPublicKey());
|
|
|
- if (token != null) {
|
|
|
- SSOToken tk = new SSOToken();
|
|
|
- tk.setUid(token.getUid());
|
|
|
- tk.setTime(token.getTime());
|
|
|
- String type = String.valueOf(request.getSession().getAttribute("type"));
|
|
|
- AppAuthentication appAuthentication = getAuthenticationByToken(token, type);
|
|
|
- if (appAuthentication == null) {
|
|
|
- return new ModelMap("error", true);
|
|
|
- }
|
|
|
- tk.setData(JSON.toJSONString(appAuthentication));
|
|
|
- SSOHelper.setSSOCookie(request, response, tk, true);
|
|
|
+ @RequestMapping(value="/login",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
|
|
+ public AppAuthentication login(HttpServletRequest request,String uu,String name,String type){
|
|
|
+ String check = null;
|
|
|
+ try {
|
|
|
+ check = loginAsk();
|
|
|
+ if(check!=null&&!"".equals(check)){ //未登录
|
|
|
+ return null;
|
|
|
}
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
}
|
|
|
- return new ModelMap("success", true);
|
|
|
+ return getAuthenticationByUUAndType(uu,name,type);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 根据token获得验证
|
|
|
- * @param token
|
|
|
- * @param type
|
|
|
- * @return
|
|
|
- */
|
|
|
- private AppAuthentication getAuthenticationByToken(AuthToken token, String type) {
|
|
|
- AppAuthentication appAuthentication = null;
|
|
|
- if (token.getData() != null) {
|
|
|
- com.uas.account.entity.UserView tokenUser = JSON.parseObject(token.getData(), com.uas.account.entity.UserView.class);
|
|
|
- appAuthentication = authenticationDao.findByEnUu(tokenUser.getSpaceDialectUID());
|
|
|
- if(appAuthentication==null){
|
|
|
- appAuthentication = new AppAuthentication();
|
|
|
- String appKey = AppKeyUtil.createAppKey();
|
|
|
- String secretKey = AppKeyUtil.createSecretKey(appKey);
|
|
|
- appAuthentication.setEnUu(tokenUser.getSpaceDialectUID());
|
|
|
- appAuthentication.setEnName(tokenUser.getSpaceName());
|
|
|
- appAuthentication.setAppKey(appKey);
|
|
|
- appAuthentication.setSecretKey(secretKey);
|
|
|
- appAuthentication.setCreateTime(DateTime.now().toDate());
|
|
|
- appAuthentication.setType(AppAuthentication.IdentificationType.getIdentificationType(type));
|
|
|
- authenticationDao.save(appAuthentication);
|
|
|
- logger.info("创建新用户:"+"企业名称="+tokenUser.getSpaceName()+"&appKey="+appKey+
|
|
|
- "&secretKey="+secretKey+"&type="+AppAuthentication.IdentificationType.getIdentificationType(type));
|
|
|
- }
|
|
|
+ public AppAuthentication getAuthenticationByUUAndType(String uu,String name,String type){
|
|
|
+ AppAuthentication appAuthentication = authenticationDao
|
|
|
+ .findByEnUu(uu);
|
|
|
+
|
|
|
+ if(appAuthentication==null){
|
|
|
+ appAuthentication = new AppAuthentication();
|
|
|
+
|
|
|
+ String appKey = AppKeyUtil.createAppKey();
|
|
|
+ String secretKey = AppKeyUtil.createSecretKey(appKey);
|
|
|
+ appAuthentication.setEnUu(uu);
|
|
|
+ appAuthentication.setEnName(name);
|
|
|
+ appAuthentication.setAppKey(appKey);
|
|
|
+ appAuthentication.setSecretKey(secretKey);
|
|
|
+ appAuthentication.setCreateTime(DateTime.now().toDate());
|
|
|
+ appAuthentication.setType(AppAuthentication.IdentificationType.valueOf(type));
|
|
|
+ authenticationDao.save(appAuthentication);
|
|
|
}
|
|
|
return appAuthentication;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 获取跨域登录的参数
|
|
|
- *
|
|
|
- * @return
|
|
|
- * @throws IOException
|
|
|
- */
|
|
|
- @RequestMapping(value = "/login/crossBefore", method = RequestMethod.GET)
|
|
|
- @ResponseBody
|
|
|
- public ModelMap getCrossLoginData(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
|
|
- ModelMap model = new ModelMap();
|
|
|
- SSOConfig config = SSOHelper.getSSOService().getConfig();
|
|
|
- // 业务系统私钥签名 authToken 自动设置临时会话 cookie 授权后自动销毁
|
|
|
- AuthToken at = SSOHelper.askCiphertext(request, response, config.getClientPrivateKey());
|
|
|
- // askUrl 询问 sso 是否登录地址
|
|
|
- model.addAttribute("askUrl", config.getCrossAskUrl());
|
|
|
- // askTxt 询问 token 密文
|
|
|
- model.addAttribute("askData", at.encryptAuthToken());
|
|
|
- return model;
|
|
|
- }
|
|
|
+/* public String loginCheck() throws IOException{
|
|
|
+ String url = "https://account.ubtob.com/sso/login/ask";
|
|
|
+ CloseableHttpClient httpClient = HttpClients.createDefault();
|
|
|
+ HttpGet httpGet = new HttpGet(url);
|
|
|
+ httpGet.setHeader("Cookie",uid);
|
|
|
+ httpGet.setHeader("User-Agent",userAgent);
|
|
|
+ return httpClient.execute(httpGet, HttpUtil.RESPONSE_HANDLER_STRING);
|
|
|
+ }*/
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public String loginAsk(){
|
|
|
+ //String uid
|
|
|
+ StringBuilder json = new StringBuilder();
|
|
|
+ String url = "https://account.ubtob.com/sso/login/ask";
|
|
|
+
|
|
|
+ try {
|
|
|
+ URL oracle = new URL(url);
|
|
|
+ HttpURLConnection yc = (HttpURLConnection)oracle.openConnection();
|
|
|
+ yc.setRequestMethod("GET");
|
|
|
+ yc.addRequestProperty("Cookie",uid);
|
|
|
+ yc.addRequestProperty("User-Agent",userAgent);
|
|
|
+
|
|
|
+ yc.setDoOutput(true);
|
|
|
+ yc.setDoInput(true);
|
|
|
+ PrintWriter printWriter = new PrintWriter(yc.getOutputStream());
|
|
|
+ printWriter.flush();
|
|
|
|
|
|
- /**
|
|
|
- * 判断是否登录
|
|
|
- * @return
|
|
|
- */
|
|
|
- @RequestMapping("/isLogin")
|
|
|
- public ModelMap isLogin() {
|
|
|
- SSOToken token = SSOHelper.getToken(request);
|
|
|
- return new ModelMap("isLogin", token!=null);
|
|
|
+ yc.setConnectTimeout(6000); //毫秒
|
|
|
+ yc.setReadTimeout(6000); //毫秒
|
|
|
+ BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
|
|
|
+ String inputLine = null;
|
|
|
+ while ( (inputLine = in.readLine()) != null) {
|
|
|
+ json.append(inputLine);
|
|
|
+ }
|
|
|
+ in.close();
|
|
|
+ } catch (MalformedURLException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return json.toString();
|
|
|
}
|
|
|
+
|
|
|
}
|