Spring Cloud 服務安全鏈接

Spring Cloud能夠增長HTTP Basic認證來增長服務鏈接的安全性。spring

一、加入security啓動器

在maven配置文件中加入Spring Boot的security啓動器。安全

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

這樣,就開啓對服務鏈接的安全保護,系統默認爲生成一個用戶名爲"user"及一個隨機密碼,隨機密碼在服務啓動的時候在日誌中會打印出來。微信

二、自定義用戶名密碼

隨機密碼沒什麼實際意義,咱們須要一個固定的鏈接用戶名和密碼。maven

在應用配置文件中加入如下配置便可。spring-boot

security: 
  user: 
    name: admin
    password: admin123456

這樣配置完後在鏈接這個服務的時候就會要求輸入用戶名和密碼,若是認證失敗會返回401錯誤。spa

{
    "timestamp": 1502689874556,
    "status": 401,
    "error": "Unauthorized",
    "message": "Bad credentials",
    "path": "/test/save"
}

三、安全鏈接

一、註冊中心安全鏈接日誌

username:password@ipaddresscode

二、Feign申明式服務安全鏈接教程

@FeignClient(name = "SERVICE", configuration = FeignAuthConfig.class)
public interface OrderService extends OrderAPI {

}
@Configuration
public class FeignAuthConfig {

    @Bean
    public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
        return new BasicAuthRequestInterceptor("admin","admin123456");
    }
}

推薦:Spring Boot & Cloud 最強技術教程ip

掃描關注咱們的微信公衆號,乾貨天天更新。

image

相關文章
相關標籤/搜索