zoukankan      html  css  js  c++  java
  • 自定义 Spring Boot 安全组件 Security 的用户名和密码的方法

    1、Spring Boot 2.3.0.RELEASE 的默认用户名为 user,密码不再启动时输出,所以需要我们手动设置用户名和密码,目前有三种方法:
    
    2、方法一,在 application.properties 配置文件中加入如下配置:
        spring.security.user.name=admin
        spring.security.user.password=123456
    
    3、方法二,在 application.yml 配置文件中加入如下配置:
        spring:
          security:
            user:
              name: root
              password: admin
    
    4、方法三,自定义【网页安全配置器】类,继承 SpringBootWebSecurityConfiguration 类,覆写类 SpringBootWebSecurityConfiguration 的配置方法 configure,代码片段如下:
        @Override
            protected void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
                /**
                 * 通过此种方式设置的用户名和密码优先级高于通过 application.yml 或 application.properties 设置的用户名和密码
                 * @作者 hapday
                 * @时间 2020/8/3 15:46
                 * @起始于 0.1.0
                */
                authenticationManagerBuilder
                        .inMemoryAuthentication()
                        .withUser("hapday")
                        .password("12345678")
                        .roles("admin");
            }
    
    5、综述:上述三种方法是等效的,可以看到我们为每种方法设置了不同的账号,实践得知,自定义类的方式的优先级最高,其次是 application.properties,最后是 application.yml,这点需要我们注意,实际开发中从安全的角度考虑,一般都会采用自定义类的方式读取数据库,而不是明文的方式。
  • 相关阅读:
    cf C. Vasya and Robot
    zoj 3805 Machine
    cf B. Vasya and Public Transport
    cf D. Queue
    cf C. Find Maximum
    cf B. Two Heaps
    cf C. Jeff and Rounding
    cf B. Jeff and Periods
    cf A. Jeff and Digits
    I Think I Need a Houseboat
  • 原文地址:https://www.cnblogs.com/hapday/p/13459142.html
Copyright © 2011-2022 走看看