1.自定义realm,在shiro的配置类中加入以下bean
1 2 3 4 5 6 7 8 9 |
/** * 身份认证 realm */ @bean public myshirorealm myshirorealm(){ myshirorealm myshirorealm = new myshirorealm(); system.out.println( "myshirorealm 注入成功" ); return myshirorealm; } |
2.重写方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
// 身份认证 @override protected authenticationinfo dogetauthenticationinfo(authenticationtoken authenticationtoken) throws authenticationexception { string username = (string) authenticationtoken.getprincipal(); system.out.println( "myshirorealm.....dogetauthenticationinfo" ); userinfo user= null ; try { user = iuserinfoservice.findbyusername(username); } catch (exception e){ e.printstacktrace(); } if (user== null ){ return null ; } // 进行验证,将正确数据讲给shiro处理 simpleauthenticationinfo authenticationinfo = new simpleauthenticationinfo( user, user.getpassword(), bytesource.util.bytes(user.getcredentialssalt()), // 加盐后的密码 getname() // 指定当前 realm 的类名 );
// 返回给安全管理器,由 securitymanager 比对密码的正确性 return authenticationinfo; } |
需要注意的是simpleauthenticationinfo 类,我们需要把数据交给他,格式为(用户,用户密码,盐,当前realm的类名)
1 2 3 4 5 6 7 |
// 进行验证,将正确数据讲给shiro处理 simpleauthenticationinfo authenticationinfo = new simpleauthenticationinfo( user, user.getpassword(), bytesource.util.bytes(user.getcredentialssalt()), // 加盐后的密码 getname() // 指定当前 realm 的类名 ); |
3.你还需要告诉shiro你是经过加密的,在config内新建如下bean
1 2 3 4 5 6 7 8 9 10 |
@bean public hashedcredentialsmatcher hashedcredentialsmatcher(){ hashedcredentialsmatcher hashedcredentialsmatcher = new hashedcredentialsmatcher(); // 使用md5 算法进行加密 hashedcredentialsmatcher.sethashalgorithmname( "md5" ); // 设置散列次数: 意为加密几次 hashedcredentialsmatcher.sethashiterations( 2 );
return hashedcredentialsmatcher; } |
并注册:
1 2 3 4 5 6 7 8 |
@bean public myshirorealm myshirorealm(){ myshirorealm myshirorealm = new myshirorealm(); // 配置 加密 (在加密后,不配置的话会导致登陆密码失败) myshirorealm.setcredentialsmatcher(hashedcredentialsmatcher()); //+++++++++++ system.out.println( "myshirorealm 注入成功" ); return myshirorealm; } |
总结
以上所述是小编给大家介绍的springboot整合shiro之加盐md5加密的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
原文链接:https://blog.csdn.net/qq_37163479/article/details/84752298
查看更多关于Springboot整合Shiro之加盐MD5加密的方法的详细内容...