shiro 不完全学习笔记-----------1

一.shiro 简介

                   shiro是apache的一个开源的安全框架,大体功能:认证,授权,企业会话管理,安全加密,缓存管理

                   shiro和springScurity的比较

                            简单                                               复杂

                            灵活                                               笨重

                         粒度较粗                                         粒度更细

                      功能相对简单                                    功能多而全

                       可脱离spring                                  不可脱离spring


二.shiro  整体架构

二.认证流程

创建securityManager-->主体提交认证-->securityManager--->调用authentictorren认证---->通过realm获取信息--->认证完成

SimpleAccountRealm sa = new SimpleAccountRealm();

     @Before
public void before()
{
sa.addAccount("admin", "admin");
}
/**
* 认证流程
*/
@Test
public void test()
{
//1.构建securityManager环境
DefaultSecurityManager dsm = new DefaultSecurityManager();
//1.1  设置realm
dsm.setRealm(sa);

//2.securityUtils 设置securityManager环境
SecurityUtils.setSecurityManager(dsm);
//2.1获得主体对象
Subject subject = SecurityUtils.getSubject();

//3.主体提交认证请求
UsernamePasswordToken token = new UsernamePasswordToken("admin","admin");
subject.login(token);
boolean b = subject.isAuthenticated();
System.out.println(b);

}

 登出     subject.logout();

二.授权流程

 创建securityManger ----->主体请求授权------->securityManager------>调用authentizer授权----->掉用raelm获取授权信息---->授权完成


                         是否拥有这一个信息                               是否和权限信息所匹配 

                subject.checkRole("manager");               subject.checkRoles("manager");

二. iniRealm&&JdbcRealm

 iniRealm:   配置文件的形式

 JdbcRealm:   

     1.设置数据源

BasicDataSource dataSouce = new BasicDataSource(); 
{
dataSouce.setUsername("root");
dataSouce.setPassword("528106");
dataSouce.setUrl("jdbc:mysql:///sm");
dataSouce.setDriverClassName("com.mysql.jdbc.Driver");
}

       2.加载jdbcRealm

                JdbcRealm realm = new JdbcRealm();
                realm.setDataSource(dataSouce);

       3.//设置查询权限的开关   默认是false不开启

 realm.setPermissionsLookupEnabled(true);(切记

              使用默认的sql的话  你的表是这样的:


   接下来和之前一样  构建scurityManager       通过subjectUtils获得主体  用主体认证和授权操作

   4.自定义sql语句

                //1.2设置查询语句
String sql = "select password from users where username=?";
realm.setAuthenticationQuery(sql);

  

三. 自定义realm

                            类  extends AuthorizingRealm

       /**
* authentiction   认证
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
String  username = (String) token.getPrincipal();

String password = getPasswordByUsername(username);

            //  用用户名去数据库查询密码   交给simpleAuthentiction  进行验证

if(password == null)
{
return null;
}
SimpleAuthenticationInfo sai = new SimpleAuthenticationInfo(username,password,"customRealm");
return sai;
}

       /**
* authorization 授权
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
SimpleAuthorizationInfo sazi = new SimpleAuthorizationInfo();
sazi.setRoles(roles);                                      //roles   根据用户名查出来的角色集合(set)
sazi.setStringPermissions(stringPermissions);      //permissions   根据xxx查出来的权限集合(set)
return null;
}


三. shiro加密  

     hashCredentialsMatcher

     //创建加密对象
    HashedCredentialsMatcher hcl = new HashedCredentialsMatcher("md5");
    //设置加密次数
    hcl.setHashIterations(1);
    //传入到realm中

    cr.setCredentialsMatcher(hcl);

        数据库里存的是加密后的字符,而你还是用你的密码去登陆   工具给你转成MD5后和数据库里的进行比对

加salt(盐)

在自定义的realm中   认证的时候 返回simpleAuthenticationinfo对象之前  摄入盐

SimpleAuthenticationInfo sai = new SimpleAuthenticationInfo(username,password,"customRealm");
//射进去
sai.setCredentialsSalt(ByteSource.Util.bytes("salt"));

                                                                                                         2018-5-22    22:37

猜你喜欢

转载自blog.csdn.net/weixin_41253479/article/details/80402989