SpringSecurity6从入门到实战之默认用户的生成流程

SpringSecurity6从入门到实战之默认用户的生成流程

这次还是如标题所示,上一章我们的登录页面已经知道是如何生成了.那么,我们通过表单登录的user用户以及密码SpringSecurity是如何进行生成的呢?

默认用户生成

让我们把登录流程重新拉回到读取/META-INF/spring/ .imports文件

我们进入SecurityAutoConfiguration源码中查看上面加载的注解

java 复制代码
package org.springframework.boot.autoconfigure.security.servlet;



@AutoConfiguration
@ConditionalOnClass({DefaultAuthenticationEventPublisher.class})
//加载配置类让SecurityProperties配置类生效
@EnableConfigurationProperties({SecurityProperties.class})
@Import({SpringBootWebSecurityConfiguration.class, SecurityDataConfiguration.class})
public class SecurityAutoConfiguration {
    public SecurityAutoConfiguration() {
    }

    @Bean
    @ConditionalOnMissingBean({AuthenticationEventPublisher.class})
    public DefaultAuthenticationEventPublisher authenticationEventPublisher(ApplicationEventPublisher publisher) {
        return new DefaultAuthenticationEventPublisher(publisher);
    }
}

一般以Properties命名结尾的都是对应的配置类,分别对应application.properties的内容,但是现在我们在application.properties中并没有配置任何内容.

SecurityProperties

java 复制代码
public class SecurityProperties {
    public static final int BASIC_AUTH_ORDER = 2147483642;
    public static final int IGNORED_ORDER = Integer.MIN_VALUE;
    public static final int DEFAULT_FILTER_ORDER = -100;
    private final Filter filter = new Filter();
    //这里就是默认创建的一个用户
    private final User user = new User();

    public SecurityProperties() {
    }

    public User getUser() {
        return this.user;
    }

    public Filter getFilter() {
        return this.filter;
    }

    public static class Filter {
        private int order = -100;
        private Set<DispatcherType> dispatcherTypes = EnumSet.allOf(DispatcherType.class);

        public Filter() {
        }

        public int getOrder() {
            return this.order;
        }

        public void setOrder(int order) {
            this.order = order;
        }

        public Set<DispatcherType> getDispatcherTypes() {
            return this.dispatcherTypes;
        }

        public void setDispatcherTypes(Set<DispatcherType> dispatcherTypes) {
            this.dispatcherTypes = dispatcherTypes;
        }
    }

    public static class User {
        private String name = "user";
        private String password = UUID.randomUUID().toString();
        private List<String> roles = new ArrayList();
        private boolean passwordGenerated = true;

        public User() {
        }

        public String getName() {
            return this.name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getPassword() {
            return this.password;
        }

        public void setPassword(String password) {
            if (StringUtils.hasLength(password)) {
                this.passwordGenerated = false;
                this.password = password;
            }
        }

        public List<String> getRoles() {
            return this.roles;
        }

        public void setRoles(List<String> roles) {
            this.roles = new ArrayList(roles);
        }

        public boolean isPasswordGenerated() {
            return this.passwordGenerated;
        }
    }
}

可以看到了这里有一个定义了name为user的用户和一个随机生成的UUID作为密码,这里就是用户的默认生成

相关推荐
L Jiawen12 分钟前
【Golang基础】基础知识(下)
服务器·开发语言·golang
Overt0p33 分钟前
抽奖系统(4)
java·spring boot·tomcat
萌萌哒草头将军43 分钟前
绿联云 NAS 安装 AudioDock 详细教程
前端·docker·容器
计算机毕设VX:Fegn08951 小时前
计算机毕业设计|基于springboot + vue宠物医院管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
云栖梦泽1 小时前
鸿蒙应用AI赋能与国际化落地实战:让待办应用跨越语言与智能边界
开发语言·鸿蒙系统
CoderCodingNo1 小时前
【GESP】C++五级真题(结构体排序考点) luogu-B3968 [GESP202403 五级] 成绩排序
开发语言·c++·算法
想做后端的小C2 小时前
Java:接口回调
java·开发语言·接口回调
爱学习的小可爱卢2 小时前
JavaEE进阶——Spring核心设计模式深度剖析
java·spring·设计模式
apocelipes2 小时前
docker-compose 部署单节点 kafka 4.0 测试环境
docker·golang·kafka·开发工具和环境
毕设源码-钟学长2 小时前
【开题答辩全过程】以 个性化电影推荐网站的设计与实现为例,包含答辩的问题和答案
java·spring boot