javascript
Spring Security简单增加短信验证码登录
查網(wǎng)上資料增加短信驗(yàn)證碼登錄都要增加一大推,要重頭寫Spring Security的實(shí)現(xiàn),我呢,只想在原來的密碼登錄基礎(chǔ)上簡(jiǎn)單實(shí)現(xiàn)一下短信驗(yàn)證碼登錄。
1、首先得先一個(gè)認(rèn)證類,來認(rèn)證驗(yàn)證碼是否正確,這個(gè)類要實(shí)現(xiàn)Spring Security提供的AuthenticationProvider接口
2、其次需要一個(gè)認(rèn)證令牌的類,作為你的認(rèn)證信息,這個(gè)類要繼承Spring Security提供的AbstractAuthenticationToken抽象類
3、然后要把你的認(rèn)證類加到spring security配置中,就是繼承WebSecurityConfigurerAdapter的類
4、最后就是登陸時(shí)調(diào)用Spring Security的認(rèn)證了
上代碼:
1、認(rèn)證類
UserDetailsServiceImpl類 這里面的東西就不固定了 想咋寫就咋寫 主要是loadUserByUsername方法 這個(gè)必須有 返回的UserDetails(Spring Security提供的用來存放用戶信息的類,你可以隨便寫個(gè)類實(shí)現(xiàn)這個(gè)類,然后你就可以存放你自己要用的信息了)loadUserByUsername這個(gè)方法會(huì)在調(diào)用Spring Security的認(rèn)證時(shí)用 我會(huì)在代碼中表明在哪塊會(huì)調(diào)用到這個(gè)類
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;/*** 用戶驗(yàn)證處理** @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("對(duì)不起,您的賬號(hào):" + username + " 已被刪除");}else if (UserStatus.DISABLE.getCode().equals(user.getStatus())){log.info("登錄用戶:{} 已被停用.", username);throw new ServiceException("對(duì)不起,您的賬號(hào):" + username + " 已停用");}return createLoginUser(user);}public UserDetails createLoginUser(SysUser user){return new LoginUser(user.getUserId(), user.getDeptId(), user.getClinicId(), user, permissionService.getMenuPermission(user));} }2、存放認(rèn)證令牌
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; //存放認(rèn)證信息,認(rèn)證之前存放手機(jī)號(hào),認(rèn)證之后存放登錄的用戶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、登錄調(diào)用認(rèn)證
總結(jié)
以上是生活随笔為你收集整理的Spring Security简单增加短信验证码登录的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ant design图片hover蒙层
- 下一篇: JS实现视频试看提示付费功能 手机端