Apache shiro(2)—first Demo(web+spring+shiro)
来源:互联网 发布:c语言中auto是什么意思 编辑:程序博客网 时间:2024/05/01 13:53
上一篇博客很简单的罗列了一下shiro的框架的构成,其实就是混个脸熟先。这篇博客呢,还是不打算具体的讲,我们先通过一个完整的实例体会一下如何使用。然后,应该就能够对于shiro就能有一个大概的,基于实际使用的认识。开始了……
JAR包
- shiro-all:这个JAR包含了所有的JAR。现在shiro的JAR包有分开的,可以根据使用的功能选用其中的部分。这里,我们的首要目标是体会功能,因此我想尽可能的避免一些问题,所以选了Shiro-all.
- conmons-beanutils,commons-logging:这两个包,先理会干嘛使用的,反正是得引入。
- aopalliance:这个包是关于Aop的,启用注解的话就必须引入该包。
- ehcache-core:这是缓存的JAR包,使用缓存的话需要引入。当然,这里可以使用其他的缓存产品,ehcache只是其中一个。
- mysql-connector-java:mysql的启动包
- spring-*:spring需要引入的包
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
配置文件就是这样了,接下来还有一些关键的类需要说一下,Realm这个类是需要我们自己实现的。是shiro框架所需要的验证信息的唯一来源。之前spring-shiro.xml中配置的类就是这个实现。这里如果嫌麻烦的话,可以直接写死的验证信息。
<span style="font-size:18px;">public classMyShiroRealm extends AuthorizingRealm {//注入的类,真正的去访问数据库 private UserManager usermgr; private PermissionManager permgr; //查询用户的权限信息@OverrideprotectedAuthorizationInfo 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;} //查询用户的身份信息@OverrideprotectedAuthenticationInfo 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的东西了。就不都贴出来了,这里是完整的下载链接。
1 1
- Apache shiro(2)—first Demo(web+spring+shiro)
- spring+apache shiro demo
- Your First Apache Shiro Application
- Apache Shiro 集成-spring
- spring+apache shiro登录
- Apache Shiro 集成-spring
- 【Shiro】Apache Shiro架构之集成web
- 【Shiro】Apache Shiro架构之集成web
- 【Shiro】Apache Shiro架构之集成web
- Shiro web 和spring
- Apache Shiro Web应用过滤器
- Apache Shiro与web整合
- apache shiro+spring简单集成
- apache shiro整合spring(一)
- shiro demo
- apache shiro自定义shiro
- 【shiro】--- spring整合shiro
- Apache Shiro
- 创建一个directshow应用程序一
- 使用jquery隐藏和移动dom
- 二三线城市产业园区发展的机遇
- Hadoop企业级完整训练:HDFS&MapReduce&HBase&Hive&Zookeeper&Pig&Project)
- ubuntu配置skynet
- Apache shiro(2)—first Demo(web+spring+shiro)
- 认识java.security.MessageDigest 类
- 10米USB延长线制作方法
- Python data structure
- HTML5三屏合一的架构设计
- 杂
- 随机取数组,打乱数组元素
- IE浏览器和火狐浏览器兼容问题——CSS篇
- WAS 各版本比较(5、6、7)