Shiro-认证(自定义Realm和散列密码)

Shiro-认证

认证

身份认证,就是判断一个用户是否为合法用户的处理过程;
通过核对用户输入的用户名和口令,看其是否与系统中存储的该用户的用户名和口令一致,来判断用户身份是否正确;

关键对象

Subject:主体
    用户
Principal:身份信息
    是主体(subject)进行身份认证的标识,标识必须具有唯一性,如用户名、手机号、邮箱地址等
credential:凭证信息
    是只有主体自己知道的安全信息,如密码、证书等。

使用ini来完成认证

1、创建maven工程

2、在Maven中添加依赖的jar包

<dependencies>

      <!-- https://mvnrepository.com/artifact/org.apache.shiro/shiro-core -->
      <dependency>
          <groupId>org.apache.shiro</groupId>
          <artifactId>shiro-core</artifactId>
          <version>1.4.1</version>
      </dependency>

      <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
      <dependency>
          <groupId>commons-logging</groupId>
          <artifactId>commons-logging</artifactId>
          <version>1.2</version>
      </dependency>

      <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-nop -->
      <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-nop</artifactId>
          <version>1.7.28</version>
          <scope>test</scope>
      </dependency>


  </dependencies>

3.创建MyRealm类(自定义Realm 处理认证和授权)

package com.longhe;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

import java.util.ArrayList;

public class MyRealm extends AuthorizingRealm {

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

        /*判断当前用户是否存在*/
        /*获取用户名*/
        String username = (String)token.getPrincipal();
        /*从数据库中查出用户名和密码 真实要从数据库中获取*/
        String name = "helong";
        String pwd = "250135413be50fe9be4b78c75e02dcc2";/*存放数据的时候也要存入这种格式*/

        /*判断身份信息*/
        if(!name.equals(username)){
            return null;
        }

        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(
                username,
                pwd,
                ByteSource.Util.bytes("helong"),/*盐添加的位置*/
                this.getName()
        );

        return info;
    }


    /*授权*/
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {

        /*获取当前身份信息*/
        Object primaryPrincipal = principals.getPrimaryPrincipal();

        /*假设 用户的角色 权限 -->实际从数据库中查询出来的*/
        ArrayList<String> roles = new ArrayList<>();
        roles.add("role1");
        roles.add("role2");
        roles.add("role3");

        /*假设 用户权限 -->实际从数据库中查询出来的*/
        ArrayList<String> permission = new ArrayList<>();
        permission.add("user:create");
        permission.add("user:delete");

        /*把角色和权限添加到授权当中*/
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        info.addRoles(roles);
        info.addStringPermissions(permission);

        return info;
    }


}

4.验证权限

        /*1.构建securityManager工厂 ini 加载配置文件*/
        IniSecurityManagerFactory factory = new IniSecurityManagerFactory("classpath:shiro.ini");
        /*2.通过工厂创建securityManager*/
        SecurityManager securityManager = factory.getInstance();
        /* 3.将securityManager设置到运行环境中*/
        SecurityUtils.setSecurityManager(securityManager);
        /*4.创建一个Subject实例*/
        Subject subject = SecurityUtils.getSubject();
        /*5.创建token令牌  这个是用户登录的时候输入的信息*/
        UsernamePasswordToken token = new UsernamePasswordToken("helong", "1234");
        /* 6.用户登录*/
        try {
            /*这个时候会调用自己创建的MyRealm来进行登录认证*/
            subject.login(token);
        } catch (UnknownAccountException e) {
            System.out.println("账号不存在");
            e.printStackTrace();
        }catch (IncorrectCredentialsException e){
            System.out.println("密码不正确");
            e.printStackTrace();
        }
        System.out.println("是否认证成功:"+subject.isAuthenticated());
        /*7.用户退出*/
        subject.logout();
        System.out.println("是否认证成功:"+subject.isAuthenticated());

5.在资源文件夹下配置ini文件(进行散列:将登录界面的信息进行加密散列处理,然后再在内部调用MyRealm进行认证)

[main]
#定义凭证匹配器
credentialsMatcher=org.apache.shiro.authc.credential.HashedCredentialsMatcher
#散列算法
credentialsMatcher.hashAlgorithmName=md5
#散列次数
credentialsMatcher.hashIterations=2
#指定realm
myRealm=com.longhe.MyRealm
#配置散列
myRealm.credentialsMatcher=$credentialsMatcher
#配置自定义散列
securityManager.realms=$myRealm

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值