spring cloud單點登入,springcloud單點

來源:互聯網
上載者:User

spring cloud單點登入,springcloud單點
概述基於springcloud的單點登入服務及基於zuul的網關服務(解決了通過zuul轉寄到認證服務之後session丟失問題)詳細代碼下載:http://www.demodashi.com/demo/10313.html一、準備工作

學習前請先系統的學習一下eureka、zuul、spring security,否則上手可能會比較困難,我當時買的《springcloud微服務實戰》,這本書寫的還不錯。

該項目基於springcloud Dalston.SR1。因公司決定使用spring cloud,前期做認證服務時發現通過zuul網關把請求轉寄到認證服務之後session丟失,一直報csrf驗證失敗問題,網上的大部分資料也不靠譜,通過研究解決掉該問題,特做了一個例子,供大家參考

二、項目

三、各個服務說明

① 服務註冊(基於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服務註冊配置資訊不再過多描述,標準用法

② api-gateway-server網關服務配置資訊

spring:  aop: #aop代理    proxyTargetClass: true  application:    name: api-gateway-serverserver:  port: 8080  tomcat:    uri-encoding: UTF-8#服務註冊eureka:   client:    serviceUrl:      defaultZone: http://127.0.0.1:8761/eureka/#  server:#    enable-self-preservation: false  #關閉eureka自我保護,生產環境不建議關閉自我保護#認證中心index頁面地址,如果直接登入認證中心則會跳轉到該地址uaa.server.index-path: /uaa/index#認證中心跳轉路徑首碼uaa.server.service.path: /uaa/**#不走認證的url集合http.authorize.matchers: /**/css/**,/**/styles/**,/**/js/**,/**/plugin/**,/**/plugins/**,/**/template/**,/**/img/**,/**/fonts/**,/**/cvr100u/**,/css/**,/js/**,/plugin/**,/template/**,/img/**,/fonts/**,/cvr100u/**#網關資訊zuul:  routes:    uaa-server:      sensitiveHeaders: "*"  #敏感headers也支援全域設定(必須這樣設定)      path: ${uaa.server.service.path}      stripPrefix: false  add-proxy-headers: true  #X-Forwarder-Host要求標頭預設添加到轉寄請求中#安全認證資訊security:  basic:    enabled: false   oauth2:    sso:      loginPath: /login    client:      accessTokenUri: http://127.0.0.1:7769/uaa/oauth/token      userAuthorizationUri: /uaa/oauth/authorize      clientId: acme      clientSecret: acmesecret    resource:      jwt:        keyValue: |          -----BEGIN PUBLIC KEY-----          MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnGp/Q5lh0P8nPL21oMMrt2RrkT9AW5jgYwLfSUnJVc9G6uR3cXRRDCjHqWU5WYwivcF180A6CWp/ireQFFBNowgc5XaA0kPpzEtgsA5YsNX7iSnUibB004iBTfU9hZ2Rbsc8cWqynT0RyN4TP1RYVSeVKvMQk4GT1r7JCEC+TNu1ELmbNwMQyzKjsfBXyIOCFU/E94ktvsTZUHF4Oq44DBylCDsS1k7/sfZC2G5EU7Oz0mhG8+Uz6MSEQHtoIi6mc8u64Rwi3Z3tscuWG2ShtsUFuNSAFNkY7LkLn+/hxLCu2bNISMaESa8dG22CIMuIeRLVcAmEWEWH5EEforTg+QIDAQAB          -----END PUBLIC KEY-----      id: openid      serviceId: ${PREFIX:}resource

③ uaa-server配置資訊

spring:  application:    name: uaa-server  #資料庫連接資訊  datasource:    url: jdbc:mysql://localhost:3306/uaa?characterEncoding=UTF-8    username: root    password: root    driver-class-name: com.mysql.jdbc.Driver    max-idle: 5    max-wait: 10000    min-idle: 2    initial-size: 3    validation-query: SELECT 1    time-between-eviction-runs-millis: 18800    jdbc-interceptors: ConnectionState;SlowQueryReport(threshold=50)  jpa:     database: MYSQL    show-sql: true  #使用redis儲存session,redis服務地址  redis:    host: 127.0.0.1     port: 6379#不緩衝thymeleaf模板,開發環境下配置該屬性,生產環境下請勿配置thymeleaf:     cache: false    cache-period: 0template:     cache: falseserver:  port: 7769  context-path: /uaa   #認證服務上下文地址(必須配置)  use-forward-headers: false  tomcat:    uri-encoding: UTF-8#服務註冊eureka:   instance:    preferIpAddress: true  client:    serviceUrl:      defaultZone: http://127.0.0.1:8761/eureka/security:  basic:    enabled: false  user:    password: password  ignored: /css/**,/js/**,/favicon.ico,/webjars/**  sessions: NEVER #永遠不自己建立session#jwt資訊(自訂的屬性,AuthorizationServerConfigurer配置類中用到)jwt:  access:    token:      converter:        resource:          location: classpath:keystore.jks          password: foobar          key-pair-alias: test    #自訂的屬性,WebSecurityConfigurer配置類中用到 http:  authorize:    #不走認證的url集合    matchers: /**/css/**,/**/js/**,/**/plugin/**,/**/template/**,/**/img/**,/**/fonts/**,/**/cvr100u/**,/css/**,/js/**,/plugin/**,/template/**,/img/**,/fonts/**,/cvr100u/**  login:    path: /login
五、java代碼配置

①、api-gateway-server服務配置都集中在WebSecurityConfigurer類中,配置比較簡單

②、uaa-server服務配置都集中在AuthorizationServerConfigurer和WebSecurityConfigurer中,AuthorizationServerConfigurer是jwt相關的配置,WebSecurityConfigurer是安全相關的配置,重要的部分代碼中已經做了注釋

六、項目運行效果

註:項目運行前請閱讀readme.txt檔案

使用者名稱:admin@163.com 密碼:admin

代碼下載:http://www.demodashi.com/demo/10313.html註:本文著作權歸作者,由demo大師發表,拒絕轉載,轉載需要作者授權

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.