引言
本文代码已提交至Github(版本号:
be503769cbddd9c9fe7775956035923410e785ab
),有兴趣的同学可以下载来看看:https://github.com/ylw-github/taodong-shop
阅读本文时,建议先阅读前面博客:
- 《淘东电商项目(31) -SSO单点登录(XXL-SSO案例)》
- 《淘东电商项目(32) -SSO单点登录(集成SSO认证服务)》
- 《淘东电商项目(33) -SSO单点登录(改造SSO认证服务登录界面)》
前面的文章已经把XXL-SSO服务集成到我们的「淘东电商」项目了,而且把登录界面也移植到了SSO服务,接下来我们集成SSO Client端。
本文目录结构:
l____引言
l____ 1. 首页门户集成SSO Client
l____ 2. 聚合支付门户集成SSO Client
l____ 3. 测试
l____总结
1. 首页门户集成SSO Client
1.Maven添加xxl-sso-core
模块:
<dependency>
<groupId>com.ylw</groupId>
<artifactId>taodong-shop-common-xxlsso-core</artifactId>
<version>1.0-RELEASE</version>
</dependency>
2.配置applicatoin.yml,完整内容如下(注意要在hosts文件里配置好域名):
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:8100/eureka
server:
port: 8080
spring:
application:
name: app-portal-web
freemarker:
cache: false
charset: UTF-8
check-template-location: true
content-type: text/html
expose-request-attributes: true
expose-session-attributes: true
request-context-attribute: request
suffix: .ftl
template-loader-path:
- classpath:/templates
redis:
host: 127.0.0.1
port: 6379
jedis:
pool:
max-idle: 100
min-idle: 1
max-active: 1000
max-wait: -1
xxl:
sso:
logout:
path: /logout
server: http://taodong.ssoserver.com:8099
xxl-sso:
excluded:
paths: ''
3.添加配置文件:
/**
* description:
* create by: YangLinWei
* create time: 2020/3/19 10:10 上午
*/
@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
创建聚合支付门户模块taodong-shop-portal-pay-web
,具体的代码不再详述,可以clone代码下来看,SSO Client方式与上面一样:
3. 测试
1.启动Eureka服务、SSO认证服务、会员服务、门户服务、聚合支付服务。
2.浏览器访问门户服务(注意:hosts文件已经配置了域名)http://taodong.com:8080/,浏览器自动跳转到登录界面:
3.输入登录信息,执行登录操作,登录成功,可以看到登录成功后,地址栏的url也发生改变了http://taodong.com:8080/?xxl_sso_sessionid=27_7621bc6aeffe49feb58904ea5f3439d0:
同时,看下cookie信息,也把session id自动写入了浏览器的cookie:
4.访问聚合支付门户http://taodong.pay.com:8079/,可以看到直接就跳转到了聚合支付的首页了,而且浏览器的Session id与门户服务的session id一样:
总结
本文主要讲解SSO Client集成与测试。