美文网首页
添加安全认证

添加安全认证

作者: 寂静的春天1988 | 来源:发表于2019-05-10 16:59 被阅读0次

    发现随便都可以进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@

    相关文章

      网友评论

          本文标题:添加安全认证

          本文链接:https://www.haomeiwen.com/subject/krgaoqtx.html