密码加密登陆是为了提升系统安全性,即便是管理员查看数据库也得不到密码web
使用shiro能够很轻松的完成加密及登陆操做算法
1:加密工具数据库
此工具用于注册时对密码进行加密apache
public static final String md5(String password, String salt){ //加密方式 String hashAlgorithmName = "MD5"; //盐:为了即便相同的密码不一样的盐加密后的结果也不一样 ByteSource byteSalt = ByteSource.Util.bytes(salt); //密码 Object source = password; //加密次数 int hashIterations = 1024; SimpleHash result = new SimpleHash(hashAlgorithmName, source, byteSalt, hashIterations); return result.toString(); }
测试一下安全
public static void main(String[] args) { String password = md5("123456", "WHLH"); System.out.println(password); //加密后的结果 //3bcbb857c763d1429a24959cb8de2593 }
2:使用shiro登陆ide
Realm类工具
@Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) { UsernamePasswordToken token=(UsernamePasswordToken) authenticationToken; String username = token.getUsername(); //查询用户信息 User user=userService.findByUsername(username); //取出盐并编码 ByteSource salt = ByteSource.Util.bytes(user.getSalt()); return new SimpleAuthenticationInfo(username, user.getPassword(),salt, getName()); }
3:修改自定义realm配置:加密算法和加密次数要和加密工具参数保持一致测试
<bean id="myRealm" class="cn.jaffreyen.web.shiro.MyRealm"> <property name="credentialsMatcher"> <bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher"> <!-- 加密算法 --> <property name="hashAlgorithmName" value="MD5"></property> <!-- 加密次数 --> <property name="hashIterations" value="1024"></property> </bean> </property> </bean>
数据库编码
完成加密