How to log in by email instead of username in Spring security

后端 未结 2 564
孤城傲影
孤城傲影 2021-02-04 22:15

I use Spring security. Is there a way to log in using email instead of a username in Spring security?

2条回答
  •  北海茫月
    2021-02-04 22:51

    The simplest way is just regard email as username.

    For many cases, email contains your username. For example: abc@gmail.com. In this email, abc is your username.

    So you just need to let the user input email directly.

    The only thing you need to update is change your query SQL similar as below:

    Change (query by username):

    SELECT username,password FROM users WHERE username=#{username}
    

    to (query by email)

    SELECT email AS username,password FROM users WHERE email=#{username}
    

提交回复
热议问题