shiro----加密算法

什么是加密算法和加盐?

在身份认证的过程中往往都会涉及到加密,如果不加密,这个时
候信息就会非常的不安全,shiro 中提供的算法比较多 如 MD5
SHA 等
使用 MD5 进行 ‘’1111 加密
b59c67bf196a4758191e42f76670ceba
进行加盐操作
1111+姓名=
加盐可以在上个基础上基础加盐,保证数据更加安全

基础使用

  1. 在ini配置文件中配置使用md5算法和加盐次数
[main]
#设置securityManager中realm
credentialsMatcher=org.apache.shiro.authc.credential.HashedCredentialsMatcher
credentialsMatcher.hashAlgorithmName=md5
credentialsMatcher.hashIterations=2

userRealm=com.bjsxt.shiro3.UserRealm
userRealm.credentialsMatcher=$credentialsMatcher
securityManager.realms=$userRealm

2.在SimpleAuthenticationInfo 中添加盐值

public class UserRealm  extends AuthorizingRealm {


    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {


        try {
            Class.forName("com.mysql.jdbc.Driver");

            Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/shiro", "root", "root");


            PreparedStatement prepareStatement = conn.prepareStatement("select  uname,pwd  from  admin ");


            ResultSet rs = prepareStatement.executeQuery();
            System.out.println(rs);

            while (rs.next()){
                //在3个参数加盐值,一般不在这写死,而是把盐值放在数据库的字段里
                SimpleAuthenticationInfo  info=new SimpleAuthenticationInfo(rs.getString("uname"),rs.getString("pwd"), ByteSource.Util.bytes("hrn"),"userRealm");

                return   info;

            }
        } catch (Exception e) {
            e.printStackTrace();
        }


        return null;
    }

posted @ 2021-08-09 15:19  是但啦  阅读(112)  评论(0)    收藏  举报