How to implement multiple ways of logging in with shiro?
The Shiro framework offers various methods to implement user login, including the following popular ways:
- “Login with username and password: The user enters their username and password, which are then verified in the background. If the validation is successful, the user will be logged in.”
- The code creates a token with the username and password, then uses it to log in the subject.
- Login with phone number verification code: User inputs phone number and verification code, backend system verifies the phone number and code, and upon successful verification, login is completed.
- Create a SMSCodeToken using the mobile number and code, then log in using that token.
- Third-party login (such as WeChat login, QQ login): Users click on the third-party login button, are redirected to the third-party login page, and upon successful login, receive an authorization code. The backend then uses this authorization code to obtain user information, verify it, and allow the user to successfully log in.
- Login the ‘subject’ using a new OAuth2 token created with the given code.
- Single Sign-On (SSO): Once a user successfully logs into one system, they can access other systems without needing to log in again, allowing for login sharing across multiple systems.
- Create a new object of PrincipalCollection class called principals with the user and realmName, then set it as an attribute in the session of the current subject using the key PRINCIPALS_SESSION_KEY from the DefaultSubjectContext class.
The above are some common implementations of Shiro’s multiple login methods, with specific implementations varying depending on the situation. In actual applications, you can choose a suitable method according to your own needs and proceed with the corresponding configuration and development.