SSO单点登录(Client端集成)

Posted IT-老牛

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SSO单点登录(Client端集成)相关的知识,希望对你有一定的参考价值。

文章目录

1.首页门户集成SSO Client

1.Maven添加xxl-sso-core模块:

<dependency>
    <artifactId>guoranxinxian-shop-common-xxlsso-core</artifactId>
    <groupId>com.guoranxinxian</groupId>
    <version>1.0-SNAPSHOT</version>
</dependency>

2.配置applicatoin.yml,完整内容如下(注意要在hosts文件里配置好域名):

3.添加配置文件

spring.redis.hostName=127.0.0.1
spring.redis.port=6379

xxl.sso.logout.path=/logout
xxl.sso.server=http://guoranxinxian.ssoserver.com:8099
xxl-sso.excluded.paths=
package com.guoranxinxian.config;

import com.xxl.sso.core.conf.Conf;
import com.xxl.sso.core.filter.XxlSsoWebFilter;
import com.xxl.sso.core.util.JedisUtil;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class XxlSsoConfig implements DisposableBean 


    @Value("$xxl.sso.server")
    private String xxlSsoServer;

    @Value("$xxl.sso.logout.path")
    private String xxlSsoLogoutPath;

    @Value("$xxl-sso.excluded.paths")
    private String xxlSsoExcludedPaths;

    @Value("$spring.redis.host")
    private String redisHost;

    @Value("$spring.redis.port")
    private String port;


    @Bean
    public FilterRegistrationBean xxlSsoFilterRegistration() 

        // xxl-sso, redis init
        JedisUtil.init(String.format("redis://%s:%s", redisHost, port));

        // xxl-sso, filter init
        FilterRegistrationBean registration = new FilterRegistrationBean();

        registration.setName("XxlSsoWebFilter");
        registration.setOrder(1);
        registration.addUrlPatterns("/*");
        registration.setFilter(new XxlSsoWebFilter());
        registration.addInitParameter(Conf.SSO_SERVER, xxlSsoServer);
        registration.addInitParameter(Conf.SSO_LOGOUT_PATH, xxlSsoLogoutPath);
        registration.addInitParameter(Conf.SSO_EXCLUDED_PATHS, xxlSsoExcludedPaths);

        return registration;
    

    @Override
    public void destroy() throws Exception 

        // xxl-sso, redis close
        JedisUtil.close();
    



2. 聚合支付门户集成SSO Client

创建聚合支付门户模块guoranxinxian-shop-portal-pay-web,具体的代码不再详述,可以clone代码下来看,SSO Client方式与上面一样:

3. 测试

1.启动Eureka服务、SSO认证服务、会员服务门户服务聚合支付服务`。

2.浏览器访问门户服务(注意:hosts文件已经配置了域名)http://guoranxinxian.com:8080/,浏览器自动跳转到登录界面:

3.输入登录信息,执行登录操作,登录成功,可以看到登录成功后,地址栏的url也发生改变了http://guoranxinxian.com:8080/?xxl_sso_sessionid=27_c11ef89924a4465cbf395bfefcafc63d

同时,看下cookie信息,也把session id自动写入了浏览器的cookie:

4.访问聚合支付门户http://guoranxinxian.pay.com:8079/,可以看到直接就跳转到了聚合支付的首页了,而且浏览器的Session id与门户服务的session id一样:

4.显示登录的用户信息

     @GetMapping("/")
    public String index(HttpServletRequest request, HttpServletResponse response, Model model)
        XxlSsoUser xxlUser = (XxlSsoUser) request.getAttribute(Conf.SSO_USER);
        if (xxlUser != null && StringUtils.isNotEmpty(xxlUser.getUserid())) 
            DataResults<Users> results = usersFeign.getByUserId(Long.valueOf(xxlUser.getUserid()));
            if(results.getData()!=null)
                String mobile = results.getData().getMobile();
                // 对手机号码实现脱敏
                String desensMobile = mobile.replaceAll("(\\\\d3)\\\\d4(\\\\d4)", "$1****$2");
                model.addAttribute("desensMobile", desensMobile);
            
        

        model.addAttribute("goods_fresh_fruits",itemServiceFeign.findGoodsByCategory1Id(1001).getData()); // 新鲜水果 1001
        model.addAttribute("goods_fresh_fish",itemServiceFeign.findGoodsByCategory1Id(1038).getData()); // 海鲜水产 1038

        List<Content> content_top= (List<Content>) redisTemplate.opsForValue().get("redis_content_top");
        if(content_top==null||content_top.size()==0)
            content_top=contentServiceFeign.findContentBycategoryId(1).getData();
            redisTemplate.opsForValue().set("redis_content_top",content_top,3, TimeUnit.MINUTES);  //3分刷新缓存
        
        model.addAttribute("content_top",content_top); // 轮播图

        model.addAttribute("content_fresh_fruits",contentServiceFeign.findContentBycategoryId(3).getData()); // 新鲜水果主体
        return "index";
    


<li th:if="$desensMobile==null"><a href="login.html">您好,请登录</a></li>
       <li th:if="$desensMobile!=null"><a href="login.html" th:text="|您好,$desensMobile|">您好,请登录</a></li>
<li>
<a href="register.html">免费注册</a>
       </li>
       <li>
           <a href="home-order.html">我的订单</a>
       </li>
       <li th:if="$desensMobile!=null"><a href="javascript:void(0);" onclick="logout();">退出</a></li>
       <li>
           <a href="home-person-footprint.html">我的足迹</a>
       </li>

5.总结

本文主要讲解SSO Client集成与测试。

以上是关于SSO单点登录(Client端集成)的主要内容,如果未能解决你的问题,请参考以下文章

单点登录实现方案

SSO单点登录(集成SSO认证服务)

CAS搭建单点登录Web端

《果然新鲜》电商项目(36)-SSO单点登录(集成SSO认证服务)

JEESZ分布式框架--单点登录集成方案

cas 单点登录(SSO)实验之二: cas-client