多系统多方式单点登入
Posted 知青先生
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多系统多方式单点登入相关的知识,希望对你有一定的参考价值。
目录
SSO单点登录
SOO(Single Sign On) 单点登录
单点流程
cookie是用来在客户端存储数据的工具。
1、在其中一个子系统中登录,转到登录系统,在登录系统中完成登录,完成登录后向发起登录的子系统写入一个cookie
2、其他系统进行登录时,先判断是否存在token信息,存在及返回用户信息,没有统一到登陆地址页面进行登入,其他系统进行刷新即可
3、cookie中的子系统的域名,保持一致,所有的子系统才能访问到这个cookie
实现多系统单点登录
1、以springboot为项目背景
<?xml version="1.0" encoding="UTF-8"?>
<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>org.example</groupId>
<artifactId>sso</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>sso-vip</module>
<module>sso-main</module>
<module>sso-cart</module>
<module>sso-login</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath />
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring.boot.version>2.1.3.RELEASE</spring.boot.version>
<junit.version>4.11</junit.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<!--热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!--测试包-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!--lombok简写-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
<!-- SpringBoot的依赖配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring.boot.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<!--druid 连接池-->
<!-- <dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>-->
<!--spring测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<!--maven打包插件-->
</project>
2、修改host文件,刷新dns
#sso
127.0.0.1 www.codeshop.com
127.0.0.1 vip.codeshop.com
127.0.0.1 cart.codeshop.com
127.0.0.1 login.codeshop.com
3、创建父项目及多个子系统项目
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>sso</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>sso-cart</artifactId>
<packaging>war</packaging>
<name>sso-cart Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
</properties>
<dependencies>
</dependencies>
<build>
</build>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>sso</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>sso-login</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>sso-login Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
</properties>
<dependencies>
<!--父依赖-->
<!--<dependency>
<groupId>org.example</groupId>
<artifactId>sso</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>-->
</dependencies>
<build>
</build>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>sso</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>sso-main</artifactId>
<packaging>war</packaging>
<name>sso-main Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
</properties>
<dependencies>
</dependencies>
<build>
</build>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>sso</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>sso-vip</artifactId>
<packaging>war</packaging>
<name>sso-vip Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
</dependencies>
<build>
</build>
</project>
3.1、启动类(单个)
package com.cn;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
/**
* @创建人 明
*/
@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
3.2、配置(单个)
server:
port: 9003
4、登录系统,负责token生成,写入。子系统登录页面拦截,负责db用户信息校验,登入用户信息退出
package com.cn.hm.controller;
import com.cn.hm.util.LogginCacheUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.thymeleaf.util.StringUtils;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpSession;
/*
*@描述
*@参数 视图控制
*@返回值
*@创建人 HM
*@创建时间
*@修改人和其它信息
*/
@Controller
@RequestMapping("/view")
public class ViewController {
/**
*@描述
*@参数 跳转到登入页面
*@返回值
*@创建人 HM
*@创建时间
*@修改人和其它信息
*/
@GetMapping("/login")
public String toLogin(@RequestParam(required = false,defaultValue = "") String target
, HttpSession session
, @CookieValue(required = false,value = "TOKEN")Cookie cookie){
if(StringUtils.isEmpty(target)){//主页地址
target="http://www.codeshop.com:9002/view/index";
}
//进入登入系统,判断是否存在token
if(cookie!=null && LogginCacheUtil.loginUser.get(cookie.getValue())!=null){
return "redirect:"+target;
}
//地址校验
//存入本次交互会话
session.setAttribute("target",target);
return "login";
}
}
package com.cn.hm.controller;
import com.cn.hm.pojo.User;
import com.cn.hm.util.LogginCacheUtil;
import org.springframework.http.HttpStatus;
import org单点登入系统怎么区分商家和一般会员的