shiro多驗證登錄代碼實例及問題解決
這篇文章主要介紹了shiro多驗證登錄代碼實例及問題解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
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ī)則(哪些資源允許匿名訪問,哪些必須認(rèn)證訪問) 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; } /** * 通過此方法完成認(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 { //先判斷這個是否是來及這個令牌的數(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這個類型才能使用 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; } /** * 通過此方法完成認(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ā)送驗證碼是存入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); }
注解中有一些需要注意的地方,建議看看,注解不對的地方還希望在下放評論指出或者聯(lián)系我
應(yīng)為我的知識有限,此方法本人實現(xiàn)目前沒有問題,其中有什么不對的地方還希望各位指出,謝謝!
使用的是jdk8,spring boot 的2.2.1版本,shiro的1,.4.1版本
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- spring boot 集成 shiro 自定義密碼驗證 自定義freemarker標(biāo)簽根據(jù)權(quán)限渲染不同頁面(推薦
- Java中SSM+Shiro系統(tǒng)登錄驗證碼的實現(xiàn)方法
- Java中基于Shiro,JWT實現(xiàn)微信小程序登錄完整例子及實現(xiàn)過程
- spring boot整合Shiro實現(xiàn)單點登錄的示例代碼
- springmvc+shiro+maven 實現(xiàn)登錄認(rèn)證與權(quán)限授權(quán)管理
- shiro之記住登錄信息
- 關(guān)于Apache shiro實現(xiàn)一個賬戶同一時刻只有一個人登錄(shiro 單點登錄)
- Shiro 控制并發(fā)登錄人數(shù)限制及登錄踢出的實現(xiàn)代碼
相關(guān)文章
springcloud+nacos實現(xiàn)灰度發(fā)布示例詳解
這篇文章主要介紹了springcloud+nacos實現(xiàn)灰度發(fā)布,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-08-08maven多個plugin相同phase的執(zhí)行順序
這篇文章主要介紹了maven多個plugin相同phase的執(zhí)行順序,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12Mybatis攔截器注解@Intercepts與@Signature注解使用
本文主要介紹了Mybatis攔截器注解@Intercepts與@Signature注解使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07JavaMail實現(xiàn)發(fā)送超文本(html)格式郵件的方法
這篇文章主要介紹了JavaMail實現(xiàn)發(fā)送超文本(html)格式郵件的方法,實例分析了java發(fā)送超文本文件的相關(guān)技巧,需要的朋友可以參考下2015-05-05