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 来源:慕课网
  • 相关阅读:
    python
    python
    gitlab
    nodejs
    java
    ElasticSearch 安装与配置 (windows)
    shell脚本批量注释
    C获取系统中CPU核数
    linux内核内存管理
    perf: interrupt took too long (3136 > 3126), lowering kernel.perf_event_max_sample_rate to 63000
  • 原文地址:https://www.cnblogs.com/sucia-panda/p/9834927.html
Copyright © 2011-2022 走看看