• SSO 基于token vue + element ui spring boot前端分离


    一、项目启动

    根据sql语句生成server库和客户端库

    1.后端启动

    配置修改:

    1. spring:
    2. datasource:
    3. driver-class-name: com.mysql.jdbc.Driver
    4. username: username
    5. password: password
    6. url: jdbc:mysql://ip:port/spike_sso?autoReconnect=true&allowMultiQueries=true&useUnicode=true&characterEncoding=UTF8&useSSL=false&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai
    7. xxl:
    8. sso:
    9. redis:
    10. address: redis://:password@ip:port/0
    11. expire:
    12. minute: 1440

    server启动运行 SpikeSsoApplication.java 默认端口9090

    client  启动运行 SpikeApplication.java 默认端口9091

    2.前端nginx配置

    1. upstream sso-api{
    2. server 127.0.0.1:6060 weight=1;
    3. }
    4. server {
    5. listen 8086;
    6. location / {
    7. root /usr/share/nginx/spike-sso-ui/dist;
    8. index index.html index.htm;
    9. try_files $uri $uri/ /index.html;
    10. }
    11. location /sso {
    12. proxy_pass http://sso-api;
    13. }
    14. #location /api {
    15. # proxy_pass http://127.0.0.1:9080;
    16. #}
    17. }
    18. server {
    19. listen 8087;
    20. location / {
    21. root /usr/share/nginx/spike-ui/dist;
    22. index index.html index.htm;
    23. try_files $uri $uri/ /index.html;
    24. }
    25. location /api {
    26. proxy_pass http://127.0.0.1:9080;
    27. }
    28. }

    二、项目详解

    1.后端代码

    XxlSsoConfig.java

    1. import com.xxl.sso.core.conf.Conf;
    2. import com.xxl.sso.core.filter.XxlSsoTokenFilter;
    3. import com.xxl.sso.core.util.JedisUtil;
    4. import org.springframework.beans.factory.DisposableBean;
    5. import org.springframework.beans.factory.annotation.Value;
    6. import org.springframework.boot.web.servlet.FilterRegistrationBean;
    7. import org.springframework.context.annotation.Bean;
    8. import org.springframework.context.annotation.Configuration;
    9. /**
    10. * @author xuxueli 2018-11-15
    11. */
    12. @Configuration
    13. public class XxlSsoConfig implements DisposableBean {
    14. @Value("${xxl.sso.server}")
    15. private String xxlSsoServer;
    16. @Value("${xxl.sso.logout.path}")
    17. private String xxlSsoLogoutPath;
    18. @Value("${xxl.sso.redis.address}")
    19. private String xxlSsoRedisAddress;
    20. @Value("${xxl-sso.excluded.paths}")
    21. private String xxlSsoExcludedPaths;
    22. @Bean
    23. public FilterRegistrationBean xxlSsoFilterRegistration() {
    24. // xxl-sso, redis init
    25. JedisUtil.init(xxlSsoRedisAddress);
    26. // xxl-sso, filter init
    27. FilterRegistrationBean registration = new FilterRegistrationBean();
    28. registration.setName("XxlSsoTokenFilter");
    29. registration.setOrder(1);
    30. registration.addUrlPatterns("/*");
    31. registration.setFilter(new XxlSsoTokenFilter());
    32. registration.addInitParameter(Conf.SSO_SERVER, xxlSsoServer);
    33. registration.addInitParameter(Conf.SSO_LOGOUT_PATH, xxlSsoLogoutPath);
    34. registration.addInitParameter(Conf.SSO_EXCLUDED_PATHS, xxlSsoExcludedPaths);
    35. return registration;
    36. }
    37. @Override
    38. public void destroy() throws Exception {
    39. // xxl-sso, redis close
    40. JedisUtil.close();
    41. }
    42. }

    2.前端代码

    axios 拦截器配置

    配置 返回501跳转

    1. import axios from "axios";
    2. import { Message } from 'element-ui';
    3. // 创建 axios 实例
    4. // 添加请求拦截器
    5. axios.interceptors.request.use(function (config) {
    6. // 在发送请求之前做些什么
    7. let token = localStorage.getItem("token");
    8. if (token != null && token != 'null') {
    9. config.headers["xxl_sso_sessionid"] = token;
    10. }
    11. return config;
    12. }, function (error) {
    13. // 对请求错误做些什么
    14. return Promise.reject(error);
    15. });
    16. // 添加响应拦截器
    17. axios.interceptors.response.use(function (response) {
    18. // 2xx 范围内的状态码都会触发该函数。
    19. // 对响应数据做点什么
    20. if (response.data.code == 501) {
    21. window.location.href="/login";
    22. }
    23. return response;
    24. }, function (error) {
    25. let status = error.response.status;
    26. if (403 == status) {
    27. Message.error("未登录");
    28. let returnUrl = localStorage.getItem("returnUrl");
    29. // if (returnUrl != 'null') {
    30. // window.location.href="/login?returnUrl="+returnUrl;
    31. // }else {
    32. window.location.href="/login";
    33. // }
    34. //this.$router.push("/");
    35. }
    36. // 超出 2xx 范围的状态码都会触发该函数。
    37. // 对响应错误做点什么
    38. return Promise.reject(error);
    39. });
    40. export {axios as axios };

    preLogin.vue处理跳转

    1. let returnUrl = this.$route.query.returnUrl;
    2. localStorage.removeItem("returnUrl");
    3. if (returnUrl != undefined && returnUrl !=null && returnUrl != 'null') {
    4. localStorage.setItem("returnUrl",returnUrl);
    5. }
    6. // 查看sso server是否登录,如果登录带着token返回
    7. let token = localStorage.getItem("token");
    8. if (token != 'null') {
    9. // 校验登录
    10. logincheck(token).then(res=>{
    11. if (res.data.data != null) {
    12. window.location.href=returnUrl+"?token="+token;
    13. }else {
    14. window.location.href="/login?returnUrl="+returnUrl;
    15. }
    16. }).catch((err) => {
    17. })
    18. .finally(() => {
    19. });
    20. } else {
    21. window.location.href="/login?returnUrl="+returnUrl;
    22. }

    三、代码地址

    注意事项: 

    客户端需要修改SSO 登录地址

    在线demo 用户名/密码 admin/123456 

    server: SSO SERVER

    client: SPIKE SERVICE

    github: https://github.com/katriina-tavi/spike

    gitee: spike: 前端分离实现单点登录 vue element springboot xxl-sso实现

  • 相关阅读:
    2024制造企业数字化趋势
    泡泡玛特:一家中国潮玩品牌的出海之旅
    Python 笔记02 (网络交互 TCP/UDP)
    prettytable:一款像数据库一样可完美格式化输出的 Python 库
    2.如何选择go语言基础类型——Leetcode习题9
    k8s之Job和CronJob
    LAXCUS分布式操作系统相比LINUX的优势
    VoLTE端到端业务详解 | 移动性管理类
    Weblogic SSRF漏洞
    JVM在线分析-监控工具(jps, jstat, jstatd)
  • 原文地址:https://blog.csdn.net/dream_snow2012/article/details/126832818