SpringCloud简单的单点登录

单点登录

单点登录(Single Sign On),简称为 SSO,是目前比较流行的企业业务整合的解决方案之一。SSO的定义是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统。
类似于京东,淘宝等…

个人理解

在这里插入图片描述

代码实现(案例)
1.导入redis依赖包
<dependency>
     <groupId>redis.clients</groupId>
     <artifactId>jedis</artifactId>
     <version>2.9.0</version>
</dependency>
2.RedisUtil工具类
/**
 * 1:连接池的封装
 * <p>
 * 2:api封装
 */
public class RedisUtil {
    static JedisPool jedisPool = null;

    static {
        GenericObjectPoolConfig poolConfig = new JedisPoolConfig();
        //参数设置:
        poolConfig.setMaxTotal(30);//最大连接数
        poolConfig.setMaxIdle(10);//最大空闲数
        poolConfig.setMaxWaitMillis(3*1000);//超时时间
        poolConfig.setTestOnBorrow(true);//借的时候进行测试

        //你在做的时候,应该丢到properties的配置文件:直接用这个工具类
        String host = "127.0.0.1";
        int port = 6379;
        int timeout = 5 * 1000;
        String password = "123456";
        jedisPool = new JedisPool(poolConfig, host, port, timeout, password);
    }

    /**
     * 设置
     * @param key
     * @param value
     */
    public static void set(String key, String value) {
        // 1:获取jedis实例
        Jedis jedis = jedisPool.getResource();
        try {
            //2:api操作
            jedis.set(key, value);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(jedis!=null) {
                //3:资源换回
                jedis.close();
            }
        }
    }

    /**
     * 获取
     * @param key
     * @return
     */
    public static String get(String key) {
        // 1:获取jedis实例
        Jedis jedis = jedisPool.getResource();
        try {
            //2:api操作
            return jedis.get(key);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            if(jedis!=null) {
                //3:资源换回
                jedis.close();
            }
        }
    }
}
3.创建注册中心Eureke(略)

在这里插入图片描述

4.创建sso_login9001模块

pom.xml

<dependency>
            <groupId>com.lining.sso</groupId>
            <artifactId>sso_base_util</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!--springboot支持-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>

        <!--eureka客户端支持 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

启动类

@SpringBootApplication
@EnableEurekaClient //表示是eureka的客户端
public class UserProviderApplication_8001 {
public static void main(String[] args) {
        SpringApplication.run(UserProviderApplication_8001.class);
    }
}

controller

@Controller
public class LoginController {
    
    @RequestMapping("/login")
    public void login(HttpServletRequest request, HttpServletResponse response){
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        System.out.println(name+":"+password);
        String uuid = UUID.randomUUID().toString().substring(10);
        RedisUtil.set("uuid",name+":"+password);
        Cookie[] cookies = request.getCookies();
        Cookie cookie2=new Cookie("uuid",uuid);
        response.addCookie(cookie2);
    }
}
5.创建sso_login9002模块

controller

@Controller
public class LoginController {

    @RequestMapping("/login")
    public void login(HttpServletRequest request, HttpServletResponse response){
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        System.out.println(name+":"+password);
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie:cookies) {
            if(cookie.getName().equals("uuid")){
                System.out.println(cookie.getName());
                String s = RedisUtil.get("uuid");
                String[] split = StringUtils.split(s, ":");
                System.out.println(split[0]);
                System.out.println(split[1]);
            }
        }
    }

以上纯粹个人见解,若果我有什么错误?欢迎指正

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值