惯例:
我是温浩然:
单点登录的客户端配置,客户端不直接操作数据库,而是与其他项目相结合,对浏览器进行操作。
下面贴登录中,客户端的代码。
- 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;
- @Controller
- public class UserLoginController extends BaseController {
- @Resource
- private 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);
- }
- }
在这里面,比较难的就是,与服务器交互的这一段代码,反正我以前是没听过没用过。其他的地方,没有什么难的了。再就是其他的一些具体的需求了。