Shiro | 实现权限验证完整版

Shiro | 实现权限验证完整版

写在前面的话

提及权限,就会想到安全,是一个十分棘手的话题。这里只是作为学校Shiro的一个记录,而不是,权限就应该这样设计之类的。

Shiro框架

1、Shiro是基于Apache开源的强大灵活的开源安全框架。

2、Shiro提供了 认证授权企业会话管理安全加密缓存管理

3、Shiro与Security对比

Shiro与Security对比

4、Shiro整体架构

Shiro整体架构图

5、特性

Shiro特性

6、认证流程

Shiro认证流程

认证

当我们理解Shiro之后,我们就能比较容易梳理出认证流程,大概就是下面这样子。

Shiro认证流程

我们来看一段测试代码:

// 1.构建SecurityManager环境
DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();
defaultSecurityManager.setRealm(simpleAccountRealm);

// 2.主体提交认证请求
SecurityUtils.setSecurityManager(defaultSecurityManager);
Subject subject = SecurityUtils.getSubject();

// 3.认证
UsernamePasswordToken token = new UsernamePasswordToken("admin", "admin");
subject.login(token);
System.out.println("是否认证:" + subject.isAuthenticated());

// 4.退出
subject.logout();
System.out.println("是否认证:" + subject.isAuthenticated());

我们发现Shiro真正帮我们做的就是认证这一步,那他到底是如何去认证的呢?

Shiro登录过程

我们把我们登录的代码完善一下:

/**
 * 登录操作,返回登录认证信息
 * @return 登录结果
 */
public String login() {
    try {
        DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();
        defaultSecurityManager.setRealm(simpleAccountRealm);

        SecurityUtils.setSecurityManager(defaultSecurityManager);
        Subject subject = SecurityUtils.getSubject();

        UsernamePasswordToken token = new UsernamePasswordToken("admin", "admin");
        
        subject.login(token);
        if (subject.isAuthenticated())
            return  "登录成功";
    } catch (IncorrectCredentialsException e1) {
        e1.printStackTrace();
        return "密码错误";
    } catch (LockedAccountException e2) {
        e2.printStackTrace();
        return "登录失败,该用户已被冻结";
    } catch (AuthenticationException e3) {
        e3.printStackTrace();
        return "该用户不存在";
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "登录失败";
}

Realm

1、IniReam

配置文件 user.ini

[users]
Mark=admin,admin
[roles]
admin=user:add,user:delete,user:update,user:select

测试代码

// IniRealm 测试
@Test
public void testAuthenticationIniRealm () {

    IniRealm iniRealm = new IniRealm("classpath:user.ini");

    DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();
    defaultSecurityManager.setRealm(iniRealm);

    SecurityUtils.setSecurityManager(defaultSecurityManager);
    Subject subject = SecurityUtils.getSubject();

    UsernamePasswordToken token = new UsernamePasswordToken("admin", "123456");
    subject.login(token);

    System.out.println("是否认证:" + subject.isAuthenticated());

    subject.checkRole("admin");
    subject.checkPermission("user:delete");

}

2、JdbcRealm

这里有两种方案,使用Shiro为我们提供了SQL语句,或者我们自己写SQL语句。

第一种:

// JdbcRealm 测试 Shiro SQL
@Test
public void testAuthenticationShiroSQL() {

    JdbcRealm jdbcRealm = new JdbcRealm();
    jdbcRealm.setDataSource(druidDataSource);
    jdbcRealm.setPermissionsLookupEnabled(true);

    DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();
    defaultSecurityManager.setRealm(jdbcRealm);

    SecurityUtils.setSecurityManager(defaultSecurityManager);
    Subject subject = SecurityUtils.getSubject();

    UsernamePasswordToken token = new UsernamePasswordToken("xfsy", "xfsy2018");
    subject.login(token);

    System.out.println("是否认证:" + subject.isAuthenticated());

    subject.checkRole("user");
    subject.checkPermission("user:select");

}

第二种:

// JdbcRealm 测试 Custom SQL
@Test
public void testAuthenticationCustomSQL() {

    JdbcRealm jdbcRealm = new JdbcRealm();
    jdbcRealm.setDataSource(druidDataSource);
    jdbcRealm.setPermissionsLookupEnabled(true);

    /**
     * @see org.apache.shiro.realm.jdbc.JdbcRealm
     */
    String sql = "select pwd from t_user where user_name = ?";
    jdbcRealm.setAuthenticationQuery(sql);

    DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();
    defaultSecurityManager.setRealm(jdbcRealm);

    SecurityUtils.setSecurityManager(defaultSecurityManager);
    Subject subject = SecurityUtils.getSubject();

    UsernamePasswordToken token = new UsernamePasswordToken("test", "123");
    subject.login(token);

    System.out.println("是否认证:" + subject.isAuthenticated());
}

3、自定义Realm

在上面我们已经看过了 JdbcRealm,所以我们也可以依葫芦画瓢自定义Ramlm。

第一步:继承 AuthorizingRealm

第二步:实现认证方法

第三步:实现授权方法

通过AuthorizingRealm,我们完全按照自己的需求实现自己的业务逻辑。

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

/**
 * @author Wenyi Feng
 * @since 2018-10-22
 */
public class CustomRealmTest extends AuthorizingRealm {

    /**
     * Retrieves the AuthorizationInfo for the given principals from the underlying data store.  When returning
     * an instance from this method, you might want to consider using an instance of
     * {@link org.apache.shiro.authz.SimpleAuthorizationInfo SimpleAuthorizationInfo}, as it is suitable in most cases.
     *
     * @param principals the primary identifying principals of the AuthorizationInfo that should be retrieved.
     * @return the AuthorizationInfo associated with this principals.
     * @see org.apache.shiro.authz.SimpleAuthorizationInfo
     */
    // 授权
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        return null;
    }

    /**
     * Returns {@code true} if authentication caching should be utilized based on the specified
     * {@link AuthenticationToken} and/or {@link AuthenticationInfo}, {@code false} otherwise.
     * <p/>
     * The default implementation simply delegates to {@link #isAuthenticationCachingEnabled()}, the general-case
     * authentication caching setting.  Subclasses can override this to turn on or off caching at runtime
     * based on the specific submitted runtime values.
     *
     * @param token the submitted authentication token
     * @param info  the {@code AuthenticationInfo} acquired from data source lookup via
     *              {@link #doGetAuthenticationInfo(org.apache.shiro.authc.AuthenticationToken)}
     * @return {@code true} if authentication caching should be utilized based on the specified
     *         {@link AuthenticationToken} and/or {@link AuthenticationInfo}, {@code false} otherwise.
     * @since 1.2
     */
    // 认证
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token)
            throws AuthenticationException {
        return null;
    }
}

