惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

www.infosecurity-magazine.com
www.infosecurity-magazine.com
V
V2EX
博客园 - 【当耐特】
爱范儿
爱范儿
B
Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
MyScale Blog
MyScale Blog
月光博客
月光博客
博客园_首页
G
Google Developers Blog
Recent Announcements
Recent Announcements
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
D
Docker
aimingoo的专栏
aimingoo的专栏
博客园 - Franky
有赞技术团队
有赞技术团队
I
InfoQ
小众软件
小众软件
云风的 BLOG
云风的 BLOG
The GitHub Blog
The GitHub Blog
H
Help Net Security
The Register - Security
The Register - Security
U
Unit 42
P
Palo Alto Networks Blog
N
News and Events Feed by Topic
酷 壳 – CoolShell
酷 壳 – CoolShell
C
CERT Recently Published Vulnerability Notes
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
Security Latest
Security Latest
Scott Helme
Scott Helme
罗磊的独立博客
B
Blog RSS Feed
C
Check Point Blog
H
Heimdal Security Blog
T
Threatpost
V
Visual Studio Blog
V
Vulnerabilities – Threatpost
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Project Zero
Project Zero
Security Archives - TechRepublic
Security Archives - TechRepublic
S
Security Affairs
S
Schneier on Security
Apple Machine Learning Research
Apple Machine Learning Research
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
N
Netflix TechBlog - Medium
Attack and Defense Labs
Attack and Defense Labs
L
Lohrmann on Cybersecurity
D
DataBreaches.Net

博客园 - pmh905001

爬虫-今日头条我的收藏-反爬虫分析(六) pycharm的没落,vs code的兴起 爬虫-今日头条我的收藏-增量式下载网页内容(五) 爬虫-今日头条我的收藏-增量式导入到Elastic Search(四) 今日头条源代块一行代码很长情况下的拖动问题 爬虫-今日头条我的收藏-增量式导入到mongodb(三) 爬虫-今日头条我的收藏-增量式(二) openpyxl一个bug 爬虫-今日头条我的收藏(一) pystray被隐藏菜单项显示出来的问题 pyinstaller生成的exe程序使用使用默认程序打开execel文件 pyinstaller生成的exe文件的所在的工作目录问题 windows下python的keyboard库在锁屏之后再次登陆快捷键(热键)失效问题 pyinstaller 报错ImportError: No module named _bootlocale windows下gitbash 鼠标左键选中文字自动自行终止命令 ctrl+c ^C tomcat jndi context.xml的特殊字符转义问题 struts2的优缺点以及如何改造 jetty-maven-plugin 版本导致jetty启动失败问题 Eclipse下pom.xml的提示 Cannot access defaults field of Properties
spring-security 如何使用用户名或邮箱登录
pmh905001 · 2023-05-23 · via 博客园 - pmh905001

其他文章是介绍使用邮箱以及验证码登录的方式,如果变成如下这种通过用户名或者邮箱的登录方式呢? https://gitee.com/pmh905001/moikiitos/

spring-security只实现用了用户名+密码登录,没有邮箱+密码登录的方式该怎么进行扩展呢? 

翻了下源代码,做法如下:在spring-security.xml添加一个能够支持邮箱登录的bean,并且注入到:authentication-manager 下的 authentication-provider 

    <beans:bean id="userDetailsManager" class="com.hyxc.moikiitos.services.UserDetailsServiceImpl">
        <beans:property name="dataSource" ref="dataSource" />
    </beans:bean>
    
    <beans:bean id="encoder" class="org.springframework.security.crypto.password.StandardPasswordEncoder"/>
  
    <authentication-manager>
        <authentication-provider user-service-ref="userDetailsManager">
            <password-encoder ref="encoder" />
        </authentication-provider>
    </authentication-manager>

剩下的事情就是扩展 JdbcUserDetailsManager,只需要覆盖 loadUsersByUsername(String username)方法,让email字段也能作为查询条件即可。代码如下:

package com.hyxc.moikiitos.services;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.provisioning.JdbcUserDetailsManager;

public class UserDetailsServiceImpl extends JdbcUserDetailsManager {
    @Override
    protected List<UserDetails> loadUsersByUsername(String username) {
        return getJdbcTemplate().query("select username,password,enabled from users where username = ? or email = ?",
                new String[] { username, username }, new RowMapper<UserDetails>() {
                    public UserDetails mapRow(ResultSet rs, int rowNum) throws SQLException {
                        String username = rs.getString(1);
                        String password = rs.getString(2);
                        boolean enabled = rs.getBoolean(3);
                        return new User(username, password, enabled, true, true, true, AuthorityUtils.NO_AUTHORITIES);
                    }

                });

    }

}