shiro权限控制

来源:互联网 发布:linux服务器安装vnc 编辑:程序博客网 时间:2024/06/05 12:44

        shiro 是一个权限管理框架,可以用来做权限的管理与控制,这个shiro框架可以对权限做控制,他的权限管理,大到role,小到权限,在权限控制上,可以通过shiro标记做资源权限管理。

       简单记录一下这个shiro的使用:自身是做web的,所以就说shiro在web中的使用。在web.xml中加shiro过滤器:

<!-- apache shiro权限 -->
<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>*.do</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
这个是说过滤.jsp和.do的请求。

        在applicationContext.xml中:

       <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager" />
<property name="loginUrl" value="/login.jsp" />
<property name="successUrl" value="/login.jsp" />
<property name="unauthorizedUrl" value="/error/noperms.jsp" />
<property name="filterChainDefinitions">
<value>
/login.jsp* = anon
/login.do* = anon
/index.jsp*= anon
/error/noperms.jsp*= anon
/*.jsp* = authc
/*.do* = authc
</value>
</property>
</bean>


<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<!--设置自定义realm -->
<property name="realm" ref="monitorRealm" />
</bean>


<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />


<!--自定义Realm 继承自AuthorizingRealm -->
<bean id="monitorRealm" class="com.ha.shiro.MonitorRealm"></bean>
<!-- securityManager -->
<bean
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="staticMethod"
value="org.apache.shiro.SecurityUtils.setSecurityManager" />
<property name="arguments" ref="securityManager" />
</bean>


<!-- Enable Shiro Annotations for Spring-configured beans. Only run after -->
<!-- the lifecycleBeanProcessor has run: -->
<bean
class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
depends-on="lifecycleBeanPostProcessor" />
<bean
class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager" />


</bean>
       这样就配好了:

      还要自定义一个realm:

      这个realm要继承AuthorizingRealm,重写:doGetAuthorizationInfo和doGetAuthenticationInfo。

      doGetAuthorizationInfo这个是获取权限和角色。

       doGetAuthenticationInfo是做登录验证,登录验证的方法:subject.login()这个就是用这个方法来检验的。

0 0
原创粉丝点击