关键词:服务集成,登录认证,SSO,系统集成,单点认证

O2OA提供多种SSO单点认证方式,比如约定密钥,OAuth2,SMAP等等。本文主要演示如何发送用户账号密码进行登录认证


此示例演示如何使用java发送Http请求来登录o2server,获取登录认证xtoken信息

完整代码示例:

使用账号密码发送请求登录


package net.o2oa.demos;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import org.apache.commons.codec.binary.StringUtils;
import org.json.JSONObject;
/**
 * 此示例演示如何使用java发送Http请求来登录o2server,获取登录认证xtoken信息 
 * @author O2OA
 */
public class Demo_LoginToServer {
        
        static final String URL_LOGIN = "/x_organization_assemble_authentication/jaxrs/authentication";
    
        public static void main(String[] args) {
            String applicationServer = "127.0.0.1";
            Integer applicationPort = 20020;
            String userName = "张三";
            String password = "o2";
            try {
                LoginResult result = login(applicationServer, applicationPort, userName, password);
                if (StringUtils.equals("success", result.getType())) {
                    System.out.println("xtoken=" + result.getToken());
                } else {
                    System.out.println("message:" + result.getMessage());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
    //服务地址:http://127.0.0.1:20020/x_organization_assemble_authentication/jaxrs/authentication
    // {"credential":"xadmin","password":"o2"}
    /**
         * 使用登录认证的接口进行服务器登录,获取xtoken信息
         * @param applicationServer  127.0.0.1
         * @param applicationPort     20020
         * @param userName             张三
         * @param password              o2
         * @return
         * @throws Exception
         */
    public static LoginResult login(String applicationServer, Integer applicationPort, String userName, String password) throws Exception {
        //参数
        String loginUrl = "http://" + applicationServer + ":" + applicationPort + URL_LOGIN;
        String loginParams = String.format("{'credential':'%s','password':'%s'}", userName, password);
        String responseData = sendPost(loginUrl, loginParams);
        JSONObject result = new JSONObject(responseData);
        String type = result.getString("type");
        if (StringUtils.equals("success", type)) {
            //登录成功
            JSONObject data = result.getJSONObject("data");
            String token = data.getString("token");
            return new LoginResult("success", token, "登录成功!");
        } else {
            //登录失败
            return new LoginResult("error", null, "用户不存在或者密码错误!");
        }
    }
    /**
        * 发送POST请求     
        *    
        * @param url  地址     
        * @param param   传入的数据   
        * @return
        */
    public static String sendPost(String url, String param) {
            PrintWriter out = null;
            BufferedReader in = null;
            String result = "";
        try {
            URL realUrl = new URL(url);
            URLConnection conn = realUrl.openConnection();
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            conn.setDoOutput(true);
            conn.setDoInput(true);
            out = new PrintWriter(conn.getOutputStream());
            out.print(param);
            out.flush();
            in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return result;
    }
}


LoginResult.java

public static class LoginResult {
    
    private String type;
    private String token;
    private String message;
    
    public LoginResult(String type, String token, String message) {
        super();
        this.type = type;
        this.token = token;
        this.message = message;
    }
    
    public String getType() {
        return type;
    }
    
    public void setType(String type) {
        this.type = type;
    }
    
    public String getToken() {
        return token;
    }
    
    public void setToken(String token) {
        this.token = token;
    }
    
    public String getMessage() {
        return message;
    }
    
    public void setMessage(String message) {
        this.message = message;
    }
}

成功响应结果JSON:

{
  "type": "success",
  "data": {
    "tokenType": "manager",
    "token": "HeEoZIVgPjQAuhSAF5z5qKCO-_-iWowwEIdiuKMfurQ",
    "roleList": [
    "Manager",
    "OrganizationManager",
    "MeetingManager",
    "ProcessPlatformManager"
    ],
    "id": "xadmin",
    "name": "xadmin",
    "employee": "xadmin",
    "distinguishedName": "xadmin@o2oa@P",
    "mail": "xadmin@o2oa.net",
    "weixin": "",
    "qq": "",
    "mobile": ""
  },
  "message": "",
  "date": "2019-10-19 14:15:17",
  "spent": 18,
  "size": -1,
  "count": 0,
  "position": 0
}

失败响应结果JSON:

{
  "readyState": 4,
  "responseText": "{\"type\": \"error\",\"message\": \"用户不存在或者密码错误.\",\"date\": \"2019-10-19 14:34:34\",\"spent\": 9,  \"size\": -1,  \"count\": 0,  \"position\": 0,   \"prompt\":\"com.x.organization.assemble.authentication.jaxrs.authentication.ExceptionPersonNotExistOrInvalidPassword\"  }",
  "responseJSON": {
  "type": "error",
  "message": "用户不存在或者密码错误.",
  "date": "2019-10-19 14:34:34",
  "spent": 9,
  "size": -1,
  "count": 0,
  "position": 0,
  "prompt": "com.x.organization.assemble.authentication.jaxrs.authentication.ExceptionPersonNotExistOrInvalidPassword"
  },
  "status": 500,
  "statusText": "Internal Server Error"
}

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>net.o2oa.demos</groupId>
  <artifactId>test_start_process_demo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>test_start_process_demo</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>        <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
    <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpclient</artifactId>
      <version>4.5.10</version>
    </dependency>        
    <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore-nio -->
    <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpcore-nio</artifactId>
      <version>4.4.12</version>
    </dependency>        
    <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
    <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpcore</artifactId>
      <version>4.4.12</version>
    </dependency>        
    <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpmime -->
    <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpmime</artifactId>
      <version>4.5.10</version>
    </dependency>
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-lang3</artifactId>
      <version>3.9</version>
    </dependency>
    <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>2.8.5</version>
    </dependency>        
    <!-- https://mvnrepository.com/artifact/org.json/json -->
    <dependency>
      <groupId>org.json</groupId>
      <artifactId>json</artifactId>
      <version>20190722</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>


推荐文章:

开发知识-React篇:在O2OA门户页面中使用React
2021-02-07
  O2OA提供了门户平台,使用可视化方式设计页面,用于设计系统主页、列表等系统中的各类页面,在一定程度上降低了开发者的技能要求。但是对于有经验的前端开发人员,
论坛管理
2021-09-23
O2OA平台论坛管理为企业提供了一个员工公开交流的场所,经过配置后,论坛可以拥有丰富的栏目、版块,可以进行版块的权限设置,可见性,发贴回复权限设置。
服务集成-发送用户账号密码进行登录认证
2021-03-02
O2OA提供多种SSO单点认证方式,比如约定密钥,OAuth2,SMAP等等。本文主要演示如何发送用户账号密码进行登录认证。
开发知识-中标麒麟安装达梦数据库(DM8)
2021-02-07
环境说明操作系统版本:NeoKylinLinuxAdvancedServerreleaseV7Update6(Chromium)1.前期准备工作1.1数据库下载
工作管理使用手册
2021-06-22
@企业办公系统@工作管理@开源办公系统@看板管理@任务管理 @工作协同@多人协同O2OA工作管理系统,能够将工作任务全面结构化、过程化,将个人与企业融合起来,加
数据清理-O2OA平台如何清除测试数据
2021-02-07
当O2OA业务开发、测试完成时,上线前通常需要清除已经存在的测试数据,或者演示数据。O2OA提供命令对指定模块的业务数据进行清除。清除测试数据清除已存在的业务数
服务集成-平台数据字典API使用说明
2021-03-15
O2OA允许用户通过脚本灵活地完成数据字典相关的增删查改操作。本文主要介绍完成平台数据字典操作相关的API和使用方式
移动办公-移动端应用权限配置
2021-02-25
O2OA平台拥有配套的原生开发的安卓和IOS移动APP,开发者在拥有公网IP或者域名的服务器上可以轻松体验移动办公环境,并且不会产生任何费用。本篇主要介绍如何配
流程设计-Excel数据导入数据网格(仅支持IE)
2021-05-17
O2OA提供数据网格组件来简体表单中的可编辑表格或者数据展现表格的设计,如费用报销,差旅报销等业务都可能使用到数据网格。本文主要介绍如何将Excel中的数据导入
开发知识-JQuery篇:如何在O2OA中使用Jquery
2021-02-07
O2OA平台默认使用的JavaScript框架是mootools,有些用户对mootools框架不是很熟悉,造成使用上一些学习成本。平台也提供其他js库的引用,

results matching ""

    No results matching ""