自定义realm-身份认证及权限

为何要自定义Realm?

Shiro框架中并没有提供一个以SpringDataJpa来实现访问数据的这样一个Realm,所以我们必须自定义一个Realm

如何自定义?

Realm是Shiro框架中的一个接口,那么我们要自定义Realm的话,就只需要自定义一个类去实现Realm接口就行了。
如果直接实现Realm接口的话,这个自定义的Realm只能用来做登录,无法用来做权限认证功能。
将我们自定义的JpaRealm类继承AuthorizingRealm类,就可以制作登录和权限认证的功能了

public class JpaRealm extends AuthorizingRealm {
    /**
     * 授权方法
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("授权");
        //获取当前登录用户对象
        Employee employee = (Employee)SecurityUtils.getSubject().getPrincipal();
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        //通过该用户去查询他的权限
        Set<String> permissions = this.findPermissionsByEmployeeId(employee.getId());
        //授权
        authorizationInfo.setStringPermissions(permissions);
        return authorizationInfo;
    }

    private Set<String> findPermissionsByEmployeeId(Long id) {
        Set<String> permissions = new HashSet<>();
        permissions.add("employee:index");
        permissions.add("employee:page");
        return permissions;
    }

    /**
     * 身份认证
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //类型强转
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        //获取用户名
        String username = token.getUsername();

        Employee employee = this.findByUsername(username);
        if(employee != null){
            //ByteSource credentialsSalt 盐值
            ByteSource salt = ByteSource.Util.bytes("abcdefg");
            return new SimpleAuthenticationInfo(employee,employee.getPassword(),salt,"JpaRealm");
        }
        return null;
    }

    private Employee findByUsername(String username) {
        if("admin".equals(username)){
            Employee employee = new Employee();
            employee.setUsername("admin");
            employee.setPassword("09f81be23578bdfe69565849199acaef");
            return employee;
        }
        return null;
    }
}

测试

public class _09_RealmTest {
    /**
     * 登录测试
     * @throws Exception
     */
    @Test
    public void test() throws Exception{
        //创securityManager对象
        DefaultSecurityManager securityManager = new DefaultSecurityManager();
        //创建自定义的JpaRealm对象
        JpaRealm jpaRealm = new JpaRealm();
        //将其设置到securityManager对象属性上

        securityManager.setRealm(jpaRealm);
        SecurityUtils.setSecurityManager(securityManager);
        //获取当前用户
        Subject subject = SecurityUtils.getSubject();
        //判断是否登录
        System.out.println(subject.isAuthenticated());
        try {
            if (!subject.isAuthenticated()) {
                //未登录的话就登录
                UsernamePasswordToken token = new UsernamePasswordToken("admin", "123456");
                subject.login(token);
            }
        } catch (UnknownAccountException e) {
            e.printStackTrace();
            System.out.println("用户名不存在");
        } catch (IncorrectCredentialsException e) {
            e.printStackTrace();
            System.out.println("密码错误");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("系统繁忙,连接超时");
        }
        System.out.println(subject.isAuthenticated());
        if (subject.isAuthenticated()) ;
        {
            //登出
            subject.logout();
        }
        //判断是否登录
        System.out.println(subject.isAuthenticated());
    }

    /**
     * 权限测试
     * @throws Exception
     */
    @Test
    public void test02() throws Exception{
        //创securityManager对象
        DefaultSecurityManager securityManager = new DefaultSecurityManager();
        //创建自定义的JpaRealm对象
        JpaRealm jpaRealm = new JpaRealm();
        //将其设置到securityManager对象属性上

        securityManager.setRealm(jpaRealm);
        SecurityUtils.setSecurityManager(securityManager);
        //获取当前用户
        Subject subject = SecurityUtils.getSubject();
        //判断是否登录
        System.out.println(subject.isAuthenticated());
        try {
            if (!subject.isAuthenticated()) {
                //未登录的话就登录
                UsernamePasswordToken token = new UsernamePasswordToken("admin", "123456");

                subject.login(token);
            }
        } catch (UnknownAccountException e) {
            e.printStackTrace();
            System.out.println("用户名不存在");
        } catch (IncorrectCredentialsException e) {
            e.printStackTrace();
            System.out.println("密码错误");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("系统繁忙,连接超时");
        }
        System.out.println(subject.isAuthenticated());
        //判断该用户的权限
        System.out.println("save:"+subject.isPermitted("employee:save"));
        System.out.println("page:"+subject.isPermitted("employee:page"));
        System.out.println("index:"+subject.isPermitted("employee:index"));
        if (subject.isAuthenticated()) ;
        {
            //登出
            subject.logout();
        }
        //判断是否登录
        System.out.println(subject.isAuthenticated());
    }
}
  • 0
    点赞
  • 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、付费专栏及课程。

余额充值