可能Shiro提供的Realm并不能满足我们的实际开发需求,所以真正弄明白自定义Realm还是有很大帮助的,你觉得呢?

4、安全加密

明文密码?

好吧,我们看看Shiro为我们提供的加密方法。

//  Md5Hash md5Hash = new Md5Hash("123456");

Md5Hash md5Hash = new Md5Hash("123456", "admin");
System.out.println(md5Hash.toString());

那我们该怎么使用了?

在Realm认证中设置盐值

// 使用admin对密码进行加密
authenticationInfo.setCredentialsSalt(ByteSource.Util.bytes("admin"));

我们只需要告诉我们的Realm,需要对密码进行加密就可以了。

CustomRealm customRealm = new CustomRealm();
HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();
// 加密类型
matcher.setHashAlgorithmName("md5"); 
// 加密次数
matcher.setHashIterations(1); 
customRealm.setCredentialsMatcher(matcher);

权限

1、Shiro为我们提供的权限

名称 说明
anon 不校验
authc 作校验
roles 需要具有指定角色(一个或多个)才能访问
perms 需要具有指定角色(一个或多个)才能访问

2、配置

<!-- 从上往下开始匹配 -->
/login.html = anon
/subLogin = anon
<!--/testRole = roles["admin"]-->
<!--/testRole1 = roles["admin", "admin1"]-->
<!--/testPerms = perms["user:delete"]-->
<!--/testPerms1 = perms["user:delete", "user:update"]-->

3、自定义权限认证

import org.apache.shiro.web.filter.authz.AuthorizationFilter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * @author Wenyi Feng
 * @since 2018-10-22
 */
public class CustomAuthorizationFilter extends AuthorizationFilter {
    
    protected boolean isAccessAllowed(ServletRequest request, 
                                      ServletResponse response, 
                                      Object mappedValue) throws Exception {
        return false;
    }
}

另外,看一下 Shiro Filter

Shiro Filter

会话管理(Session)

