单点登录之,客户端

来源:互联网 发布:php记录访客信息 编辑:程序博客网 时间:2024/06/06 03:05

惯例:

我是温浩然:

单点登录的客户端配置,客户端不直接操作数据库,而是与其他项目相结合,对浏览器进行操作。

下面贴登录中,客户端的代码。

[java] view plain copy
  1. package com.tujia.tuuser.controller;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.Map;  
  5.   
  6. import javax.annotation.Resource;  
  7. import javax.servlet.http.HttpServletRequest;  
  8. import javax.servlet.http.HttpServletResponse;  
  9.   
  10. import net.sf.json.JSONObject;  
  11.   
  12. import org.apache.http.HttpResponse;  
  13. import org.apache.http.client.ClientProtocolException;  
  14. import org.apache.http.client.methods.HttpPost;  
  15. import org.apache.http.entity.StringEntity;  
  16. import org.apache.http.impl.client.DefaultHttpClient;  
  17. import org.apache.http.util.EntityUtils;  
  18. import org.springframework.stereotype.Controller;  
  19. import org.springframework.ui.Model;  
  20. import org.springframework.web.bind.annotation.RequestBody;  
  21. import org.springframework.web.bind.annotation.RequestMapping;  
  22. import org.springframework.web.bind.annotation.RequestMethod;  
  23.   
  24. import com.google.gson.Gson;  
  25. import com.google.gson.JsonObject;  
  26. import com.tujia.corelogin.security.SecurityUtil;  
  27. import com.tujia.tucommon.config.ResultCode;  
  28. import com.tujia.tucommon.controller.BaseController;  
  29. import com.tujia.tuuser.entity.User;  
  30.   
  31. @Controller  
  32. public class UserLoginController extends BaseController {  
  33.   
  34.     @Resource  
  35.     private SecurityUtil securityUtil;  
  36.       
  37.       
  38.     @RequestMapping(value = "/tulogin", produces = "text/html")  
  39.     public String listByUser(Model model,HttpServletRequest request, HttpServletResponse response) {  
  40.           
  41.         securityUtil.requrl(request,response);  
  42.         return "tulogin";  
  43.     }  
  44.       
  45.     //没有权限访问此请求  
  46.     @RequestMapping(value = "/noauthority", produces = "text/html")  
  47.     public String noAuthority(Model model,HttpServletRequest request, HttpServletResponse response) {  
  48.           
  49.         return "noAuthority";  
  50.     }  
  51.       
  52.     /** 
  53.      * 退出接口 
  54.      */  
  55.     @RequestMapping(value = "/tulogout",method =RequestMethod.POST)  
  56.     public void logout(  
  57.             HttpServletRequest request, HttpServletResponse response) {  
  58.         ResultCode resultCode = ResultCode.SUCCEED;  
  59.         securityUtil.logout(request, response);  
  60.         Object result = null;  
  61.         setResponse(response, resultCode, result);  
  62.     }  
  63.       
  64.     @RequestMapping(value = "/tulogin")  
  65.     public void login(HttpServletRequest request,  
  66.             HttpServletResponse response,@RequestBody Map<String, Object> map){  
  67.           
  68.         Object resultCode = ResultCode.SUCCEED;  
  69.         Object result = null;  
  70.         securityUtil.requrl(request,response);  
  71.         String phone = (String) map.get("phone");  
  72.         String password = (String) map.get("password");  
  73.         String requrl = request.getRequestURI();  
  74.           
  75.           
  76.         DefaultHttpClient httpClient = new DefaultHttpClient();  
  77.         HttpPost method = new HttpPost("http://localhost:8080/tu-login/tuloginMethod");  
  78.   
  79.         JSONObject jsonParam = new JSONObject();  
  80.         jsonParam.put("phone", phone);  
  81.         jsonParam.put("password", password);  
  82.         jsonParam.put("requrl", requrl);  
  83.   
  84.         StringEntity entity = new StringEntity(jsonParam.toString(), "utf-8");  
  85.         entity.setContentEncoding("UTF-8");  
  86.         entity.setContentType("application/json");  
  87.         method.setEntity(entity);  
  88.           
  89.         HttpResponse resGet;  
  90.         try {  
  91.             //通过DefaultHttpClient 来获得返回的参数(值)  
  92.             resGet = httpClient.execute(method);  
  93.             //将返回值设置编码格式,(避免乱码)  
  94.             String resData = EntityUtils.toString(resGet.getEntity(),"utf-8");  
  95.             //通过net.sf.json.JSONObject 来解析字符串  
  96.             JSONObject resJSON = JSONObject.fromObject(resData);  
  97.             //把json中的user对象获取,并强转。  
  98.             Object userjson = resJSON.get("user");  
  99.             String userString = userjson.toString();  
  100.               
  101.             //通过com.google.gson.Gson 来处理json 类型的user对象。  
  102.             Gson gson = new Gson();  
  103.               
  104.             //user就是转换后的对象。  
  105.   
  106.             //在这里,我对com.google.gson.JsonObject有点疑问,为啥这个不能解析返回的字符串呢?  
  107.             //这个类有什么作用?  
  108.             JsonObject jsonObj = new JsonObject();  
  109.             jsonObj.getAsJsonObject(resData);  
  110.           
  111.             if("A00000".equals(resJSON.get("code"))){  
  112.                 User user = gson.fromJson((String) userString, User.class);  
  113.                 String token = (String)resJSON.get("token");  
  114.                 securityUtil.addCookieToken(request,response,token,user.getId());  
  115.                 request.getSession().setAttribute("user", user);  
  116.             }  
  117.               
  118.               
  119.             if ( !"A00000".equals(resJSON.get("code"))) {  
  120.                 result = resJSON.get("data");  
  121.             }else{  
  122.             result = securityUtil.getReqURL(request,response);  
  123.             }  
  124.               
  125.               
  126.             resultCode = resJSON.get("code");  
  127.         } catch (ClassCastException e) {  
  128.             logger.error(e.getMessage());  
  129.             resultCode = ResultCode.INTERNAL_ERROR;  
  130.             result = e.getMessage();  
  131.         }catch (ClientProtocolException e) {  
  132.             e.printStackTrace();  
  133.         } catch (IOException e) {  
  134.             e.printStackTrace();  
  135.         }  
  136.         setResponse(response, resultCode, result);  
  137.     }  
  138.       
  139. }  

在这里面,比较难的就是,与服务器交互的这一段代码,反正我以前是没听过没用过。其他的地方,没有什么难的了。再就是其他的一些具体的需求了。
原创粉丝点击