spring cloud 總結(jié)

一. config 加密

先下載JCE,替換

keytool -genkeypair -alias {我的key} -keyalg RSA -dname "CN=Web Server,OU=Unit,O=Organization,L=City,S=State,C=CN" -keypass {我的secret} -keystore my_keystore.jks -storepass {我的password}

把生成的my_keystore.jks 拷的resources下
修改bootstrap.yml

encrypt:
    key-store:    # 非對(duì)稱(chēng)加密
        location: classpath:/my_keystore.jks
        password: 我的password
        alias: 我的key
        secret: 我的secret
    key: 自定義key    # 對(duì)稱(chēng)加密

password: '{cipher}密碼' # 在yml中用括起來(lái), properties中不用

二. config 動(dòng)態(tài)刷新

在需要刷新的和config-server中都加入spring-cloud-starter-bus-amqp依賴,刷新的地方加上@RefreshScope注解

spring:
    rabbitmq:
            host: 39.107.123.121
            port: 5672
            username: guest
            password: '{cipher}密碼'
anagement:
    security:
        enabled: false
encrypt:
    key: fengf

/bus/refresh?destination=** 刷新

三.Edgware升級(jí)到Finchley

①spring boot 1.5.x --> 2.0.x
②eureka:spring-cloud-starter-eureka --> spring-cloud-starter-netflix-eureka-client
spring-cloud-starter-eureka-server --> spring-cloud-starter-netflix-eureka-server
③ zuul spring-cloud-starter-zuul --> spring-cloud-starter-netflix-zuul
④hystrix spring-cloud-starter-hystrix --> spring-cloud-starter-netflix-hystrix
⑤調(diào)用 spring-cloud-starter-feign --> spring-cloud-starter-openfeign
spring-cloud-starter-ribbon --> 不需要再加了,已經(jīng)包含在eureka 里了
⑥config刷新
server和client配置 取消安全驗(yàn)證: management.security.enable=false --> management.endpoints.web.exposure.include=bus-refresh
添加 spring-boot-starter-actuator 和 spring-cloud-starter-bus-amqp 依賴
客戶端要加上 @RefreshScope注解 ??!
調(diào)用 : /bus/refresh ---> /actuator/bus-refresh

四. oauth2

斷斷續(xù)續(xù)折騰了有四五個(gè)月個(gè),20180718今天終于能獲取token了, 心態(tài)都崩了好幾次??偨Y(jié)如下
①post /oauth/token 返回 401 unauthorized
可能為security和oauth2的Resource互相覆蓋,在ResourceConfig中設(shè)置放過(guò)/oauth/token
也嘗試了在配置文件中添加security.oauth2.resource.filter-order=3但這個(gè)已經(jīng)過(guò)時(shí)而且注釋掉無(wú)影響
②加密密碼
可以配置不加密密碼

@Bean
    public static NoOpPasswordEncoder passwordEncoder() {
        return (NoOpPasswordEncoder) NoOpPasswordEncoder.getInstance();
    }

或者按照spring5中新的格式 {PasswordEncoder的id}原始密碼

@Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory().withClient("client").secret("{noop}mysecret")
                .authorizedGrantTypes("password", "refresh_token").scopes("all");
    }

但實(shí)測(cè)后在.secret("")里只要寫(xiě)加密后的字符串就行了
③請(qǐng)求參數(shù)


image.png

或者

post localhost:8888/auth/oauth/token?grant_type=password
header: {
    Authorization: Basic d2ViOndlYkFwcA==    clientId和clientSecret編碼后
    Content-Type: application/x-www-form-urlencoded
}
body:{
    username: f
    password: ff
}

鑒權(quán)check_token

post localhost:8888/auth/oauth/check_token
header 相同 
body:{
        token: "token"
}

刷新token

post localhost:8888/auth/oauth/token?grant_type=refresh_token&refresh_token="refresh_token"
header:{
    Authorization:  Basic d2ViOndlYkFwcA==
}
無(wú)body

五. zuul放過(guò)header

zuul 會(huì)默認(rèn)過(guò)濾掉請(qǐng)求header,比如 Authorization, 在配置文件中設(shè)置

zuul:
    host:
        connect-timeout-millis: 990000
        socket-timeout-millis: 990000
    sensitive-headers:           
    add-host-header: true

六. gateway的坑

  1. gateway是基于webflux實(shí)現(xiàn),所以不要引用spring-boot-starter-web這種基于springmvc的
  2. gateway與hystrix
spring:
    cloud:
        gateway:
            discovery:
                locator:
                    enabled: true
            default-filters:
            routes:
                -   id: user
                    uri: lb://user
                    predicates:
                        - Path=/user/**
                    filters:
                        - RewritePath=/user/(?<path>.*), /$\{path}
                        - name: Hystrix
                          args:
                              name: userHystrixCommand
                              fallbackUri: forward:/hystrixTimeout
@RequestMapping("/hystrixTimeout")
public String hystrixTimeout() {
    return "gateway觸發(fā)了斷路由";
}

@HystrixCommand(commandKey = "userHystrixCommand",commandProperties = {
        @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "30000")}
)
public Map userHystrixCommand() {
    Map<String, String> map = new HashMap<>();
    map.put("message", "gateway觸發(fā)了userHystrixCommand");
    return map;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容