shrio框架SE的环境搭建

来源:互联网 发布:淘宝店铺推荐少女 知乎 编辑:程序博客网 时间:2024/06/09 16:36

1.首先我们要清楚为什么要在JAVA SE的环境中搭建:

因为我们现在将侧重点放在shiro的使用上面,它的API以及使用的流程

a).导入相关的jar

b).配置文件,找到shiro-root-1.3.2\samples\quickstart\src\main\resources下面的两个
           文件将之拷贝过来到我们的JAVASE项目的src目录下面即可.

2.开发一个shiro JAVASE项目:

1.获取安全管理器(SecurityManager):

a).首先我们需要通过Factory这个工厂来获取我们的SecurityManager,他是

一个接口,通过new子类IniSecurityManagerFactory传入shiro.ini文件来构

建实例。然后通过工厂实例来获取SecurityManager的实例(安全管理器)

b).设置安全管理器,通过SecruityUtils来设置SecurityManager安全管理器

2.获取用户:

a).获取Subject(即将登陆的用户),借助SecruityUtils来获取Subject

3.获取登录验证:

4.权限管理:

5.角色管理:

6.session:会话管理:

a).通过Subject实例来获取session会话

3.如何通过shiro框架常用的API来完成用户的认证登陆

1.判断Subject到底有没有被认证,通过Subject实例的isAuthenticated方法,返回

false代表没有被认证过,否则相反。

2.如果没有认证我们就设置一个用户和密码,通过UsernamePasswordToken对象

实例来封装用户名和密码,必须是shiro.ini文件中已存在的用户和密码,然后通过

实例UsernamePasswordToken调用方法setRememberMe设置为true意思就是说

记住我,然后在完成一个真正意义的登陆通过Subject实例来调用Login传入UsernamePasswordToken

实例,如果在这过程比如说密码或者用户错误,不存在,用户锁死,这些错误,对应

shiro提供了一些异常类参考(4)

4.异常的介绍

1.UnknownAccountException:账户不存在异常

2.IncorrectCredentialsException:密码错误异常

3.LockedAccountException:用户锁死异常

5.用户登陆成功后的角色管理和权限判断

1.通过Subject实例的hasRole方法来判断某个用户是否拥有指定的角色。

2.通过Subject实例的isPermitted方法来判断某个用户是否有指定的权限

6.WEB集成shiro

a).导入shiro相关的jar

这个就直接通过maven来引用jar即可

b).配置web.xml文件

这些配置在下载的shiro解压包里面找到shiro-root-1.3.2\samples\spring\src\main\webapp\WEB-INF

目录下面就有这些文件根据这里面的配置即可

1.首先配置filter

   <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>

c).配置applicationContext.xml文件

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">    <!-- =========================================================         Shiro Core Components - Not Spring Specific         ========================================================= -->    <!-- Shiro's main business-tier object for web-enabled applications         (use DefaultSecurityManager instead when there is no web environment)-->    <!-- 安全管理认证器 -->    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">        <property name="cacheManager" ref="cacheManager"/>        <!-- Single realm app.  If you have multiple realms, use the 'realms' property instead. -->        <property name="realm" ref="jdbcRealm"/>    </bean>    <!-- Let's use some enterprise caching support for better performance.  You can replace this with any enterprise         caching framework implementation that you like (Terracotta+Ehcache, Coherence, GigaSpaces, etc -->    <!-- 我们可以使用的环境技术 -->    <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">        <!-- Set a net.sf.ehcache.CacheManager instance here if you already have one.  If not, a new one             will be creaed with a default config:             <property name="cacheManager" ref="ehCacheManager"/> -->        <!-- If you don't have a pre-built net.sf.ehcache.CacheManager instance to inject, but you want             a specific Ehcache configuration to be used, specify that here.  If you don't, a default             will be used.: -->        <!-- 这个 ehcache.xml 文件直接可以从我们下载的shiro解压包下的shiro-root-1.3.2\samples\spring-hibernate\src\main\resources        拿取        -->        <property name="cacheManagerConfigFile" value="classpath:ehcache.xml"/>    </bean>    <!-- Used by the SecurityManager to access security data (users, roles, etc).         Many other realm implementations can be used too (PropertiesRealm,         LdapRealm, etc. -->    <!-- 这个是处理数据的,我们这里只需要实现 org.apache.shiro.realm.Realm 接口即可 -->    <bean id="jdbcRealm" class="com.vnetbs.utils.ShiroRealm"></bean>    <!-- =========================================================         Shiro Spring-specific integration         ========================================================= -->    <!-- Post processor that automatically invokes init() and destroy() methods         for Spring-configured Shiro objects so you don't have to         1) specify an init-method and destroy-method attributes for every bean            definition and         2) even know which Shiro objects require these methods to be            called. -->    <!-- 必须要有这样实例,用来管理在spring容器当中的shiro常见的对象 -->    <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>    <!-- Enable Shiro Annotations for Spring-configured beans.  Only run after         the lifecycleBeanProcessor has run: -->    <!-- 启用shiro的注解 -->    <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>    <!-- Secure Spring remoting:  Ensure any Spring Remoting method invocations can be associated         with a Subject for security checks. -->    <!-- 网络的配置 -->    <bean id="secureRemoteInvocationExecutor" class="org.apache.shiro.spring.remoting.SecureRemoteInvocationExecutor">        <property name="securityManager" ref="securityManager"/>    </bean>    <!-- Define the Shiro Filter here (as a FactoryBean) instead of directly in web.xml -         web.xml uses the DelegatingFilterProxy to access this bean.  This allows us         to wire things with more control as well utilize nice Spring things such as         PropertiesPlaceholderConfigurer and abstract beans or anything else we might need: -->    <!-- 这个bean用来配置shiro filter1.这个bean的id必须要和web.xml中的fileter-name的名字必须是一致的我们在web.xml中配置代理类会自动的到IOC容器中找在filter-name当值对应的bean对象-->    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">    <!-- 注入安全管理器 -->        <property name="securityManager" ref="securityManager"/>        <!-- 代表当前web程序的欢迎页面 -->        <property name="loginUrl" value="/index/index.jsp"/>        <!-- 认证登陆成功之后所要跳转的界面 -->        <property name="successUrl" value="/index/success.jsp"/>        <!-- 代表类授权的URL,如果登陆失败跳转到那个JSP界面 -->        <property name="unauthorizedUrl" value="/index/unauthorized.jsp"/>        <!-- The 'filters' property is not necessary since any declared javax.servlet.Filter bean             defined will be automatically acquired and available via its beanName in chain             definitions, but you can perform overrides or parent/child consolidated configuration             here if you like: -->        <!-- <property name="filters">            <util:map>                <entry key="aName" value-ref="someFilterPojo"/>            </util:map>        </property> -->        <!-- 代表我们要完成shiro过滤器的具体配置anon:代表未经认证可以认证的资源authc:必须经过认证之后才可以访问的web资源-->        <property name="filterChainDefinitions">            <value>                /index/index.jsp = anon                /** = authc            </value>        </property>    </bean></beans>

shiro工作原理:



DelegatingFilterProxy作用详解:

Shiro框架核心配置:spring的配置文件中配置一个bean,org.apache.shiro.spring.web.ShiroFilterFactoryBean


web.xml核心配置org.springframework.web.filter.DelegatingFilterProxy


DelegatingFilterProxy是用来到spring容器中去找filter-name相同名字的bean实例

如果没有对的则会抛错,创建bean失败,shiroFilter。


原创粉丝点击