溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用shiro怎么實現(xiàn)多驗證登錄

發(fā)布時間:2021-06-17 14:24:40 來源:億速云 閱讀:261 作者:Leah 欄目:編程語言

今天就跟大家聊聊有關(guān)使用shiro怎么實現(xiàn)多驗證登錄,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

1. 首先新建一個shiroConfig shiro的配置類,代碼如下:

@Configuration是標(biāo)識這個類是一個配置文件,在啟動時會加載這個類里面的內(nèi)容,這個配置文件的位置的一定一定一定不能防止啟動類外面的文件夾中,否則還會在啟動類上加注解

@Bean是將這個類交給spring管理

@Configuration
public class SpringShiroConfig {


  /**
   * @param realms 這兒使用接口集合是為了實現(xiàn)多驗證登錄時使用的
   * @return
   */
  @Bean
  public SecurityManager securityManager(Collection<Realm> realms) {
    DefaultWebSecurityManager sManager = new DefaultWebSecurityManager();
    sManager.setRealms(realms);
    return sManager;
  }

  @Bean
  public ShiroFilterFactoryBean shiroFilterFactory(SecurityManager securityManager) {
    ShiroFilterFactoryBean sfBean = new ShiroFilterFactoryBean();
    sfBean.setSecurityManager(securityManager);
    //如果是匿名訪問時,訪問了不能訪問的資源跳轉(zhuǎn)的位置
    sfBean.setLoginUrl("/index");
    //定義map指定請求過濾規(guī)則(哪些資源允許匿名訪問,哪些必須認證訪問)
    LinkedHashMap<String, String> map = new LinkedHashMap<>();
    //靜態(tài)資源允許匿名訪問:"anon" 靜態(tài)資源授權(quán)時不能寫static下面所有的開放,要將static下面的所有文件夾一個一個的開放,templates同理
    //map的key可以為文件的位置,也可以為請求的路徑
    map.put("/bower_components/**", "anon");
    map.put("/json/**", "anon");
    map.put("/pages", "anon");
    map.put("/user/userPasswordLogin", "anon");
    map.put("/user/login", "anon");
    map.put("/user/reg", "anon");
    //訪問這個路徑時不會進入controller,會在這兒直接攔截退出,問為什么的,自己想請求流程去
    map.put("/user/userLogout", "logout");
    //攔截除上面之外的所有請求路徑
    map.put("/**", "user");
    sfBean.setFilterChainDefinitionMap(map);
    return sfBean;
  }

  @Bean
  public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
    return new LifecycleBeanPostProcessor();
  }

2. 寫Realms的實現(xiàn)類,一般繼承自AuthorizingRealm(這個是實現(xiàn)用戶名,密碼登錄),代碼如下:

@Service
public class ShioUserRealm extends AuthorizingRealm {

  //注入userdao
  @Autowired
  private UserDao userDao;
  /**
   * 設(shè)置憑證匹配器
   *
   * @param credentialsMatcher
   */
  @Override
  public void setCredentialsMatcher(CredentialsMatcher credentialsMatcher) {
    /*這里設(shè)置了MD5鹽值加密,這兒就必須使用HashedCredentialsMatcher才能有下面兩個方法*/
    HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();
    //這里是設(shè)置加密方式
    matcher.setHashAlgorithmName("MD5");
    //這里是設(shè)置加密的次數(shù)
    matcher.setHashIterations(2);
    super.setCredentialsMatcher(matcher);
  }

  /**
   * 這兒是設(shè)置授權(quán)的
   * @param principalCollection
   * @return
   */
  @Override
  protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {

    return null;
  }

  /**
   * 通過此方法完成認證數(shù)據(jù)的獲取及封裝,系統(tǒng)底層會將認證數(shù)據(jù)傳遞認證管理器,有認證管理器完成認證操作
   * @param authenticationToken
   * @return
   * @throws AuthenticationException
   */
  @Override
  protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

    //先判斷這個是否是來及這個令牌的數(shù)據(jù):我們這兒分為了UsernamePasswordToken(shiro給我們提供的。)、UserPhoneToken
    if (!(authenticationToken instanceof UsernamePasswordToken)) {
      return null;
    }
    //獲取controller傳過來的數(shù)據(jù)
    UsernamePasswordToken upToken = (UsernamePasswordToken) authenticationToken;
    //upToken.setRememberMe(true);shiro默認為false,是是否記住我的功能
    //這兒為用戶提交的username
    String username = upToken.getUsername();
    //去數(shù)據(jù)更加name取到用戶的信息
    User user = userDao.findUserByUserName(username);
    //判斷數(shù)據(jù)庫是否有這用戶
    if (user == null) {
      throw new UnknownAccountException();
    }
    //判斷用戶的狀態(tài)是否被禁用(數(shù)據(jù)庫的字段)
    if (user.getState() == 0) {
      throw new LockedAccountException();
    }
    //這兒是取到用戶信息中的鹽值,鹽值要轉(zhuǎn)換為ByteSource這個類型才能使用
    ByteSource credentialsSalt = ByteSource.Util.bytes(user.getSalt());
    //這兒是將這個用戶的信息交給shiro(user為用戶對象,user.getPassword()是要加密的對象,credentialsSalt為鹽值,getName()當(dāng)前對象)
    SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, user.getPassword(), credentialsSalt, getName());
    return info;
  }
}

3. 此時用戶的賬號密碼登錄已經(jīng)可以使用了controller代碼如下:

@RequestMapping("userPasswordLogin")
  @ResponseBody
  public JsonResult userPasswordLogin(String username, String password) {
    Subject subject = SecurityUtils.getSubject();
    UsernamePasswordToken token = new UsernamePasswordToken(username, password);
    subject.login(token);
    return new JsonResult("login Ok");
  }

