zoukankan      html  css  js  c++  java
  • (二)Spring Security 入门体验之——用户密码配置

    Spring Security用户密码配置

    除了上面Spring Security在不进行任何配置下默认给出的用户user 密码随项目启动生成随机字符串,我们还可以通过以下方式配置

    1、springboot配置文件中配置

    server:
      port: 8083
    
    spring:
      security:
        user:
          name: admin  # 用户名
          password: 123456  # 密码

    2、java代码在内存中配置

    新建Security 核心配置类继承WebSecurityConfigurerAdapter

    @Configuration
    @EnableWebSecurity
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
        /**
         * 将用户设置在内存中
         * @param auth
         * @throws Exception
         */
        @Autowired
        public void config(AuthenticationManagerBuilder auth) throws Exception {
            // 在内存中配置用户,配置多个用户调用`and()`方法
            auth.inMemoryAuthentication()
                    // 指定加密方式
                    .passwordEncoder(passwordEncoder())
                    .withUser("admin").password(passwordEncoder().encode("123456")).roles("ADMIN")
                    .and()
                    .withUser("test").password(passwordEncoder().encode("123456")).roles("USER");
        }
    
        @Bean
        public PasswordEncoder passwordEncoder() {
            // BCryptPasswordEncoder:Spring Security 提供的加密工具,可快速实现加密加盐
            return new BCryptPasswordEncoder();
        }
    }

    3、从数据库中获取用户账号、密码信息

    这种方式也就是我们项目中通常使用的方式,这个留到后面的文章再说

  • 相关阅读:
    冒泡排序
    Bootstrap fileinput v1.0(ssm版)
    Bootstrap table后端分页(ssm版)
    Bootstrap table前端分页(ssm版)
    北京鱼乐贝贝面试题
    通过前端控制器源码分析springmvc的执行过程
    java Pattern(正则)类
    ssm所需要的pom(jre8、tomcat8、spring4)
    Java 流(Stream)、文件(File)和IO
    idea构建一个简单的maven_web项目
  • 原文地址:https://www.cnblogs.com/changyuyao/p/14206324.html
Copyright © 2011-2022 走看看