单点登录介绍

1. 单点登录介绍(sso)

单点登录SSO(Single Sign On)说得简单点就是在一个多系统共存的环境下,用户在一处登录后,就不用在其他系统中登录,也就是用户的一次登录能得到其他所有系统的信任。单点登录在大型网站里使用得非常频繁,例如像阿里巴巴这样的网站,在网站的背后是成百上千的子系统,用户一次操作或交易可能涉及到几十个子系统的协作,如果每个子系统都需要用户认证,不仅用户会疯掉,各子系统也会为这种重复认证授权的逻辑搞疯掉。实现单点登录说到底就是要解决如何产生和存储那个信任。

2. Cookie方式实现

Cookie 的数据是可以共享的,但要满足条件:

  1. 子path可以访问父path的数据,端口一样
    localhost:8080/crm
    localhost:8080/crm/ehr

  2. 设置根Path,端口可以不一样

    localhost:8080/
    localhost:8090/

  3. 父子域
    如京东购物和京东超市

2.1.maven jar包依赖

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.10.RELEASE</version>
	</parent>

<!-- Add typical dependencies for a web application -->
<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>

	<!--必须有才能编译jsp -->
	<dependency>
		<groupId>org.apache.tomcat.embed</groupId>
		<artifactId>tomcat-embed-jasper</artifactId>
		<scope>provided</scope>
	</dependency>

	<!-- JSTL标签类 -->
	<dependency>
		<groupId>jstl</groupId>
		<artifactId>jstl</artifactId>
		<version>1.2</version>
	</dependency>
	<!-- 热部署 -->
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-devtools</artifactId>
		<optional>true</optional>
	</dependency>
	
	<!--测试 -->
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
		<scope>test</scope>
	</dependency>
</dependencies>

2.2.application.yml 配置信息

构建两个项目:crm 、ehr

eh项目r:
	server:
	  port: 8080
	 # context-path: /crm
	spring:
	  mvc:
	    view: 
	      prefix: /WEB-INF/jsp/
	      suffix: .jsp

crm项目:
	server:
	  port: 8090
	 # context-path: /ehr
	spring:
	  mvc:
	    view: 
	      prefix: /WEB-INF/jsp/
	      suffix: .jsp

2.3核心代码

Ehr:核心代码

@RequestMapping("/index")
@Controller
public class IndexController {
	@RequestMapping("/index")
	public String index(HttpServletResponse response) {
		return "index";
	}

@RequestMapping("/login")
public String login(String name,String pwd,HttpServletResponse response) {
	String page="index";
	if("admin".equals(name)&& "123456".equals(pwd)){
		Cookie cookie=new Cookie("login", "yes");
		cookie.setPath("/");
		response.addCookie(cookie);
		page="success";
	}
	
	return page;
}

Crm:核心代码

@RequestMapping("/")
	public String login(HttpServletRequest request) {
		String page = "login";
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (Cookie cookie : cookies) {
				if ("login".equals(cookie.getName())) {
					if ("yes".equals(cookie.getValue())) {
						page = "success";
					}
				}
			}
		}

		return page;
	}

2.4.页面代码

登录页面代码:

<h1>c1登录</h1>
<form action="/index/login" method="post">
用户名:<input name="name" /><br/>
密码:<input name="pwd"  /><br/>
<input type="submit"  value="登录"/>
</form>

3 redis-session的单点登录实现

1. Maven 依赖

	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-redis</artifactId>
		<version>1.4.1.RELEASE</version>
	</dependency>
	<!-- spring-session 共享 -->
	<dependency>
		<groupId>org.springframework.session</groupId>
		<artifactId>spring-session-data-redis</artifactId>
	</dependency>

2. 在启动服务类加上@EnableRedisHttpSession

@SpringBootApplication
@EnableRedisHttpSession
public class Client_2 {
	public static void main(String[] args) {
		SpringApplication.run(Client_2.class, args);
	}
}

3. 核心代码

@RequestMapping("/login")
	public String login(String name,String pwd,HttpServletResponse response, HttpServletRequest request ) {
		String page="login";
		if("admin".equals(name)&& "123456".equals(pwd)){
			request.getSession().setAttribute("user", "yes");
			page="success";
		}
		return page;
	}

4.若是redis有密码,请配置

spring:
		#redis配置
	  redis:
	  #  数据库索引
	      database: 0
	  #    服务器地址
	      host: 127.0.0.1
	  #    服务器连接端口
	      port: 6379
	  #    链接密码
	      password:
	  #    链接池
	      pool:
	  #    最大连接数(负值表示没有限制)
	        max-active: 8
	  #      最大阻塞等待时间(负值表示没有限制)
	        max-wait: 1
	  #      最大空闲链接
	        max-idle: 8
	  #      最小空闲链接
	        min-idle: 0
	  #    链接超时时间(毫秒)
	        timeout: 5000 

4 使用token实现单点方式

这种方式的获取比较复杂,上述两种方式比较简单,一般小公司用上面的两种方式即可
请参考企业微信的accesstoken获取的方式

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值