spring-cloud Finchley.RELEASE 版本爲eureka 增加用戶名密碼驗證

 這個版本的eureka ,配置用戶名密碼 與其他版本 有所不同

<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Finchley.RELEASE</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

1.首先是配置文件 修改:

 

原來的配置方式

#security:  
#  basic:  
#    enabled: true  
#  user:  
#   name: user  
#   password: password    


現在的配置方式

spring:
  security:
    user:
      name: user  
      password: password    

2.pom文件

	<!--添加用戶名密碼  -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-security</artifactId>
		</dependency> 

3.


@EnableWebSecurity
@Configuration
public class WebSecurityConfig  extends  WebSecurityConfigurerAdapter   {
	
	  @Override
	    protected void configure(HttpSecurity http) throws Exception {
	        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER);
	        http.csrf().disable(); //關閉csrf
	        //注意:爲了可以使用 http://${user}:${password}@${host}:${port}/eureka/ 這種方式登錄,所以必須是httpBasic,如果是form方式,不能使用url格式登錄
	        http.authorizeRequests().anyRequest().authenticated().and().httpBasic(); //開啓認證
	            
		
		  }

}

所以要在程序裏 寫一個配置類

這樣用戶名 密碼就可以添加上了

 

如果不寫那個配置類 ,就會成這個樣子

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章