4. 我們現(xiàn)在來實現(xiàn)短信驗證碼登錄實現(xiàn):

4.1 先寫UserPhoneToken,我放在l和springShiroConfig同一目錄下:

@Component
public class UserPhoneToken extends UsernamePasswordToken implements Serializable {

  private static final long serialVersionUID = 6293390033867929958L;
  // 手機號碼
  private String phoneNum;
  //無參構(gòu)造
  public UserPhoneToken(){}
  
  //獲取存入的值
  @Override
  public Object getPrincipal() {
    if (phoneNum == null) {
      return getUsername();
    } else {
      return getPhoneNum();
    }
  }

  @Override
  public Object getCredentials() {
    if (phoneNum == null) {
      return getPassword();
    }else {
      return "ok";
    }

  }

  public UserPhoneToken(String phoneNum) {
    this.phoneNum = phoneNum;
  }

  public UserPhoneToken(final String userName, final String password) {
    super(userName, password);
  }

  public String getPhoneNum() {
    return phoneNum;
  }

  public void setPhoneNum(String phoneNum) {
    this.phoneNum = phoneNum;
  }
  @Override
  public String toString() {
    return "PhoneToken [PhoneNum=" + phoneNum + "]";
  }

}

4.2 在寫shiroUserPhoneRealm,代碼如下:

@Service
public class ShioUserPhoneRealm extends AuthorizingRealm {

  @Autowired
  private UserDao userDao;

  @Override
  public void setCredentialsMatcher(CredentialsMatcher credentialsMatcher) {
    //這兒的CredentialsMatcher的new的對象必須是AllowAllCredentialsMatcher
    CredentialsMatcher matcher = new AllowAllCredentialsMatcher();
    super.setCredentialsMatcher(matcher);
  }

  @Override
  protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
    return null;
  }

  /**
   * 通過此方法完成認證數(shù)據(jù)的獲取及封裝,系統(tǒng)底層會將認證數(shù)據(jù)傳遞認證管理器,有認證管理器完成認證操作
   * @param authenticationToken
   * @return
   * @throws AuthenticationException
   */
  @Override
  protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

    UserPhoneToken token = null;
    if (authenticationToken instanceof UserPhoneToken) {
      token = (UserPhoneToken) authenticationToken;
    }else {
      return null;
    }
    //獲取我發(fā)送驗證碼是存入session中的驗證碼和手機號
    String verificationCode = (String) SecurityUtils.getSubject().getSession().getAttribute("verificationCode");
    String phone = (String) SecurityUtils.getSubject().getSession().getAttribute("phone");
    //獲取controller傳過來的數(shù)據(jù)
    String verificationCode1 = (String) token.getPrincipal();
    //去數(shù)據(jù)庫根據(jù)手機號查詢用戶信息
    User user = userDao.findUserByUserPhone(phone);
    if (StringUtils.isEmpty(verificationCode)) {
      throw new ServiceException("網(wǎng)絡(luò)錯誤");
    }
    //比對手機號
    if (!verificationCode.equals(verificationCode1)) {
      throw new ServiceException("驗證碼不正確");
    }
    if (user == null) {
      throw new UnknownAccountException();
    }
    if (user.getState() == 0) {
      throw new LockedAccountException();
    }
    return new SimpleAuthenticationInfo(user,phone,getName());
  }
}

4.3 手機號碼登錄驗證已經(jīng)基本完成:controller代碼如下:

password為接收的驗證碼

@PostMapping("verificationCodeLogin")
  @ResponseBody
  public JsonResult verificationCodeLogin(String password) {
    Subject subject = SecurityUtils.getSubject();
    UserPhoneToken token = new UserPhoneToken(password);
    subject.login(token);
    return new JsonResult("login OK");
  }

使用過程中遇到的bug

1.

org.apache.shiro.authc.UnknownAccountException: Realm [cn.tedu.wxacs.service.impl.ShioUserPhoneRealm@768d8431] was unable to find account data for the submitted AuthenticationToken [org.apache.shiro.authc.UsernamePasswordToken - 張三, rememberMe=false].

出現(xiàn)這個問題是我的是因為Realm中的某個實現(xiàn)類沒有加注解,我這兒演示時是應(yīng)為ShiroUserRealm為加@Service注解

2.

org.apache.shiro.authc.AuthenticationException: Authentication token of type [class org.apache.shiro.authc.UsernamePasswordToken] could not be authenticated by any configured realms. Please ensure that at least one realm can authenticate these tokens.

這兒出現(xiàn)的問題是應(yīng)為我的ShioUserRealm的AuthenticationInfo方法的User user = userDao.findUserByUserName(username);這行代碼出現(xiàn)的問題,debug的時候就發(fā)現(xiàn)這一句執(zhí)行后就保錯

原因:是因為我的application.yml文件中沒有寫dao對應(yīng)的mapper文件的路徑

3. 在ShioUserPhoneRealm的doGetAuthenticationInfo方法的new SimpleAuthenticationInfo(user,phone,getName())這個位置后就報錯是應(yīng)為ShioUserPhoneRealm的這個方法中你沒有將new的對象設(shè)置為AllowAllCredentialsMatcher();

@Override
  public void setCredentialsMatcher(CredentialsMatcher credentialsMatcher) {
    //這兒的CredentialsMatcher的new的對象必須是AllowAllCredentialsMatcher
    CredentialsMatcher matcher = new AllowAllCredentialsMatcher();
    super.setCredentialsMatcher(matcher);
  }

看完上述內(nèi)容,你們對使用shiro怎么實現(xiàn)多驗證登錄有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI