单点登录之,客户端
来源:互联网 发布:苏州高博软件 编辑:程序博客网 时间:2024/06/06 02:54
惯例:
我是温浩然:
单点登录的客户端配置,客户端不直接操作数据库,而是与其他项目相结合,对浏览器进行操作。
下面贴登录中,客户端的代码。
package com.tujia.tuuser.controller;import java.io.IOException;import java.util.Map;import javax.annotation.Resource;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import net.sf.json.JSONObject;import org.apache.http.HttpResponse;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.methods.HttpPost;import org.apache.http.entity.StringEntity;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.util.EntityUtils;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import com.google.gson.Gson;import com.google.gson.JsonObject;import com.tujia.corelogin.security.SecurityUtil;import com.tujia.tucommon.config.ResultCode;import com.tujia.tucommon.controller.BaseController;import com.tujia.tuuser.entity.User;@Controllerpublic class UserLoginController extends BaseController {@Resourceprivate SecurityUtil securityUtil;@RequestMapping(value = "/tulogin", produces = "text/html") public String listByUser(Model model,HttpServletRequest request, HttpServletResponse response) { securityUtil.requrl(request,response); return "tulogin"; }//没有权限访问此请求@RequestMapping(value = "/noauthority", produces = "text/html") public String noAuthority(Model model,HttpServletRequest request, HttpServletResponse response) { return "noAuthority"; }/** * 退出接口 */@RequestMapping(value = "/tulogout",method =RequestMethod.POST)public void logout(HttpServletRequest request, HttpServletResponse response) {ResultCode resultCode = ResultCode.SUCCEED;securityUtil.logout(request, response);Object result = null;setResponse(response, resultCode, result);}@RequestMapping(value = "/tulogin")public void login(HttpServletRequest request,HttpServletResponse response,@RequestBody Map<String, Object> map){Object resultCode = ResultCode.SUCCEED;Object result = null;securityUtil.requrl(request,response);String phone = (String) map.get("phone");String password = (String) map.get("password");String requrl = request.getRequestURI();DefaultHttpClient httpClient = new DefaultHttpClient();HttpPost method = new HttpPost("http://localhost:8080/tu-login/tuloginMethod");JSONObject jsonParam = new JSONObject();jsonParam.put("phone", phone);jsonParam.put("password", password);jsonParam.put("requrl", requrl);StringEntity entity = new StringEntity(jsonParam.toString(), "utf-8");entity.setContentEncoding("UTF-8");entity.setContentType("application/json");method.setEntity(entity);HttpResponse resGet;try {//通过DefaultHttpClient 来获得返回的参数(值)resGet = httpClient.execute(method);//将返回值设置编码格式,(避免乱码)String resData = EntityUtils.toString(resGet.getEntity(),"utf-8");//通过net.sf.json.JSONObject 来解析字符串JSONObject resJSON = JSONObject.fromObject(resData);//把json中的user对象获取,并强转。Object userjson = resJSON.get("user");String userString = userjson.toString();//通过com.google.gson.Gson 来处理json 类型的user对象。Gson gson = new Gson();//user就是转换后的对象。//在这里,我对com.google.gson.JsonObject有点疑问,为啥这个不能解析返回的字符串呢?//这个类有什么作用?JsonObject jsonObj = new JsonObject();jsonObj.getAsJsonObject(resData);if("A00000".equals(resJSON.get("code"))){User user = gson.fromJson((String) userString, User.class);String token = (String)resJSON.get("token");securityUtil.addCookieToken(request,response,token,user.getId());request.getSession().setAttribute("user", user);}if ( !"A00000".equals(resJSON.get("code"))) {result = resJSON.get("data");}else{result = securityUtil.getReqURL(request,response);}resultCode = resJSON.get("code");} catch (ClassCastException e) {logger.error(e.getMessage());resultCode = ResultCode.INTERNAL_ERROR;result = e.getMessage();}catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}setResponse(response, resultCode, result);}}
在这里面,比较难的就是,与服务器交互的这一段代码,反正我以前是没听过没用过。其他的地方,没有什么难的了。再就是其他的一些具体的需求了。
0 0
- 单点登录之,客户端
- 单点登录之,客户端
- CAS单点登录源码解析之【客户端】
- 单点登录CAS7-客户端配置单点登录
- Mvc4单点登录之三Cas 客户端配置
- 1、[置顶] CAS单点登录源码解析之【客户端】
- 服务器客户端单点登录,单点注销详解
- Cas单点登录客户端配置
- java之单点登录
- java之单点登录
- 单点登录(八)cas支持客户端登录——客户端
- 单点登录学习(3)CAS客户端配置
- 单点登录(四)cas客户端配置
- cas 单点登录服务端客户端配置
- CAS单点登录python客户端分析
- CAS单点登录客户端--C++版
- CAS单点登录服务端与客户端配置
- 单点登录CAS系列8-客户端配置单点登出
- animation - 3
- Java中如何聪明地使用锁
- Iterator的使用
- PHP验证码实现与倒计时
- CocoaPods安装和使用教程
- 单点登录之,客户端
- 8 个最优秀的 Android Studio 插件
- httpclient总结
- 解决 PHPEXCEL 纯数字 科学计数的问题
- 黑马-C语言-枚举
- Java 多线程面试问题汇总
- 欢迎使用CSDN-markdown编辑器
- 黑马程序员—Java中的IO流
- Windows 集群