package com.xiaoya.smart.controller;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang.ObjectUtils.Null;
import org.apache.commons.lang3.StringUtils;
import org.apache.oltu.oauth2.as.issuer.MD5Generator;
import org.apache.oltu.oauth2.as.issuer.OAuthIssuer;
import org.apache.oltu.oauth2.as.issuer.OAuthIssuerImpl;
import org.apache.oltu.oauth2.as.request.OAuthAuthzRequest;
import org.apache.oltu.oauth2.as.request.OAuthTokenRequest;
import org.apache.oltu.oauth2.as.response.OAuthASResponse;
import org.apache.oltu.oauth2.common.OAuth;
import org.apache.oltu.oauth2.common.exception.OAuthProblemException;
import org.apache.oltu.oauth2.common.exception.OAuthSystemException;
import org.apache.oltu.oauth2.common.message.OAuthResponse;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializerProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xiaoya.smart.service.OauthService;
import com.xiaoya.smart.util.Const;
import com.xiaoya.smart.util.JsonUtil;
import com.xiaoya.smart.util.WLYUtil;
import com.zbiti.core.controller.BaseController;
@SuppressWarnings("rawtypes")
@Controller
@RequestMapping("/oauth")
public class OauthController extends BaseController {
private static final ObjectMapper mapper = new ObjectMapper();
private static final Logger log = LoggerFactory.getLogger(OauthController.class);
@Resource(name = "oauthServiceImpl")
OauthService oauthService;
/**
* 处理微信服务器发来的消息
*/
String outURL = "https://服务器域名/项目名/oauth/login.do";
int outlen = outURL.length();
@RequestMapping("/login")
public String approvelogin(HttpServletRequest request, HttpSession session, Model model) {
try {
OAuthAuthzRequest oauthRequest;
oauthRequest = new OAuthAuthzRequest(request);
oauthRequest.getResponseType();
String url = "redirectUrl=" + oauthRequest.getRedirectURI() + "&state=" + oauthRequest.getState()
+ "&client_id=" + oauthRequest.getClientId();
model.addAttribute("url", url);
System.out.println(url);
// String outURL = java.net.URLDecoder.decode(url, "GBK");
// System.out.println("decode后跳转的地址:"+outURL);
// int outlength = outURL.length();
} catch (OAuthSystemException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (OAuthProblemException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
// String responseURL = outURL.substring(cutlength, outlength);
// System.out.println(responseURL);
// OAuthURL = OAuthURL + responseURL;
// System.out.println("decode后要跳转的地址:"+OAuthURL);
return "index";
}
@RequestMapping("/loginOfAliGenie")
public String dologinOfAliGenie(Map<String, Object> out, HttpServletRequest request, HttpServletResponse response,
HttpSession session, Model model) throws Exception {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
String phoneNum = request.getParameter("phonenum");
String password = request.getParameter("password");
String url = request.getHeader("referer");
String deurl = java.net.URLDecoder.decode(url, "GBK");
String outurl = deurl.substring(outlen);
log.info("请求跳转地址:" + url);
log.info("decode地址:" + deurl);
log.info("截取后地址:" + outurl);
Object obj = oauthService.loginOfAliGenie(phoneNum, password);
if (obj.equals("") || obj == null) {
return "index";
} else {
String code = UUID.randomUUID().toString();
code = code.replaceAll("-", "");
oauthService.recoverCode(code, (String) obj);
String response_type = "code";
log.info("看好了" + response_type);
// String outURL = java.net.URLEncoder.encode(redirectUrl, "UTF-8");
// String clientId = (String) session.getAttribute("client_id");
String URL = outurl + "&code=" + code;
String codeurl = "https://服务器域名/项目名/oauth/responseCode.do";
log.info("url===>" + URL);
log.info("codeurl+url==>" + codeurl + URL);
String redirectURL = codeurl + URL;
return "redirect:" + redirectURL;
}
// String userId=(String) obj;
// List<String> list=oauthService.getHomeOfAliGenie(userId);
// model.addAttribute("homelist",list);
// return (String) obj;
}
@RequestMapping("/responseCode")
public Object toShowUser(Model model, HttpServletRequest request) throws IOException {
log.info("----------服务端/responseCode--------------------------------------------------------------");
try {
// 构建OAuth 授权请求
OAuthAuthzRequest oauthRequest = new OAuthAuthzRequest(request);
oauthRequest.getClientId();
oauthRequest.getResponseType();
oauthRequest.getRedirectURI();
log.info("clientId,responseType,redirectURL==>" + oauthRequest.getClientId()
+ oauthRequest.getResponseType() + oauthRequest.getRedirectURI());
String token = oauthRequest.getParam("token");
String state = oauthRequest.getState();
String code = oauthRequest.getParam("code");
log.info("token==>" + token + "state==>" + state + "code==>" + code);
if (oauthRequest.getClientId() != null || oauthRequest.getClientId() != "") {
// 利用oauth授权请求设置responseType,目前仅支持CODE,另外还有TOKEN
String responseType = oauthRequest.getParam(OAuth.OAUTH_RESPONSE_TYPE);
// 进行OAuth响应构建
OAuthASResponse.OAuthAuthorizationResponseBuilder builder = OAuthASResponse
.authorizationResponse(request, HttpServletResponse.SC_FOUND);
// 设置授权码
builder.setParam("token", token);
builder.setParam("state", state);
builder.setCode(code);
// 得到到客户端重定向地址
String redirectURI = oauthRequest.getParam(OAuth.OAUTH_REDIRECT_URI);
// 构建响应
final OAuthResponse response1 = builder.location(redirectURI).buildQueryMessage();
log.info("服务端/responseCode内,返回的回调路径:" + response1.getLocationUri());
String responceUri = response1.getLocationUri();
log.info(responceUri);
// 根据OAuthResponse返回ResponseEntity响应
HttpHeaders headers = new HttpHeaders();
try {
headers.setLocation(new URI(response1.getLocationUri()));
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String strURL = "https://服务器域名/项目名/oauth/responseAc