栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

简单聊一聊单点登录

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

简单聊一聊单点登录

单点登录
  • 一、单点登录
  • 二、演示步骤
    • ①简单结构(域名的设置)
    • ②创建项目
      • 1.sso-server 模块代码
        • LoginController代码如下
        • GulimallTestSsoServerApplication启动类代码
        • login.html代码
        • application.properties配置文件
        • pom.xml文件
      • 2.sso-client1模块代码
        • HelloController代码
        • GulimallTestSsoClient1Application
        • list.html代码
        • application.properties
        • pom.xml文件
      • 3.sso-client2模块
        • HelloController代码
        • GulimallTestSsoClient2Application代码
        • list.html代码
        • application.properties配置文件
        • pom.xml文件
  • 三、演示流程

一、单点登录

  单点登录(SingleSignOn,SSO),就是通过用户的一次性鉴别登录。当用户在身份认证服务器上登录一次以后,就可以获得访问单点登录系统中其他关联系统和应用软件的权限,同时这种实现是不需要管理员对用户的登录状态或其他信息进行修改的,这意味着在多个应用系统中,用户只需一次登录就可以访问所有相互信任的应用系统。这种方式减少了由登录产生的时间消耗,辅助了用户管理,是比较流行的。总结就是一处登录,处处可用。

二、演示步骤 ①简单结构(域名的设置)

 Windows里面修改域名的地方:C:WindowsSystem32driversetc下面的hosts文件里面,打开hosts文件,在最后加上以下的信息即可。

127.0.0.1  sso.com 
127.0.0.1  client1.com
127.0.0.1  client2.com


joke:双11的时候,害怕女朋友剁手,把 www.taobao域名映射到本机,一直访问就访问不到。
  核心:三个系统即使域名不一样,想办法给三个系统同步同一个用户的数据;
  1)中央认证服务器:sso.com;
  2)其他系统,想要登录去sso.com登录,登录成功跳转回来;
  3)只要有一个登录,其他都不用登录;
  4)全系统统一一个sso-sessionid;所有系统可能域名都不相同;

②创建项目

  1)利用Spring的初始化向导,创建单点登录服务器,将web、thymeleaf和lombok选中即可。以此创建三个模块。

1.sso-server 模块代码

LoginController代码如下
package com.atguigu.gulimall.ssoserver.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

@Controller
public class LoginController {

    @Autowired
    StringRedisTemplate redisTemplate;
    @ResponseBody
    @GetMapping("/userInfo")
    public String userInfo(@RequestParam("token") String token){
        String s = redisTemplate.opsForValue().get(token);
        return s;
    }
    @GetMapping("/login.html")
    public String loginPage(@RequestParam("redirect_url") String url, Model model,
                            @CookieValue(value = "sso_token",required = false) String sso_token){
        if (!ObjectUtils.isEmpty(sso_token)){
            //说明之前有人登录过,浏览器留下了痕迹
            return "redirect:"+url+"?token="+sso_token;
        }
        model.addAttribute("url",url);
        return "login";
    }
    @PostMapping("/doLogin")
    public String doLogin(@RequestParam("username") String username,
                          @RequestParam("password") String password,
                          @RequestParam("url") String url,
                          HttpServletResponse response){
        if(!ObjectUtils.isEmpty(username) && !ObjectUtils.isEmpty(password)){
            //登录成功,跳回之前页面
            //把登录成功的用户存起来
            String uuid= UUID.randomUUID().toString().replace("-","");
            redisTemplate.opsForValue().set(uuid,username);
            Cookie sso_token = new Cookie("sso_token",uuid);
            response.addCookie(sso_token);
            return "redirect:"+url+"?token="+uuid;
        }
        //登录失败,展示登录页
        return "login";
    }
}

GulimallTestSsoServerApplication启动类代码
package com.atguigu.gulimall.ssoserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class GulimallTestSsoServerApplication {

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

}

login.html代码



    
    登录页


  
用户名:
密码:
application.properties配置文件
server.port=8080

spring.redis.host=127.0.0.1

pom.xml文件


    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.6.5
         
    
    com.atguigu
    gulimall-test-sso-server
    0.0.1-SNAPSHOT
    gulimall-test-sso-server
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
        
            org.springframework.boot
            spring-boot-starter-data-redis
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            org.projectlombok
                            lombok
                        
                    
                
            
        
    



