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作为密码,这里就是用户的默认生成

相关推荐
java_logo2 分钟前
Supabase Postgres Docker 容器化部署指南
运维·docker·postgresql·容器·postgres部署教程·postgres部署文档·docker postgres
学编程就要猛6 分钟前
算法:1.移动零
java·算法
北冥有一鲲9 分钟前
LangChain.js:Tool、Memory 与 Agent 的深度解析与实战
开发语言·javascript·langchain
开开心心_Every17 分钟前
优化C盘存储:自定义软件文档保存路径工具
java·网络·数据库·typescript·word·asp.net·excel
多则惑少则明18 分钟前
AI大模型实用(八)Java快速实现智能体整理(使用LangChain4j-agentic来进行情感分析/分类)
java·人工智能·spring ai·langchain4j
吴佳浩 Alben19 分钟前
Python入门指南(六) - 搭建你的第一个YOLO检测API
开发语言·python·yolo
love530love19 分钟前
Win11+RTX3090 亲测 · ComfyUI Hunyuan3D 全程实录 ③:diso 源码编译实战(CUDA 13.1 零降级)
开发语言·人工智能·windows·python·comfyui·hunyuan3d·diso
qq_3771123720 分钟前
JAVA的平凡之路——此峰乃是最高峰JVM-GC垃圾回收器(2)-06
java·开发语言·jvm
黄俊懿21 分钟前
【深入理解SpringCloud微服务】Gateway简介与模拟Gateway手写一个微服务网关
spring boot·后端·spring·spring cloud·微服务·gateway·架构师
weixin_4686352924 分钟前
用python获取双色球历史数据,纯数据处理,非爬虫
开发语言·爬虫·python