会话管理,就是拿到Session之后,我们怎么处理。什么意思?分布式系统,需要共享Session。

import org.apache.shiro.session.Session;
import org.apache.shiro.session.UnknownSessionException;
import org.apache.shiro.session.mgt.eis.AbstractSessionDAO;

import java.io.Serializable;
import java.util.Collection;

/**
 * 自定义Session操作接口
 * @author Wenyi Feng
 * @since 2018-10-22
 */
public class CustomSessionDAO extends AbstractSessionDAO {
    
    // 创建Session
    protected Serializable doCreate(Session session) {
        return null;
    }

    // 读session
    protected Session doReadSession(Serializable sessionId) {
        return null;
    }

    // 修改session
    public void update(Session session) throws UnknownSessionException {

    }

    // 删除session
    public void delete(Session session) {

    }

    // 获取当前活动的session
    public Collection<Session> getActiveSessions() {
        return null;
    }
}

缓存管理(Cache)

缓存管理同Session管理,可以这样说,session是一套系统的基础,缓存决定系统的优化级别,很重要。

缓存设计

另外,我们看一下,Shiro为我们设计的缓存接口。

package org.apache.shiro.cache;

import java.util.Collection;
import java.util.Set;

public interface Cache<K, V> {
    V get(K var1) throws CacheException;

    V put(K var1, V var2) throws CacheException;

    V remove(K var1) throws CacheException;

    void clear() throws CacheException;

    int size();

    Set<K> keys();

    Collection<V> values();
}

自动登录

我们只需要为token设置RememberMe就可以了。

token.setRememberMe(user.getRememberMe());

链接

[1] Shiro安全框架入门

[2] 本次测试代码:ShiroLearn

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/101356.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 谷歌离线地图包下载安卓版_谷歌地图 app

    谷歌离线地图包下载安卓版_谷歌地图 appGoogle离线地图发布文档一键离线地图发布(工具软件下载)  使用教程百度离线地图发布  教程 手机离线地图发布——Oruxmaps制作发布高清卫星离线地图谷歌离线地图发布API解析说明:1. 当前版本支持谷歌电子/卫星地图瓦片、高德地图、阿里云地图、超图、腾讯地图等(只需下载该地图源的瓦片拷贝到指定目录即可);2. 效果预览

    2022年9月21日
    0
  • A4988步进驱动

    A4988步进驱动基本知识绕组  常用的步进电机有四根线,1A1B2A2B,1A和1B是一个绕组,2A和2B是一个绕组,用万用表测试1A和1B之间是短路的,2A和2B之间是短路的,1A和1B,2A和2B是等效的。  通常状况下,步进电机可以自由转动(用手可以拧动),1A和1B接在一起的时候,用手拧会感到明显阻力,1A和1B,2A和2B分别接在一起,则阻力更大。步距角  所谓步进电机,就是可以…

    2022年6月29日
    36
  • 微信二维码登录原理是什么_请使用微信扫描二维码登录

    微信二维码登录原理是什么_请使用微信扫描二维码登录网页登陆是微信4.2以后版本提供的一种全新的登陆方式。用户只需要用手机扫一扫微信网页中的二维码,就能马上实现微信网页登陆。这种登陆方式虽然炫酷,但是多少有些违背直觉:网页端是怎么知道是哪个微信账号

    2022年8月4日
    3
  • Linux命令—tail 加过滤功能

    Linux命令—tail 加过滤功能tail-n 行数-f 文件夹路径以及名字,用于查看log 后面可以追加 |grep +名字 用于过滤ps aux|grep 文件 查看执行情况 tail-n40-f/home/logs/info.log|grepkeyword …

    2022年5月23日
    196
  • Simple Problem with Integers POJ – 3468

    Simple Problem with Integers POJ – 3468

    2021年9月27日
    40
  • 免备案cdn_cdn贝安装

    免备案cdn_cdn贝安装免备案CDN,符码CDN应该是使用CDN贝搭建,该CDN系统相对来说比较成熟,功能支持比较丰富。今天对符码免备案CDN进行简单的测试,仅供大家参考。首先,就是简单粗暴的多地区PING大法,简单看下CDN延迟效果:如果要是对比那么多免备案CDN延迟来说,符码CDN平均可以做到45ms的延迟,无疑是测试的所有免备案CDN中多地区PING平均延迟最小的一个,甚至一些国内CDN也达不到这个低延迟效果,延迟…

    2022年9月3日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号