Shiro框架使用使用自定义的登陆验证方法实现校验------Shiro框架

package com.alatus.shirotest;

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.realm.AuthenticatingRealm;
import org.apache.shiro.util.ByteSource;

public class MyRealm extends AuthenticatingRealm {
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
//        自定义的登录验证方法
//        Shiro的login方法底层会调用这个类的认证方法进行认证
//        需要配置让自定义的realm生效,可以在imi文件中配置,也可以在spring的ioc容器配置
//        该方法只是获取进行对比的信息,认证逻辑还是Shiro底层的认证逻辑
//        获取身份信息
        String username = authenticationToken.getPrincipal().toString();
        System.out.println(username);
//        获取凭证信息
        String password = authenticationToken.getCredentials().toString();
        System.out.println(password);
//        访问数据库获取存储的信息
//        假设数据库我们找到只有一个叫zhangsan的用户
        if(username.equals("zhangsan")){
//            从数据库查询加密后的密文
            String pwd = "2c72932f1de524f5a0062c288847f97b";
//            封装到校验逻辑对象
            AuthenticationInfo info = new SimpleAuthenticationInfo(
                    authenticationToken.getPrincipal(),
                    pwd,
                    ByteSource.Util.bytes("salt"),
                    authenticationToken.getPrincipal().toString()
                    );
            //        返回数据
            return info;
        }
        return null;
    }
}
package com.alatus.shirotest;

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.realm.AuthenticatingRealm;
import org.apache.shiro.util.ByteSource;

public class MyRealm extends AuthenticatingRealm {
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
//        自定义的登录验证方法
//        Shiro的login方法底层会调用这个类的认证方法进行认证
//        需要配置让自定义的realm生效,可以在imi文件中配置,也可以在spring的ioc容器配置
//        该方法只是获取进行对比的信息,认证逻辑还是Shiro底层的认证逻辑
//        获取身份信息
        String username = authenticationToken.getPrincipal().toString();
        System.out.println(username);
//        获取凭证信息
        String password = authenticationToken.getCredentials().toString();
        System.out.println(password);
//        访问数据库获取存储的信息
//        假设数据库我们找到只有一个叫zhangsan的用户
        if(username.equals("zhangsan")){
//            从数据库查询加密后的密文
            String pwd = "2c72932f1de524f5a0062c288847f97b";
//            封装到校验逻辑对象
            AuthenticationInfo info = new SimpleAuthenticationInfo(
                    authenticationToken.getPrincipal(),
                    pwd,
                    ByteSource.Util.bytes("salt"),
                    authenticationToken.getPrincipal().toString()
                    );
            //        返回数据
            return info;
        }
        return null;
    }
}
[users]
zhangsan=2c72932f1de524f5a0062c288847f97b,role1,role2
lisi=123

[roles]
role1=user:insert,user:select

[main]
md5CredentialsMatcher=org.apache.shiro.authc.credential.Md5CredentialsMatcher
md5CredentialsMatcher.hasIterations=3

myrealm=com.alatus.shirotest.MyRealm
myrealm.credentialsMatcher=$md5CredentialsMatcher
securityManager.realms=$myrealm
[users]
zhangsan=2c72932f1de524f5a0062c288847f97b,role1,role2
lisi=123

[roles]
role1=user:insert,user:select

[main]
md5CredentialsMatcher=org.apache.shiro.authc.credential.Md5CredentialsMatcher
md5CredentialsMatcher.hasIterations=3

myrealm=com.alatus.shirotest.MyRealm
myrealm.credentialsMatcher=$md5CredentialsMatcher
securityManager.realms=$myrealm
  • 13
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值