今天就跟大家聊聊有關(guān)使用shiro怎么實(shí)現(xiàn)多驗(yàn)證登錄,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
站在用戶的角度思考問題,與客戶深入溝通,找到中衛(wèi)網(wǎng)站設(shè)計(jì)與中衛(wèi)網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:成都網(wǎng)站制作、成都網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、空間域名、虛擬主機(jī)、企業(yè)郵箱。業(yè)務(wù)覆蓋中衛(wèi)地區(qū)。
1. 首先新建一個(gè)shiroConfig shiro的配置類,代碼如下:
@Configuration是標(biāo)識這個(gè)類是一個(gè)配置文件,在啟動(dòng)時(shí)會加載這個(gè)類里面的內(nèi)容,這個(gè)配置文件的位置的一定一定一定不能防止啟動(dòng)類外面的文件夾中,否則還會在啟動(dòng)類上加注解
@Bean是將這個(gè)類交給spring管理
@Configuration public class SpringShiroConfig { /** * @param realms 這兒使用接口集合是為了實(shí)現(xiàn)多驗(yàn)證登錄時(shí)使用的 * @return */ @Bean public SecurityManager securityManager(Collectionrealms) { DefaultWebSecurityManager sManager = new DefaultWebSecurityManager(); sManager.setRealms(realms); return sManager; } @Bean public ShiroFilterFactoryBean shiroFilterFactory(SecurityManager securityManager) { ShiroFilterFactoryBean sfBean = new ShiroFilterFactoryBean(); sfBean.setSecurityManager(securityManager); //如果是匿名訪問時(shí),訪問了不能訪問的資源跳轉(zhuǎn)的位置 sfBean.setLoginUrl("/index"); //定義map指定請求過濾規(guī)則(哪些資源允許匿名訪問,哪些必須認(rèn)證訪問) LinkedHashMap map = new LinkedHashMap<>(); //靜態(tài)資源允許匿名訪問:"anon" 靜態(tài)資源授權(quán)時(shí)不能寫static下面所有的開放,要將static下面的所有文件夾一個(gè)一個(gè)的開放,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"); //訪問這個(gè)路徑時(shí)不會進(jìn)入controller,會在這兒直接攔截退出,問為什么的,自己想請求流程去 map.put("/user/userLogout", "logout"); //攔截除上面之外的所有請求路徑 map.put("/**", "user"); sfBean.setFilterChainDefinitionMap(map); return sfBean; } @Bean public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() { return new LifecycleBeanPostProcessor(); }
2. 寫Realms的實(shí)現(xiàn)類,一般繼承自AuthorizingRealm(這個(gè)是實(shí)現(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才能有下面兩個(gè)方法*/ 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; } /** * 通過此方法完成認(rèn)證數(shù)據(jù)的獲取及封裝,系統(tǒng)底層會將認(rèn)證數(shù)據(jù)傳遞認(rèn)證管理器,有認(rèn)證管理器完成認(rèn)證操作 * @param authenticationToken * @return * @throws AuthenticationException */ @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException { //先判斷這個(gè)是否是來及這個(gè)令牌的數(shù)據(jù):我們這兒分為了UsernamePasswordToken(shiro給我們提供的。)、UserPhoneToken if (!(authenticationToken instanceof UsernamePasswordToken)) { return null; } //獲取controller傳過來的數(shù)據(jù) UsernamePasswordToken upToken = (UsernamePasswordToken) authenticationToken; //upToken.setRememberMe(true);shiro默認(rèn)為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這個(gè)類型才能使用 ByteSource credentialsSalt = ByteSource.Util.bytes(user.getSalt()); //這兒是將這個(gè)用戶的信息交給shiro(user為用戶對象,user.getPassword()是要加密的對象,credentialsSalt為鹽值,getName()當(dāng)前對象) SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, user.getPassword(), credentialsSalt, getName()); return info; } }
3. 此時(shí)用戶的賬號密碼登錄已經(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)在來實(shí)現(xiàn)短信驗(yàn)證碼登錄實(shí)現(xiàn):
4.1 先寫UserPhoneToken,我放在l和springShiroConfig同一目錄下:
@Component public class UserPhoneToken extends UsernamePasswordToken implements Serializable { private static final long serialVersionUID = 6293390033867929958L; // 手機(jī)號碼 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; } /** * 通過此方法完成認(rèn)證數(shù)據(jù)的獲取及封裝,系統(tǒng)底層會將認(rèn)證數(shù)據(jù)傳遞認(rèn)證管理器,有認(rèn)證管理器完成認(rèn)證操作 * @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ā)送驗(yàn)證碼是存入session中的驗(yàn)證碼和手機(jī)號 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ù)手機(jī)號查詢用戶信息 User user = userDao.findUserByUserPhone(phone); if (StringUtils.isEmpty(verificationCode)) { throw new ServiceException("網(wǎng)絡(luò)錯(cuò)誤"); } //比對手機(jī)號 if (!verificationCode.equals(verificationCode1)) { throw new ServiceException("驗(yàn)證碼不正確"); } if (user == null) { throw new UnknownAccountException(); } if (user.getState() == 0) { throw new LockedAccountException(); } return new SimpleAuthenticationInfo(user,phone,getName()); } }
4.3 手機(jī)號碼登錄驗(yàn)證已經(jīng)基本完成:controller代碼如下:
password為接收的驗(yàn)證碼
@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)這個(gè)問題是我的是因?yàn)镽ealm中的某個(gè)實(shí)現(xiàn)類沒有加注解,我這兒演示時(shí)是應(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的時(shí)候就發(fā)現(xiàn)這一句執(zhí)行后就保錯(cuò)
原因:是因?yàn)槲业腶pplication.yml文件中沒有寫dao對應(yīng)的mapper文件的路徑
3. 在ShioUserPhoneRealm的doGetAuthenticationInfo方法的new SimpleAuthenticationInfo(user,phone,getName())這個(gè)位置后就報(bào)錯(cuò)是應(yīng)為ShioUserPhoneRealm的這個(gè)方法中你沒有將new的對象設(shè)置為AllowAllCredentialsMatcher();
@Override public void setCredentialsMatcher(CredentialsMatcher credentialsMatcher) { //這兒的CredentialsMatcher的new的對象必須是AllowAllCredentialsMatcher CredentialsMatcher matcher = new AllowAllCredentialsMatcher(); super.setCredentialsMatcher(matcher); }
看完上述內(nèi)容,你們對使用shiro怎么實(shí)現(xiàn)多驗(yàn)證登錄有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。