Shiro自定义Ream

  1. 项目目录结构
    这里写图片描述
    2.代码:
package com.sun.shiro;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.realm.Realm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * 自定义realm的实现
 * @author suntan
 * @dade 2017年3月3日下午7:27:42
 * @version v1.0
 */
public class MyRealmDemo1 implements Realm{

    /**
     * 日志
     */
    private Logger logger = LoggerFactory.getLogger(MyRealmDemo1.class);

    public String getName() {

        return "myReamDemo1";
    }

    public boolean supports(AuthenticationToken token) {

        return token instanceof UsernamePasswordToken;
    }

    public AuthenticationInfo getAuthenticationInfo(AuthenticationToken token)  throws AuthenticationException {
        //获取用户名和密码
        String userName = (String) token.getPrincipal();
        //String passWord = (String) token.getCredentials();
        String passWord = new String((char[])token.getCredentials());
        if(!StringUtils.equals(userName, "sunny")) {
            logger.info("=======>用户名错误");
            throw new UnknownAccountException();
        }
        if(!StringUtils.equals(passWord, "1127")) {
            logger.info("====>密码错误");
            throw new IncorrectCredentialsException();
        }

        //用户名密码都对
        return new SimpleAuthenticationInfo(userName, passWord, getName());
    }



}

3.测试代码

package com.sun.shiro;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MyRealmDemo1Test {

    /**
     * 日志
     */
    private Logger logger = LoggerFactory.getLogger(MyRealmDemo1Test.class);

    @Test
    public void testGetAuthenticationInfo() {
        //1.加载配置文件
        Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro_realm.ini");
        //2.实例化对象
        SecurityManager securityManager = factory.getInstance();
        //绑定
        SecurityUtils.setSecurityManager(securityManager);
        //的到当前用户
        Subject subject = SecurityUtils.getSubject();
        //根据用户名密码登陆
        UsernamePasswordToken token = new UsernamePasswordToken("sunny", "1127");
        try {
            logger.info("=======>执行登陆");
            subject.login(token);
            logger.info("=======>登陆成功");
        } catch (Exception e) {
            logger.error("========>登陆失败:" + e);
        }
        //登出
        subject.logout();


    }

}

执行结果:
这里写图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的Shiro自定义Realm代码示例: ``` import org.apache.shiro.authc.*; import org.apache.shiro.realm.*; public class MyRealm extends AuthorizingRealm { @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { // 这里实现权限和角色信息的获取和设置,可以从数据库或其他数据源获取 SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo(); authorizationInfo.addRole("admin"); authorizationInfo.addStringPermission("user:create"); return authorizationInfo; } @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { // 这里实现用户认证,可以从数据库或其他数据源获取用户信息进行验证 String username = (String) token.getPrincipal(); String password = new String((char[]) token.getCredentials()); if (!"admin".equals(username)) { throw new UnknownAccountException(); } if (!"password".equals(password)) { throw new IncorrectCredentialsException(); } return new SimpleAuthenticationInfo(username, password, getName()); } } ``` 该自定义Realm实现了Shiro的`AuthorizingRealm`接口,实现了权限和角色信息的获取和设置,以及用户认证的方法。其中,`doGetAuthorizationInfo`方法用于获取用户的角色和权限信息,并将其封装到`AuthorizationInfo`对象中返回;`doGetAuthenticationInfo`方法用于验证用户的身份和凭证信息,并将其封装到`AuthenticationInfo`对象中返回。在这个简单的示例中,认证信息被硬编码为了"admin"和"password",实际使用时应该从数据库或其他数据源中获取。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值