SpringMVC 攔截web端登陸

@Controller
@RequestMapping(value = "/document")
public class DocumentController {
    
    /**
     * 登錄
     * @param username
     * @param password
     */
    @RequestMapping(value = "login", method = RequestMethod.POST)
    @ResponseBody
    public JsonOverHttpResponse<HashMap>  login(
        @RequestParam String username,
        @RequestParam String password, HttpServletRequest req) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        String psw = MD5Util.md5Hex(username.toLowerCase() + password).toString();
        map.put("loginId", username);
        map.put("password", psw);
        map.put("longitude", 0.11);
        map.put("latitude", 22.112);
        map.put("appName", "interface");
        map.put("appVersion", "0.2");
        map.put("deviceRegistrationId", "123456");
        map.put("mobileOsVersion", "windows");
        map.put("deviceType", "4");
        map.put("mobileType", "web");
        String requestPath = req.getRequestURL().toString();
        requestPath = requestPath.substring(0, requestPath.indexOf("api") + 3);
        JsonOverHttpRequest<HashMap> jsonParam = getParamString(map, "");
        Map<String, Object> mapData = new HashMap<String, Object>();
        String str = this.send(jsonUtils.toJsonString(jsonParam), requestPath + "/user/login");
        return jsonUtils.toObject(str, JsonOverHttpResponse.class);
    }

    private String send(String jsonParam, String path) {
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost method = new HttpPost(path);
        HttpResponse response = null;
        try {
            StringEntity entity = new StringEntity(jsonParam != null ? jsonParam : "", "utf-8");//解決中文亂碼問題
            entity.setContentEncoding("UTF-8");
            entity.setContentType("application/json");
            method.setEntity(entity);
            method.setHeader(new BasicHeader("Accept", "application/json"));
            method.setHeader(new BasicHeader("Accept-Language", "en"));
            response = httpClient.execute(method);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }
        String json = "";
        if (response != null && response.getStatusLine().getStatusCode() == 200) {
            for (Header head : response.getAllHeaders()) {
                System.out.println(head);
            }
            HttpEntity responseEntity = response.getEntity();
            try {
                json = EntityUtils.toString(responseEntity,
                        Charset.forName("UTF-8"));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return json;
    }
    
    private JsonOverHttpRequest<HashMap> getParamString(HashMap<String, Object> param, String token) {
        JsonOverHttpRequest<HashMap> request = new JsonOverHttpRequest<HashMap>();
        JsonOverHttpRequestHeader header = new JsonOverHttpRequestHeader();

        header.setReceiver("jobnow_gateway_V0.0.0.1");
        header.setSender("jobnow_app_android_V0.0.0.1");
        header.setAcceptContent("json");
        header.setAcceptEncoding("UTF-8");
        Date sendTime = new Date();
        //noinspection deprecation
        header.setSendTime(sendTime.toGMTString());
        Map<String, String> parameters = new HashMap<String, String>();
        parameters.put("token", token);
        header.setParameters(parameters);

        request.setHeader(header);
        request.setBody(param);
        return request;
    }
}
相關文章
相關標籤/搜索