SSO单点登录

一、什么是SSO

单点登录( Single Sign-On , 简称 SSO )是目前比较流行的服务于企业业务整合的解决方案之一, SSO 使得在多个应用系统中,用户只需要 登录一次 就可以访问所有相互信任的应用系统。

1. SSO 原理

1.1. SSO 体系中的角色
一般 SSO 体系主要角色有三种:
1、 User (多个)
2、 Web 应用(多个)
3、 SSO 认证中心( 1 个 )
1.2. SSO 实现模式的原则
SSO 实现模式一般包括以下三个原则:
1、 所有的认证登录都在 SSO 认证中心进行;
2、 SSO 认证中心通过一些方法?来告诉 Web 应用当前访问用户究竟是不是已通过认证的用户;
3、 SSO 认证中心和所有的 Web 应用建立一种信任关系,也就是说 web 应用必须信任认证中心。(单点信任)

2、cookie方式实现

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.application.yml 配置信息

	server:
	  port: 8080
	 # context-path: /c1
	spring:
	  mvc:
	    view: 
	      prefix: /WEB-INF/jsp/
	      suffix: .jsp

3.核心代码

@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;
  }
  
} 

二、redis-session的单点登录实现(最新式)

1.Maven 依赖 

   			<!-- redis 缓存 -->
   	<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 (注意别忘记开启redis-(没密码开启))

@SpringBootApplication
@EnableRedisHttpSession
public class Client_2 {

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

3.附录:若想加上redis密码请参考

server:
  port: 8080
 # context-path: /c1
spring:
  mvc:
    view: 
      prefix: /WEB-INF/jsp/
      suffix: .jsp
  #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 
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值