zoukankan      html  css  js  c++  java
  • 《SSO CAS单点系列》之 APP原生应用如何访问CAS认证中心

    4.开发支持APP登录的移动服务端接口。接收APP登录请求,采用HttpClient转发至CAS认证中心登录,返回json数据解析并最终返回给客户端。本地会话采用redis维护,登录成功,返回access_token。

    接口定义:url: /login.json
    入参: username string
    password string
    出参: ret string
    msg string
    access_token string

    核心代码如下:


    作者:手插口袋_
    链接:https://www.imooc.com/article/4200
    来源:慕课网

    https://www.imooc.com/article/4200

     1 @RequestMapping("/login.json")
     2 public @ResponseBody ResultBean login(HttpServletRequest request, 
     3                      HttpServletResponse response) {
     4 
     5  ResultBean resultBean = new ResultBean();
     6  String username = request.getParameter("username");
     7  String password = request.getParameter("password");
     8 
     9  HttpClient httpClient = new DefaultHttpClient();
    10 
    11  String url = SSO_SERVER_URL + "?mode=app&service=" + SSO_CLIENT_SERVICE;
    12 
    13  HttpGet httpGet = new HttpGet(url); 
    14  try{
    15   HttpResponse httpClientResponse = httpClient.execute(httpGet);
    16   int statusCode = httpClientResponse.getStatusLine().getStatusCode();
    17   if (statusCode == HttpStatus.SC_OK){
    18    String result = EntityUtils.toString(httpClientResponse.getEntity(),
    19               "utf-8").replace('
    20 ', ' ').replace('
    21 ', ' ').trim();
    22    //解析json数据
    23    ObjectMapper objectMapper = new ObjectMapper();
    24    LtBean ltBean = objectMapper.readValue(result, LtBean.class);
    25    List<NameValuePair> formparams = new ArrayList<NameValuePair>();
    26    formparams.add(new BasicNameValuePair("username", username));
    27    formparams.add(new BasicNameValuePair("password", password));
    28    formparams.add(new BasicNameValuePair("lt", ltBean.getLt()));
    29    formparams.add(new BasicNameValuePair("execution", ltBean.getExecution()));
    30    formparams.add(new BasicNameValuePair("_eventId", "submit"));
    31 
    32    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
    33    HttpPost httpPost = new HttpPost(SSO_SERVER_URL);
    34    httpPost.setEntity(entity);
    35 
    36    httpClientResponse = httpClient.execute(httpPost);
    37    statusCode = httpClientResponse.getStatusLine().getStatusCode();
    38 
    39    if (statusCode == HttpStatus.SC_OK){
    40      result = EntityUtils.toString(httpClientResponse.getEntity(), "utf-8")
    41                 .replace('
    42 ', ' ').replace('
    43 ', ' ').trim();
    44 
    45      objectMapper = new ObjectMapper();
    46      resultBean = objectMapper.readValue(result, ResultBean.class);
    47      if(resultBean.getRet().equals("")){
    48       String access_token = UUID.randomUUID().toString(); //会话句柄
    49       TokenUtil.setAccess_token(access_token, username); //放入redis
    50       resultBean.setRet("0");
    51       resultBean.setMsg("登录成功");
    52       resultBean.setAccess_token(access_token);
    53      }
    54     }
    55   }
    56 
    57  }catch(Exception e){
    58   e.printStackTrace();
    59   resultBean.setRet("-2");
    60   resultBean.setMsg("系统服务错误,请稍后再试!");
    61   return resultBean; 
    62  }finally{
    63   httpClient.getConnectionManager().shutdown();
    64  }
    65   return resultBean;  
    66 }
    67 
    68 作者:手插口袋_
    69 链接:https://www.imooc.com/article/4200
    70 来源:慕课网
  • 相关阅读:
    Mysql数据库中的EXISTS和NOT EXISTS
    (4)事件处理——(2)在页面加载的时候执行任务(Performing tasks on page load)
    SICP 习题 (1.13) 解题总结
    [Usaco2009 Feb]Revamping Trails 堆优化 Dijkstra
    (4)事件处理——(3)代码执行的顺序(Timing of code execution)
    【Android】Handler的应用(一):从服务器端加载JSON数据
    HDU 4498 Function Curve (分段, simpson)
    关于 android Intent 传对象和对象数组的一些操作
    mahout算法源码分析之Collaborative Filtering with ALS-WR 并行思路
    javaScript数据类型与typeof操作符
  • 原文地址:https://www.cnblogs.com/sucia-panda/p/9834927.html
Copyright © 2011-2022 走看看