A Microservice Architecture with Spring Boot and Spring Cloud(二)
阿新 • • 發佈:2018-12-17
安全配置
下一步是保護這兩個API。 雖然後面我們可能需要用OAuth2 + JWT來實現,但現在從基本認證開始。 這正是我們要開始的地方。
首先,我們Book application 的安全配置如下:
@EnableWebSecurity
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
public void configureGlobal1(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.httpBasic()
.disable()
.authorizeRequests()
.antMatchers(HttpMethod.GET, "/books").permitAll()
.antMatchers(HttpMethod.GET, "/books/*" ).permitAll()
.antMatchers(HttpMethod.POST, "/books").hasRole("ADMIN")
.antMatchers(HttpMethod.PATCH, "/books/*").hasRole("ADMIN")
.antMatchers(HttpMethod.DELETE, "/books/*").hasRole("ADMIN")
.anyRequest().authenticated()
.and()
.csrf()
.disable();
}
}
Rating application的安全配置:
@EnableWebSecurity
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
public void configureGlobal1(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.httpBasic()
.disable()
.authorizeRequests()
.regexMatchers("^/ratings\\?bookId.*$").authenticated()
.antMatchers(HttpMethod.POST,"/ratings").authenticated()
.antMatchers(HttpMethod.PATCH,"/ratings/*").hasRole("ADMIN")
.antMatchers(HttpMethod.DELETE,"/ratings/*").hasRole("ADMIN")
.antMatchers(HttpMethod.GET,"/ratings").hasRole("ADMIN")
.anyRequest().authenticated()
.and()
.csrf()
.disable();
}
}
因為這些API很簡單,所以我們可以直接使用全域性匹配器來進行安全管理。 然而,隨著它們變得越來越複雜,我們需要將其遷移到方法體上用註釋實現。
上面的安全配置定義很簡單:
- 任何人都可以訪問資源
- 只有擁有管理員許可權的可以修改資源
SpringCloud配置
現在,隨著我們的兩個API獨立執行,是時候使用SpringCloud和引用我們的微服務架構中的一些非常有用的元件:
- 服務配置。提供,管理和集中配置,以例項化不同模組的配置。
- 服務發現 。使應用程式能夠有效和靈活地發現服務。
- 閘道器服務。作為反向代理,並通過在一個埠上提供所有API來隱藏我們系統的複雜性。
- 兩個REST API 。 Books API和Ratings API。
我們將使用Spring Initializr快速引導這三個新應用程式。
首先,我們將設定服務配置。 我們需要Cloud Config,Eureka,和Security:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
接下來,我們需要使用@EnableConfigServer通過Eureka客戶端發現我們的服務配置,如下所示:
@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class ConfigApplication {...}
這裡是我們的Boot application.properties:
server.port=8081
spring.application.name=config
spring.cloud.config.server.git.uri=file:///${user.home}/application-config
eureka.client.region=default
eureka.client.registryFetchIntervalSeconds=5
eureka.client.serviceUrl.defaultZone=http://discUser:[email protected]:8082/eureka/
security.user.name=configUser
security.user.password=configPassword
security.user.role=SYSTEM
接下來,我們需要在我們的HOME目錄中建立一個本地的Git儲存庫application-config來儲存配置檔案:
cd ~
mkdir application-config
cd application-config
git init
請注意,我們正在使用本地Git倉庫進行測試。