一、常见问题

序号 问题描述 地址
1.1 前后端分离项目PC端如何实现免登流程 点击查看
1.2 springmvc项目如何实现免登流程 点击查看
1.3 springboot项目如何实现免登 点击查看
1.4 实际的案例 点击查看

1.1 前后端分离项目PC端如何实现免登流程

1.1.1 免登的流程

通过免登服务,在微应用的PC中,无需用户再次输入用户名和密码,可以通过身份认证获取当前使用政企通的用户身份及相关信息。

1.1.2 具体的操作步骤

新框架采用前后端分离技术,大致的对接流程如下图所示:

1.1.2.1 企业端
  • Step1. 用户输入用户名和密码登录平台;
  • Step2. 登录成功,平台会向公共的域写入加密后的用户的code(由平台完成无需自己实现)(注意:code-key生产环境comEncodeProd、 测试环境comEncodeTest、开发环境:comEncodeDev);

    handleIframe = () => {
      const {
        location,
        dispatch
      } = this.props;
        const govOrCom = localStorage.getItem('govOrCom') || 'com';
        // 默认使用新免登
        const iFrame = document.getElementById('crossIframe');
    
        // 获取 政府端
        window.onload = function() {
          if (govOrCom === 'gov') {
            iFrame.contentWindow.postMessage(JSON.stringify({
              type: 'GET',
              key: constant.govEncode, //注意:code-key生产环境comEncodeProd、测试环境comEncodeTest、开发环境:comEncodeDev
            }), 'https://cross.zqtong.com');
          } else {
            // 获取 企业端端
            iFrame.contentWindow.postMessage(JSON.stringify({
              type: 'GET',
              key: constant.comEncode, //注意:code-key生产环境govEncodeProd、测试环境govEncodeTest、开发环境:govEncodeDev
            }), 'https://cross.zqtong.com');
          }
        }
    
        window.receiveMessageFromIndex = (e) => {
    
          // 可能存在浏览器插件会发message,不是免登的消息不做操作
          if (e && e.origin !== 'https://cross.zqtong.com') {
            return;
          }
    
          if (e !== undefined && e.data && e.data !== '') {
            dispatch({
              type: 'user/getDecUserinfos', //这边是调用客户端后台解密的接口,需要自己参考文档实现
              payload: {
                code: e.data,
              }
            });
          } 
        };
    
        window.addEventListener('message', window.receiveMessageFromIndex, false);
    };
    
  • Step3. 在客户端服务端里面,根据分配的clientID和clientSecret获取政府端或企业端token;
  • Step4. 在客户端微应用里面,会从公共的域读出加密后的code并解密获取userid和baseid解密接口(可以参考Ecoder中的项目模板实现);
  • Step5. 根据userid和baseid和token获取当前登录用户的身份信息;
1.1.2.2 政府端
  • Step1. 用户输入用户名和密码登录平台;
  • Step2. 登录成功,平台会向公共的域写入加密后的用户的code(由平台完成无需自己实现)(注意:code-key生产环境govEncodeProd、 测试环境govEncodeTest、开发环境:govEncodeDev);

    handleIframe = () => {
      const {
        location,
        dispatch
      } = this.props;
        const govOrCom = localStorage.getItem('govOrCom') || 'gov';
        // 默认使用新免登
        const iFrame = document.getElementById('crossIframe');
    
        // 获取 政府端
        window.onload = function() {
          if (govOrCom === 'gov') {
            iFrame.contentWindow.postMessage(JSON.stringify({
              type: 'GET',
              key: constant.govEncode, //注意:code-key生产环境comEncodeProd、测试环境comEncodeTest、开发环境:comEncodeDev
            }), 'https://cross.zqtong.com');
          } else {
            // 获取 企业端端
            iFrame.contentWindow.postMessage(JSON.stringify({
              type: 'GET',
              key: constant.comEncode, //注意:code-key生产环境govEncodeProd、测试环境govEncodeTest、开发环境:govEncodeDev
            }), 'https://cross.zqtong.com');
          }
        }
    
        window.receiveMessageFromIndex = (e) => {
    
          // 可能存在浏览器插件会发message,不是免登的消息不做操作
          if (e && e.origin !== 'https://cross.zqtong.com') {
            return;
          }
    
          if (e !== undefined && e.data && e.data !== '') {
            dispatch({
              type: 'user/getDecUserinfos', //这边是调用客户端后台解密的接口,需要自己参考文档实现
              payload: {
                code: e.data,
              }
            });
          } 
        };
    
        window.addEventListener('message', window.receiveMessageFromIndex, false);
    };
    
  • Step3. 在客户端服务端里面,根据分配的clientID和clientSecret获取政府端或企业端token;
  • Step4. 在客户端微应用里面,会从公共的域读出加密后的code并解密获取userid解密接口(可以参考Ecoder中的项目模板实现);
  • Step5. 根据userid和token获取当前登录用户的身份信息;

