当前位置:   article > 正文

spring cloud单点登录_springcloud gateway 互踢登录单点登录

springcloud gateway 互踢登录单点登录

技术:springboot1.5.2.RELEASE + springcloud Dalston.SR1

运行环境:jdk1.7 + MySQL5.5 + redis3.0.503

概述

基于springcloud的单点登录服务及基于zuul的网关服务(解决了通过zuul转发到认证服务之后session丢失问题)

详细

一、准备工作

学习前请先系统的学习一下eureka、zuul、spring security,否则上手可能会比较困难,我当时买的《springcloud微服务实战》,这本书写的还不错。

该项目基于springcloud Dalston.SR1。因公司决定使用spring cloud,前期做认证服务时发现通过zuul网关把请求转发到认证服务之后session丢失,一直报csrf验证失败问题,网上的大部分资料也不靠谱,通过研究解决掉该问题,特做了一个例子,供大家参考

二、项目截图

    blob.png    

blob.png

blob.png

三、各个服务说明

    ① 服务注册(基于eureka):项目名称:service-registry-server    端口号:8761  

        启动类:cn.com.springcloudtest.cloud.service.registry.ServiceRegistryServerApplication

    ② 网关服务(基于zuul): 项目名称:api-gateway-server    端口号:8080

        启动类:cn.com.springcloudtest.cloud.api.gateway.ApiGatewayServerApplication

    ③ 认证服务(基于oauth2及spring security): 项目名称:uaa-server    端口号:7769

        启动类:cn.com.springcloudtest.cloud.uaa.UaaServerApplication

        认证服务使用redis保存了session,客户端保存于mysql数据库

四、配置文件说明

