分布式单点登录框架XXL-SSO(一)入门

简介

1.概述

XXL-SSO 是一个分布式单点登录框架。只需要登录一次就可以访问所有相互信任的应用系统。 拥有"轻量级、分布式、跨域、Cookie+Token均支持、Web+APP均支持"等特性。现已开放源代码,开箱即用。

2.特性

  • 1、简洁:API直观简洁,可快速上手
  • 2、轻量级:环境依赖小,部署与接入成本较低
  • 3、单点登录:只需要登录一次就可以访问所有相互信任的应用系统
  • 4、分布式:接入SSO认证中心的应用,支持分布式部署
  • 5、HA:Server端与Client端,均支持集群部署,提高系统可用性
  • 6、跨域:支持跨域应用接入SSO认证中心
  • 7、Cookie+Token均支持:支持基于Cookie和基于Token两种接入方式,并均提供Sample项目
  • 8、Web+APP均支持:支持Web和APP接入
  • 9、实时性:系统登陆、注销状态,全部Server与Client端实时共享
  • 10、CS结构:基于CS结构,包括Server"认证中心"与Client"受保护应用"
  • 11、记住密码:未记住密码时,关闭浏览器则登录态失效;记住密码时,支持登录态自动延期,在自定义延期时间的基础上,原则上可以无限延期
  • 12、路径排除:支持自定义多个排除路径,支持Ant表达式,用于排除SSO客户端不需要过滤的路径

3.下载

中文文档

源码仓库地址

源码仓库地址 Release Download
https://github.com/xuxueli/xxl-sso Download
https://gitee.com/xuxueli0323/xxl-sso Download

4.环境

  • JDK:1.7+
  • Redis:4.0+

2.快速入门(基于Cookie)

1.源码编译

- xxl-sso-server:中央认证服务,支持集群
- xxl-sso-core:Client端依赖
- xxl-sso-samples:单点登陆Client端接入示例项目
    - xxl-sso-web-sample-springboot:基于Cookie接入方式,供用户浏览器访问,springboot版本
    - xxl-sso-token-sample-springboot:基于Token接入方式,常用于无法使用Cookie的场景使用,如APP、Cookie被禁用等,springboot版本

2.认证中心(SSO Server)

配置文件位置:application.properties

### web
server.port=8080
server.context-path=/xxl-sso-server

### resources
spring.mvc.static-path-pattern=/static/**
spring.resources.static-locations=classpath:/static/

### freemarker
spring.freemarker.templateLoaderPath=classpath:/templates/
spring.freemarker.suffix=.ftl
spring.freemarker.charset=UTF-8
spring.freemarker.request-context-attribute=request
spring.freemarker.settings.number_format=0.##########

### xxl-sso
### redis 地址: 如 "{ip}"、"{ip}:{port}"、"{redis/rediss}://xxl-sso:{password}@{ip}:{port:6379}/{db}";多地址逗号分隔
xxl.sso.redis.address=redis://127.0.0.1:6379
### 登录态有效期窗口,默认24H,当登录态有效期窗口过半时,自动顺延一个周期
xxl.sso.redis.expire.minite=1440

3.单点登陆Client端接入示例项目

xxl-sso-web-sample-springboot

1.maven依赖

<!-- xxl-sso-core -->
        <dependency>
            <groupId>com.xuxueli</groupId>
            <artifactId>xxl-sso-core</artifactId>
            <version>${parent.version}</version>
        </dependency>

2.配置 XxlSsoFilter

package com.xxl.sso.sample.config;

import com.xxl.sso.core.conf.Conf;
import com.xxl.sso.core.filter.XxlSsoWebFilter;
import com.xxl.sso.core.util.JedisUtil;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author xuxueli 2018-11-15
 */
@Configuration
public class XxlSsoConfig implements DisposableBean {


    @Value("${xxl.sso.server}")
    private String xxlSsoServer;

    @Value("${xxl.sso.logout.path}")
    private String xxlSsoLogoutPath;

    @Value("${xxl-sso.excluded.paths}")
    private String xxlSsoExcludedPaths;

    @Value("${xxl.sso.redis.address}")
    private String xxlSsoRedisAddress;


    @Bean
    public FilterRegistrationBean xxlSsoFilterRegistration() {

        // xxl-sso, redis init
        JedisUtil.init(xxlSsoRedisAddress);

        // xxl-sso, filter init
        FilterRegistrationBean registration = new FilterRegistrationBean();

        registration.setName("XxlSsoWebFilter");
        registration.setOrder(1);
        registration.addUrlPatterns("/*");
        registration.setFilter(new XxlSsoWebFilter());
        registration.addInitParameter(Conf.SSO_SERVER, xxlSsoServer);
        registration.addInitParameter(Conf.SSO_LOGOUT_PATH, xxlSsoLogoutPath);
        registration.addInitParameter(Conf.SSO_EXCLUDED_PATHS, xxlSsoExcludedPaths);

        return registration;
    }

    @Override
    public void destroy() throws Exception {

        // xxl-sso, redis close
        JedisUtil.close();
    }

}

3.配置说明

### web
server.port=8081
server.context-path=/xxl-sso-web-sample-springboot

### freemarker
spring.freemarker.request-context-attribute=request
spring.freemarker.cache=false

### resource (default: /**  + classpath:/static/ )
spring.mvc.static-path-pattern=/static/**
spring.resources.static-locations=classpath:/static/

### xxl-sso
### SSO Server认证中心地址(推荐以域名方式配置认证中心需要修改host文件)
xxl.sso.server=http://xxlssoserver.com:8080/xxl-sso-server
### 注销登陆path,值为Client端应用的相对路径
xxl.sso.logout.path=/logout
### 路径排除Path,允许设置多个,且支持Ant表达式。用于排除SSO客户端不需要过滤的路径
xxl-sso.excluded.paths=
xxl.sso.redis.address=redis://127.0.0.1:6379

4.启动验证

修改Host文件:域名方式访问认证中心,模拟跨域与线上真实环境

### 在host文件中添加以下内容
127.0.0.1 xxlssoserver.com
127.0.0.1 xxlssoclient1.com
127.0.0.1 xxlssoclient2.com

分别运行 “xxl-sso-server” 与 “xxl-sso-web-sample-springboot”

1、SSO认证中心地址:

http://xxlssoserver.com:8080/xxl-sso-server

2、Client01应用地址:

http://xxlssoclient1.com:8081/xxl-sso-web-sample-springboot/

3、Client02应用地址:

http://xxlssoclient2.com:8081/xxl-sso-web-sample-springboot/

初次访问都会重定向到统一认证授权服务,同时客户端会带有重定向回调地址

5.SSO登录/注销流程验证

正常情况下,登录流程如下:

1、访问 "Client01应用地址" ,将会自动 redirect 到 "SSO认证中心地址" 登录界面

2、成功登录后,将会自动 redirect 返回到 "Client01应用地址",并切换为已登录状态

3、此时,访问 "Client02应用地址",不需登陆将会自动切换为已登录状态

此时客户端1和客户端2都会从统一授权认证服务中获取到cookie信息,此信息是回调后url携带过来 如上图

正常情况下,注销流程如下:

1、访问 "Client01应用地址" 配置的 "注销登陆path",将会自动 redirect 到 "SSO认证中心地址" 并自动注销登陆状态

2、此时,访问 "Client02应用地址",也将会自动注销登陆状态

发布了229 篇原创文章 · 获赞 49 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/baiyan3212/article/details/103271696