添加安全认证
2019-05-10 本文已影响0人
寂静的春天1988
发现随便都可以进eureka页面,我们需要添加一个安全认证
eureka类:
增加pom文件
<!-- 添加安全认证 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
添加yml
spring:
security: #添加用户认证
user:
name: root #设置用户名
password: 123 #设置用户密码
这是发现再通过127.0.0.1:8080进入eureka需要输入用户名和密码才能进入了。
config类
package com.ganlong.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable(); //关闭csrf
http.authorizeRequests().anyRequest().authenticated().and().httpBasic(); //开启认证
}
}
同时teacher和student的yml
service-url:
defaultZone: http://root:123@127.0.0.1:8080/eureka/
联系地址需要加userName:passWord@