标签:
上一篇博客很简单的罗列了一下shiro的框架的构成,其实就是混个脸熟先。这篇博客呢,还是不打算具体的讲,我们先通过一个完整的实例体会一下如何使用。然后,应该就能够对于shiro就能有一个大概的,基于实际使用的认识。开始了……
Web.xml
<span style="font-size:18px;"><!--默认页面 -->
<welcome-file-list>
<welcome-file>/Page/login.jsp</welcome-file>
</welcome-file-list>
<!--shiro配置文件的位置,这里spring和shiro都配置在一个文件上了 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring-shiro.xml
</param-value>
</context-param>
<!--启动Web容器时,自动装配ApplicationContext的配置信息,这里的配置文件就是shiro配置文件-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--shiro的主过滤器 -->
<filter>
<filter-name>shiroSecurityFilter</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>shiroSecurityFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--以下是servlet的配置 -->
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>com.tgb.shirodemo.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>money</servlet-name>
<servlet-class>com.tgb.shirodemo.servlet.mainServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>money</servlet-name>
<url-pattern>/money</url-pattern>
</servlet-mapping></span>
Spring-shiro.xml
<span style="font-size:18px;"><!--这是我的需要注入的到ealm中的两个执行访问数据库的bean -->
<beanid="userMgr"class="com.tgb.shirodemo.manager.UserManager"></bean>
<beanid="permissionMgr"class="com.tgb.shirodemo.manager.PermissionManager"></bean>
<!--shiro和数据库的桥梁,相当于DAO -->
<beanid="shiroRealm"class="com.tgb.shirodemo.shiro.MyShiroRealm">
<property name="usermgr"ref="userMgr"></property>
<property name="permgr"ref="permissionMgr"></property>
</bean>
<!-- 缓存管理器 -->
<beanid="shiroEhcacheManager"class="org.apache.shiro.cache.ehcache.EhCacheManager">
<propertyname="cacheManagerConfigFile"value="classpath:ehcache-shiro.xml" />
</bean>
<!--安全管理器,上篇博客看到了它是统筹全局的一个组件-->
<beanid="securityManager"class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<!-- 集成上数据访问的Realm -->
<property name="realm"ref="shiroRealm"></property>
<!-- 集成上缓存管理器 -->
<property name="cacheManager"ref="shiroEhcacheManager"></property>
</bean>
<!--保证shiro内部生命周期的bean被执行 -->
<beanid="lifecycleBeanPostProcessor"class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
<!--以下两个是关于启用注解的配置 -->
<beanclass="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
depends-on="lifecycleBeanPostProcessor"></bean>
<beanclass="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<propertyname="securityManager" ref="securityManager"/>
</bean>
<!--shiro主过滤器的配置,这里的名字和web中的要对应 -->
<bean id="shiroSecurityFilter"class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<!-- 集成上安全管理器 -->
<propertyname="securityManager"ref="securityManager"></property>
<property name="loginUrl"value="/Page/login.jsp"></property>
<property name="successUrl"value="/Page/main.jsp"></property>
<propertyname="unauthorizedUrl"value="/Page/second.jsp"></property>
<!-- 过滤器链,对URL配置过滤规则 -->
<propertyname="filterChainDefinitions">
<value>
/=anon
/login=anon
/**=authc
</value>
</property>
</bean>
</beans></span>缓存的XML
<span style="font-size:18px;"><ehcacheupdateCheck="false" name="shiroCache">
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="false"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
/>
</ehcache></span>配置文件就是这样了,接下来还有一些关键的类需要说一下,Realm这个类是需要我们自己实现的。是shiro框架所需要的验证信息的唯一来源。之前spring-shiro.xml中配置的类就是这个实现。这里如果嫌麻烦的话,可以直接写死的验证信息。
<span style="font-size:18px;">public classMyShiroRealm extends AuthorizingRealm {
//注入的类,真正的去访问数据库
private UserManager usermgr;
private PermissionManager permgr;
//查询用户的权限信息
@Override
protectedAuthorizationInfo doGetAuthorizationInfo(
PrincipalCollectionprincipals) {
ShiroUseruser=(ShiroUser)principals.fromRealm(getName()).iterator().next();
Collection<Permission>cper= permgr.getPermission(user);
SimpleAuthorizationInfoinfo=new SimpleAuthorizationInfo();
Iterator<Permission>it=cper.iterator();
while(it.hasNext()){
info.addStringPermission(it.next().getPermissionName());
}
returninfo;
}
//查询用户的身份信息
@Override
protectedAuthenticationInfo doGetAuthenticationInfo(
AuthenticationTokentoken) throws AuthenticationException {
ShiroUseruser=usermgr.getUserByName(token.getPrincipal().toString());
if(user==null){
thrownew UnknownAccountException();
}else{
returnnew SimpleAuthenticationInfo(user,user.getPassWord(),getName());
}
}
publicUserManager getUsermgr() {
returnusermgr;
}
publicvoid setUsermgr(UserManager usermgr) {
this.usermgr= usermgr;
}
publicPermissionManager getPermgr() {
returnpermgr;
}
publicvoid setPermgr(PermissionManager permgr) {
this.permgr= permgr;
}
}</span>基本上需要做的事情就这么多了,我的这个例子里的其他的类都是基本的spring的东西了。就不都贴出来了,这里是完整的下载链接。
Apache shiro(2)—first Demo(web+spring+shiro)
标签:
原文地址:http://blog.csdn.net/zhuojiajin/article/details/42167661