shiro权限认证+RBAC权限五表+细粒度控制

RBAC权限五表

经典的权限五表:用户表+角色表+权限表+角色和用户的中间表+权限与角色的中间表
在这里插入图片描述
这里最重要的就是权限表,一般情况下不同角色在登录之后遇见的菜单不是一样的多,就是在权限表中查出来的不是一样多
在这里插入图片描述
图中的数据库没有写全!!!!!!
shiro就是通过上面的方式来查询权限的:当用户登录之后先确定角色信息,在通过角色查询权限

shiro的使用

shiro
maven

        <!--shiro和spring整合-->
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-spring</artifactId>
            <version>1.3.2</version>
        </dependency>
        <!--shiro核心包-->
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-core</artifactId>
            <version>1.3.2</version>
        </dependency>

controller:

//1.获取subject
		Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken upToken = new UsernamePasswordToken(email,password);//登录用户名和密码
			subject.login(upToken);
			//3.登录成功的逻辑
			//从shiro中获取用户对象
			User loginUser = (User)subject.getPrincipal();//安全数据(用户对象)

web.xml中的配置

<filter>
        <filter-name>shiroFilter</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
        <init-param>
            <param-name>targetFilterLifecycle</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>shiroFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

shiro的配置文件:配置文件是可以在shiro官网上下载的

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <description>Shiro与Spring整合</description>

    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="realm" ref="authRealm"/><!-- 引用自定义的realm -->
    </bean>


    <!-- 自定义Realm域的编写 -->
    <bean id="authRealm" class="我们自己的ream域路径">
        <!-- 注入自定义的密码比较器 -->
        <property name="credentialsMatcher" ref="customerCredentialsMatcher" ></property>
    </bean>

    <!-- 自定义的密码比较器 -->
    <bean id="customerCredentialsMatcher" class="自定义的密码比较器></bean>

    <!-- filter-name这个名字的值来自于web.xml中filter的名字 -->
    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <property name="securityManager" ref="securityManager"/>
        <!--登录页面  -->
        <property name="loginUrl" value="/login.jsp"></property>
        <!-- 登录失败后 -->
        <property name="unauthorizedUrl" value="/unauthorized.jsp"></property>

        <property name="filterChainDefinitions">
            <!-- /**代表下面的多级目录也过滤 -->
            <value>
               /xxxx/xxxx/* = perms["一级菜单"]
                /index.jsp* = anon
                /login.jsp* = anon
                /login* = anon
                /logout* = anon
                /css/** = anon
                /img/** = anon
                /plugins/** = anon
                /make/** = anon
                /** = authc
                /*.* = authc
            </value>
        </property>
    </bean>

    <!-- 保证实现了Shiro内部lifecycle函数的bean执行 -->
    <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>

    <!-- 生成代理,通过代理进行控制 -->
    <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor">
        <property name="proxyTargetClass" value="true"/>
    </bean>

    <!-- 开启对shiro注解支持 -->
    <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
        <property name="securityManager" ref="securityManager"/>
    </bean>
    <aop:aspectj-autoproxy proxy-target-class="true"/>
</beans>

shiro权限解释:
在这里插入图片描述

自定义reaml:

/**
 * 自定义realm:继承AuthorizingRealm
 */
public class AuthRealm extends AuthorizingRealm {

	@Autowired
	private UserService userService;

	@Autowired
	private PeimissionService peimissionService;
	//授权
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
		//1.获取安全数据中的user对象
		User user = (User) principalCollection.getPrimaryPrincipal();
		//2.获取权限
		List<Module> modules = peimissionService.findByUserId(user.getId());
		//3.构造返回值
		SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
		Set<String> perms = new HashSet<>(); //存放所有菜单的名称
		for (Module module : modules) {
			perms.add(module.getName());//模块的名称
		}
		//授权(perms是所有的权限,因为构造返回值的时候需要的权限参数是set所以要将所有的权限保存到set中)
		info.setStringPermissions(perms);

		return info;
	}


	//认证
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
		//1.获取用户输入的账号和密码
		UsernamePasswordToken upToken = (UsernamePasswordToken)authenticationToken;
		String account= upToken.getUsername();
		String password = new String(upToken.getPassword()); //将char数组转化为string字符串
		//2.根据account查询用户
		User user = userService.findByEmail(account);
		//3.如果用户存在,构造安全数据(当前登录的用户)并return
		if(user != null) {
			return new SimpleAuthenticationInfo(user,user.getPassword(),this.getName()) ;//安全数据,构造方法(用户对象,数据库密码。当前realm名称(当前类名))
		}else{
			//4.如果用户不存在,返回null
			return null;
		}
	}
}

密码比较器:

/**
 * 自定义密码比较器
 *      继承SimpleCredentialsMatcher
 *      重写方法    :doCredentialsMatch方法
 */
public class CustomCredentialsMatcher extends SimpleCredentialsMatcher {

	public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
		//1.获取用户输入的密码和数据库密码
		UsernamePasswordToken upToken = (UsernamePasswordToken) token;
		String account= upToken.getUsername();
		String password = new String(upToken.getPassword());
		String dbPassword = (String)info.getCredentials();//获取数据库密码
		//2.对用户输入的密码加密
		password = Encrypt.md5(password,account);
		//3.比较两次密码
		return password.equals(dbPassword);
	}
}

至此shiro的操作已经完成了,还有进一步的操作就是缓存操作,在执行授权的时候我们要去是数据库查询出当前登录的用的查询出来,在进行授权,比较的浪费资源,所以有了缓存操作,很简单
目前的缓存有
目前市面支持3中缓存:

内置缓存管理器:org.apache.shiro.cache.MemoryConstrainedCacheManager

Ehcache管理器:org.apache.shiro.cache.ehcache.EhCacheManager

redis缓存管理器:org.crazycake.shiro.RedisCacheManager

这里简单的介绍内置缓存
在shiro的配置文件中配置下面的任意一个即可
`

<!--创建ehcache的缓存管理器:基于文件的缓存管理
<bean id="cacheManger" class="org.apache.shiro.cache.ehcache.EhCacheManager">
    注入ehcache的配置文件
    <property name="cacheManagerConfigFile" value="classpath:ehcache-shiro.xml"></property>
</bean>-->

<!--基于redis的缓存管理器-->
<!--1.redis配置-->
<!--<bean id="redisManager" class="org.crazycake.shiro.RedisManager">
    <property name="host" value="127.0.0.1:6379"></property>
</bean>-->
<!--2.创建redis的缓存管理器-->
<!--<bean id="cacheManger" class="org.crazycake.shiro.RedisCacheManager">
    <property name="redisManager" ref="redisManager"></property>
</bean>-->`

shiro的注解开发

在需要加权限的接口上加一个注解@RequiresPermissions(“一级菜单”)即可
注意在没有权限的请款下访问会报错(unauthorized)
这个就需要在自己项目的异常处理器上来处理了

数据进行细粒度的控制

在公司开发经常需要做数据的限制,例如员工登录之后所能查询到的数据与公司老板登录之后的数据不一样,虽然他们都有查询的权限但是查到的数据不同,这里就需要做对数据的控制,如果只是有老板和员工之分比较的好做,但是如果有了老板–>经理–>组长–>员工的区分呢,如果一个系统是多个公司再用,可能一个系统上有很多不同公司的老板,怎么把不同公司的数据不会与其他公司数据区分开

很多人说加字段,思路是对的,所加的字段需要有一定的规则

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值