SpringCloud简单的单点登录

单点登录

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

个人理解

SpringCloud简单的单点登录_第1张图片

代码实现(案例)

1.导入redis依赖包

     redis.clients
     jedis
     2.9.0

2.RedisUtil工具类
/**
 * 1:连接池的封装
 * 

* 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(略)

SpringCloud简单的单点登录_第2张图片

4.创建sso_login9001模块

pom.xml


            com.lining.sso
            sso_base_util
            1.0-SNAPSHOT
        
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-test
        

        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        

启动类

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

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

你可能感兴趣的:(SpringCloud简单的单点登录)