有些配置作者也没全部搞明白,网上找的设置,但是这么设置确定是没问题的

    ① service-registry-server服务注册配置信息不再过多描述,标准用法     

  1. spring:
  2.   aop: #aop代理
  3.     proxyTargetClass: true
  4.   application:
  5.     name: api-gateway-server
  6. server:
  7.   port: 8080
  8.   tomcat:
  9.     uri-encoding: UTF-8
  10. #服务注册
  11. eureka: 
  12.   client:
  13.     serviceUrl:
  14.       defaultZone: http://127.0.0.1:8761/eureka/
  15. #  server:
  16. #    enable-self-preservation: false  #关闭eureka自我保护,生产环境不建议关闭自我保护
  17. #认证中心index页面地址,如果直接登录认证中心则会跳转到该地址
  18. uaa.server.index-path: /uaa/index
  19. #认证中心跳转路径前缀
  20. uaa.server.service.path: /uaa/**
  21. #不走认证的url集合
  22. http.authorize.matchers: /**/css/**,/**/styles/**,/**/js/**,/**/plugin/**,/**/plugins/**,/**/template/**,/**/img/**,/**/fonts/**,/**/cvr100u/**,/css/**,/js/**,/plugin/**,/template/**,/img/**,/fonts/**,/cvr100u/**
  23. #网关信息
  24. zuul:
  25.   routes:
  26.     uaa-server:
  27.       sensitiveHeaders: "*"  #敏感headers也支持全局设置(必须这样设置)
  28.       path: ${uaa.server.service.path}
  29.       stripPrefix: false
  30.   add-proxy-headers: true  #X-Forwarder-Host请求头默认添加到转发请求中
  31. #安全认证信息
  32. security:
  33.   basic:
  34.     enabled: false 
  35.   oauth2:
  36.     sso:
  37.       loginPath: /login
  38.     client:
  39.       accessTokenUri: http://127.0.0.1:7769/uaa/oauth/token
  40.       userAuthorizationUri: /uaa/oauth/authorize
  41.       clientId: acme
  42.       clientSecret: acmesecret
  43.     resource:
  44.       jwt:
  45.         keyValue: |
  46.           -----BEGIN PUBLIC KEY-----
  47.           MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnGp/Q5lh0P8nPL21oMMrt2RrkT9AW5jgYwLfSUnJVc9G6uR3cXRRDCjHqWU5WYwivcF180A6CWp/ireQFFBNowgc5XaA0kPpzEtgsA5YsNX7iSnUibB004iBTfU9hZ2Rbsc8cWqynT0RyN4TP1RYVSeVKvMQk4GT1r7JCEC+TNu1ELmbNwMQyzKjsfBXyIOCFU/E94ktvsTZUHF4Oq44DBylCDsS1k7/sfZC2G5EU7Oz0mhG8+Uz6MSEQHtoIi6mc8u64Rwi3Z3tscuWG2ShtsUFuNSAFNkY7LkLn+/hxLCu2bNISMaESa8dG22CIMuIeRLVcAmEWEWH5EEforTg+QIDAQAB
  48.           -----END PUBLIC KEY-----
  49.       id: openid
  50.       serviceId: ${PREFIX:}resource

    ② api-gateway-server网关服务配置信息

  1. spring:
  2.   application:
  3.     name: uaa-server
  4.   #数据库连接信息
  5.   datasource:
  6.     url: jdbc:mysql://localhost:3306/uaa?characterEncoding=UTF-8
  7.     username: root
  8.     password: root
  9.     driver-class-name: com.mysql.jdbc.Driver
  10.     max-idle: 5
  11.     max-wait: 10000
  12.     min-idle: 2
  13.     initial-size: 3
  14.     validation-query: SELECT 1
  15.     time-between-eviction-runs-millis: 18800
  16.     jdbc-interceptors: ConnectionState;SlowQueryReport(threshold=50)
  17.   jpa: 
  18.     database: MYSQL
  19.     show-sql: true
  20.   #使用redis存储session,redis服务地址
  21.   redis:
  22.     host: 127.0.0.1 
  23.     port: 6379
  24. #不缓存thymeleaf模板,开发环境下配置该属性,生产环境下请勿配置
  25. thymeleaf: 
  26.     cache: false
  27.     cache-period: 0
  28. template: 
  29.     cache: false
  30. server:
  31.   port: 7769
  32.   context-path: /uaa   #认证服务上下文地址(必须配置)
  33.   use-forward-headers: false
  34.   tomcat:
  35.     uri-encoding: UTF-8
  36. #服务注册
  37. eureka: 
  38.   instance:
  39.     preferIpAddress: true
  40.   client:
  41.     serviceUrl:
  42.       defaultZone: http://127.0.0.1:8761/eureka/
  43. security:
  44.   basic:
  45.     enabled: false
  46.   user:
  47.     password: password
  48.   ignored: /css/**,/js/**,/favicon.ico,/webjars/**
  49.   sessions: NEVER #永远不自己创建session
  50. #jwt信息(自定义的属性,AuthorizationServerConfigurer配置类中用到)
  51. jwt:
  52.   access:
  53.     token:
  54.       converter:
  55.         resource:
  56.           location: classpath:keystore.jks
  57.           password: foobar
  58.           key-pair-alias: test
  59. #自定义的属性,WebSecurityConfigurer配置类中用到 
  60. http:
  61.   authorize:
  62.     #不走认证的url集合
  63.     matchers: /**/css/**,/**/js/**,/**/plugin/**,/**/template/**,/**/img/**,/**/fonts/**,/**/cvr100u/**,/css/**,/js/**,/plugin/**,/template/**,/img/**,/fonts/**,/cvr100u/**
  64.   login:
  65.     path: /login

    ③ uaa-server配置信息

五、java代码配置

    ①、api-gateway-server服务配置都集中在WebSecurityConfigurer类中,配置比较简单

    ②、uaa-server服务配置都集中在AuthorizationServerConfigurer和WebSecurityConfigurer中,AuthorizationServerConfigurer是jwt相关的配置,WebSecurityConfigurer是安全相关的配置,重要的部分代码中已经做了注释

        

六、项目运行效果

注:项目运行前请阅读readme.txt文件

用户名:admin@163.com  密码:admin   

blob.png

blob.png

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/正经夜光杯/article/detail/768446
推荐阅读
相关标签
  

闽ICP备14008679号