javascript
Spring Security简单增加短信验证码登录
查網上資料增加短信驗證碼登錄都要增加一大推,要重頭寫Spring Security的實現,我呢,只想在原來的密碼登錄基礎上簡單實現一下短信驗證碼登錄。
1、首先得先一個認證類,來認證驗證碼是否正確,這個類要實現Spring Security提供的AuthenticationProvider接口
2、其次需要一個認證令牌的類,作為你的認證信息,這個類要繼承Spring Security提供的AbstractAuthenticationToken抽象類
3、然后要把你的認證類加到spring security配置中,就是繼承WebSecurityConfigurerAdapter的類
4、最后就是登陸時調用Spring Security的認證了
上代碼:
1、認證類
UserDetailsServiceImpl類 這里面的東西就不固定了 想咋寫就咋寫 主要是loadUserByUsername方法 這個必須有 返回的UserDetails(Spring Security提供的用來存放用戶信息的類,你可以隨便寫個類實現這個類,然后你就可以存放你自己要用的信息了)loadUserByUsername這個方法會在調用Spring Security的認證時用 我會在代碼中表明在哪塊會調用到這個類
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.stereotype.Service;/*** 用戶驗證處理** @author ruoyi*/ @Service public class UserDetailsServiceImpl implements UserDetailsService {private static final Logger log = LoggerFactory.getLogger(UserDetailsServiceImpl.class);@Autowiredprivate ISysUserService userService;@Autowiredprivate SysPermissionService permissionService;@Overridepublic UserDetails loadUserByUsername(String username) throws UsernameNotFoundException{SysUser user = userService.selectUserByUserNameOrPhone(username);if (StringUtils.isNull(user)){log.info("登錄用戶:{} 不存在.", username);throw new ServiceException("登錄用戶:" + username + " 不存在");}else if (UserStatus.DELETED.getCode().equals(user.getDelFlag())){log.info("登錄用戶:{} 已被刪除.", username);throw new ServiceException("對不起,您的賬號:" + username + " 已被刪除");}else if (UserStatus.DISABLE.getCode().equals(user.getStatus())){log.info("登錄用戶:{} 已被停用.", username);throw new ServiceException("對不起,您的賬號:" + username + " 已停用");}return createLoginUser(user);}public UserDetails createLoginUser(SysUser user){return new LoginUser(user.getUserId(), user.getDeptId(), user.getClinicId(), user, permissionService.getMenuPermission(user));} }2、存放認證令牌
import org.springframework.security.authentication.AbstractAuthenticationToken; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.SpringSecurityCoreVersion;import java.util.Collection;public class SmsCodeAuthenticationToken extends AbstractAuthenticationToken {private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;private final Object principal; //存放認證信息,認證之前存放手機號,認證之后存放登錄的用戶private Object credentials;public SmsCodeAuthenticationToken(String mobile, Object credentials) {super(null);this.principal = mobile;this.credentials = credentials;this.setAuthenticated(false);}public SmsCodeAuthenticationToken(Object principal, Object credentials, Collection<? extends GrantedAuthority> authorities) {super(authorities);this.principal = principal;this.credentials = credentials;super.setAuthenticated(true);}public Object getCredentials() {return this.credentials;}public Object getPrincipal() {return this.principal;}public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {if (isAuthenticated) {throw new IllegalArgumentException("Cannot set this token to trusted - use constructor which takes a GrantedAuthority list instead");} else {super.setAuthenticated(false);}}public void eraseCredentials() {super.eraseCredentials();this.credentials = null;} }3、spring security配置
4、登錄調用認證
總結
以上是生活随笔為你收集整理的Spring Security简单增加短信验证码登录的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ant design图片hover蒙层
- 下一篇: 安排电影院座位--贪心算法