2.sso-client1模块代码

HelloController代码
package com.atguigu.gulimall.ssoclient1.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class HelloController {
    @Value("${sso.server.url}")
    String ssoServerUrl;
    
    @ResponseBody
    @GetMapping("/hello")
    public String hello() {
        return "hello world";
    }

    
    @GetMapping("/employees")
    public String employees(Model model, HttpSession session,
                            @RequestParam(value = "token",required = false) String token){
        if(!ObjectUtils.isEmpty(token)){
            //去sso登录成功跳回来就会带上
            //TODO 1.去sso获取当前token真正对应的用户信息
            RestTemplate restTemplate = new RestTemplate();
            ResponseEntity forEntity = restTemplate.getForEntity("http://sso.com:8080/userInfo?token=" + token, String.class);
            String body = forEntity.getBody();
            session.setAttribute("loginUser",body);
        }
        Object loginUser = session.getAttribute("loginUser");
        if (loginUser==null) {
            //没有登录,就跳转到登录服务器进行登录
            //跳转过去以后,使用url上的查询参数表示我们自己是哪一个页面
            //redirect_url=http://client1.com:8081/employees
            return "redirect:"+ssoServerUrl+"?redirect_url=http://client1.com:8081/employees";
        }else{
            List emps = new ArrayList<>();
            emps.add("张三");
            emps.add("李四");
            model.addAttribute("emps",emps);
            return "list";
        }
    }
}

GulimallTestSsoClient1Application
package com.atguigu.gulimall.ssoclient1;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class GulimallTestSsoClient1Application {

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

}

list.html代码



    
    员工列表


    

欢迎:[[${session.loginUser}]]

  • 姓名:[[${emp}]]
application.properties
server.port=8081

sso.server.url=http://sso.com:8080/login.html

pom.xml文件


    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.6.5
         
    
    com.atguigu
    gulimall-test-sso-client1
    0.0.1-SNAPSHOT
    gulimall-test-sso-client1
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            org.projectlombok
                            lombok
                        
                    
                
            
        
    



3.sso-client2模块

HelloController代码
package com.atguigu.gulimall.ssoclient2.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class HelloController {
    @Value("${sso.server.url}")
    String ssoServerUrl;
    
    @ResponseBody
    @GetMapping("/hello")
    public String hello() {
        return "hello";
    }

    
    @GetMapping("/boss")
    public String employees(Model model, HttpSession session,
                            @RequestParam(value = "token",required = false) String token){
        if(!ObjectUtils.isEmpty(token)){
            //去sso登录成功跳回来就会带上
            //TODO 1.去sso获取当前token真正对应的用户信息
            RestTemplate restTemplate = new RestTemplate();
            ResponseEntity forEntity = restTemplate.getForEntity("http://sso.com:8080/userInfo?token=" + token, String.class);
            String body = forEntity.getBody();
            session.setAttribute("loginUser",body);
        }
        Object loginUser = session.getAttribute("loginUser");
        if (loginUser==null) {
            //没有登录,就跳转到登录服务器进行登录
            //跳转过去以后,使用url上的查询参数表示我们自己是哪一个页面
            //redirect_url=http://client1.com:8081/employees
            return "redirect:"+ssoServerUrl+"?redirect_url=http://client2.com:8082/boss";
        }else{
            List emps = new ArrayList<>();
            emps.add("张三");
            emps.add("李四");
            model.addAttribute("emps",emps);
            return "list";
        }
    }
}

GulimallTestSsoClient2Application代码
package com.atguigu.gulimall.ssoclient2;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class GulimallTestSsoClient2Application {

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

}

list.html代码



    
    员工列表


    

欢迎:[[${session.loginUser}]]

  • 姓名:[[${emp}]]
application.properties配置文件
server.port=8082

sso.server.url=http://sso.com:8080/login.html

pom.xml文件


    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.6.5
         
    
    com.atguigu
    gulimall-test-sso-client2
    0.0.1-SNAPSHOT
    gulimall-test-sso-client2
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            org.projectlombok
                            lombok
                        
                    
                
            
        
    



三、演示流程

测试视频

单点登录

视频地址:https://www.bilibili.com/video/BV1Wa411Y7cr/

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/837592.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号