1.2 springmvc项目如何实现免登流程

1.2.1 添加pom.xml

<dependency>
    <groupId>org.springframework.security.oauth</groupId>
    <artifactId>spring-security-oauth2</artifactId>
    <version>2.0.13.RELEASE</version>
   <exclusions>
        <exclusion>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
        </exclusion>
    </exclusions>
</dependency>

1.2.2 修改 applicationContext-security.xml

line25
 <custom-filter ref="oauthCLient" before="FIRST"/>
 <custom-filter ref="oauthssoFilter" before="BASIC_AUTH_FILTER"/>
    <!-- OAuth 2.0 配置开始 -->
    <oauth:client id="oauthCLient"/>

    <!-- client-id 和 client-secret 需要向xiezhi@eazytec.com申请 -->
    <oauth:resource id="procredit" client-id="XXXXXXXXXX"
                    client-secret="XXXXXXXX" scope="read"
                    type="authorization_code" user-authorization-uri="https://authorize.eazytec-cloud.com/oauth/authorize"
                    access-token-uri="https://authorize.eazytec-cloud.com/oauth/token"/>

    <oauth:rest-template id="procreditRestTemplate" resource="procredit"/>

    <beans:bean id="tokenServices"
                class="org.springframework.security.oauth2.provider.token.RemoteTokenServices">
        <beans:property name="restTemplate" ref="procreditRestTemplate"/>
        <beans:property name="clientId" value="XXXXXXX"/>
        <beans:property name="clientSecret" value="XXXXXX"/>
        <beans:property name="checkTokenEndpointUrl" value="https://authorize.eazytec-cloud.com/oauth/check_token"/>
    </beans:bean>

     <beans:bean id="oauthssoFilter"
                    class="com.eazytec.webapp.filter.OAuth2BPMClientAuthenticationProcessingFilter">
            <beans:constructor-arg name="defaultFilterProcessesUrl"
                                   value="/login/oauth2"/>
            <beans:property name="restTemplate" ref="procreditRestTemplate"/>
            <beans:property name="tokenServices" ref="tokenServices"/>
            <beans:property name="authenticationManager" ref="authenticationManager"/>
        </beans:bean>


    <!-- Oauth2配置结束 -->

1.2.3. 修改 urlrewrite.xml

 <rule>
    <from>/login/oauth2</from>
    <to last="true">/login/oauth2</to>
 </rule>

1.2.4. 如果使用孙峰的框架构建,则需要在web.xml里面加下如下

<listener>
        <listener-class>
            org.springframework.web.context.request.RequestContextListener
        </listener-class>
    </listener>

1.2.5. 代码参考

代码点击查看

1.3 springboot项目如何实现免登

待写

1.4 实际的案例

1.4.1 我们需要支持的2类业务场景

  • 免登过程
  • 平台与任意应用之间,任意应用之间的互相的跳转

1.4.2 根据新旧系统我们又可以分类

  • 历史已有系统的对接
  • 新开系统的对接
1.4.2.1 历史已有系统的对接

历史存在的系统,存在多语言,多框架,多组织架构等复杂的问题,所以我们采用自己根据Oauth2协议实现登录+用户账号统一映射(区分政府端和企业端)+重定向的方式来解决上述2大类业务场景;

1.4.2.2 新框架系统的对接
江苏卓易信息科技股份有限公司-TNG基础平台组 all right reserved,powered by Gitbook该文件最后修改时间: 2021-01-27 06:23:28

results matching ""

    No results matching ""