权限管理以及shiro的简述(个人理解)
权限管理简要设计(数据库表):
权限表:存贮各种权限(url);
用户表:属于某个组;
组(角色):组中根据需求拥有各种权限(角色表与组表性质类似)。
表关系:组和权限表(多对多),组和用户表(多对多)。(这里的关系要根据实际需求来做决定,不是固定的)
权限表可以通过其他方式进行表示,这里写权限表是为了方便理解
在用户登陆系统时,会进行权限的认证,一般通过过滤器进行处理。
shiro的一些个人理解(大体思路):
登陆认证过程:
首先需要构建realm对象,这个对象包含着从数据库或者是缓存里面读取出来的登陆信息以及权限信息。这个对象确认以后,我们来创建securityManager对象,并将realm对象传递给securityManager对象。然后通过SecurityUtils工具类将securityManager设置进去,并通过它获取subject对象。接着,来创建token对象,token对象用于保存页面传递来的登录信息。最后通过subject.login()方法将token传递进去进行登陆认证。如果没有异常,登陆成功。
Realm:shiro框架提供了两个realm,IniRealm和JdbcRealm。分别是通过配置文件和数据来读取信息。这两种方式并不适用于真正的项目。我们需要自定义Realm,自定义Realm需要继承AuthorizingRealm。继承这个类以后需要去实现他的两个方法:doGetAuthorizationInfo()这个方法用于认证用户角色和权限,doGetAuthenticationInfo()这个方法用于认证用户密码。
spring整合shiro:需要将securityManager交给spring去管理(DeafultWebSecurityManager)。以及HashedCredentialsMatcher交给spring进行管理。还需要将filter交给spring管理(ShiroFilterFactoryBean)。还需要将filter配置到web.xml中。
spring配置文件需要配置如下bean:
<!--配置shiro的securityManager--> <bean class="org.apache.shiro.web.mgt.DefaultWebSecurityManager" id="securityManager"> <property name="realm" ref="myRealm"/> </bean> <bean class="shiro.realm.MyRealm" id="myRealm"/> <bean id="filterChainDefinitions" class="java.lang.String"> <constructor-arg> <value> / = anon /login = anon /test = authc </value> </constructor-arg> </bean> <bean class="org.apache.shiro.spring.web.ShiroFilterFactoryBean" id="shiroFilter"> <property name="securityManager" ref="securityManager"/> <property name="loginUrl" value="/login"/> <property name="filterChainDefinitions" ref="filterChainDefinitions"/> </bean>
mvc需要做的配置:
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor"> <property name="proxyTargetClass" value="true" /> </bean> <bean class="org.apache.shiro.spring.LifecycleBeanPostProcessor" id="lifecycleBeanPostProcessor"/> <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"> <property name="securityManager" ref="securityManager"/> </bean>
这段配置一定写在mvc的配置文件中,否则shiro注解无效
以下为shiro需要的jar包
<!--shiro部分--> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-ehcache</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-web</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>1.2.3</version> </dependency>