shiro框架从入门到精通(一)

今天刚接触shiro框架,以前也看到过,只是没有去学习,今天接触一下,感觉还是很容易的,先就让我们一步步探讨,看看shiro怎么用,我们为什么要用shiro,它能给我们带来了什么好处。

一、先学会怎么样使用shiro:

本人观点,学知道,再使用,然后精通,因为此框架比较简单,那么让我们直接进入前面两步骤。

二、shiro 能做什么

声明:本文纯属个人理解

    shiro 是一个权限管理框架,它能够完成认证、授权、加密、会话管理等功能。认证和授权为权限控制的核心,简单来说,“认证”就是证明你是谁? Web 应用程序一般做法通过表单提交用户名及密码达到认证目的。“授权”即是否允许已认证用户访问受保护资源

这里为大家推荐两篇非常不错的博客,建议初学者可以先看看这两篇博客,对shiro有个初步了解以后再来跟着我一起学习,一些基本的解释这里就不说了大家可以参考博客:

http://www.open-open.com/lib/view/open1334628062874.html

http://www.ibm.com/developerworks/cn/java/j-lo-shiro/

我相信看完上边两篇播客后你已经对shiro有了了解,下边开始教大家如何使用shiro了

1.首先在web.xml中配置过滤器让请求资源经过 Shiro 的过滤处理,这与其它过滤器的使用类似

<!-- Spring ApplicationContext配置文件的路径,可使用通配符,多个路径用,号分隔 此参数用于后面的Spring Context Loader -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath*:/applicationContext-shiro.xml
    </param-value>
</context-param>

<!-- shiro security filter -->
<filter>
    <!-- 这里的filter-name要和spring的applicationContext-shiro.xml里的
            org.apache.shiro.spring.web.ShiroFilterFactoryBean的bean name相同 -->
    <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>

2.applicationContext-shiro.xml文件中

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:util="http://www.springframework.org/schema/util" 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
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
    <description>Shiro安全配置</description>

    <bean id="shiroSecurityFilter"
    <!--初始化资源-->
        class="cn.com.dhc.ec.web.admin.shiro.CustomShiroFilterFactoryBean">
         <!-- shiro的核心安全接口 在 Shiro 认证与授权处理过程中,提及到 Realm。Realm 可以理解为读取用户信息、角色及权限的 DAO。由于大多 Web 应用程序使用了关系数据库,因此实现 JDBC Realm 是常用的做法-->  
        <property name="securityManager" ref="securityManager" />
        <!-- 要求登录时的链接 -->
        <property name="loginUrl" value="/login.jhtml" />
        <!-- 登陆成功后要跳转的连接 -->
        <property name="successUrl" value="/index.jhtml" />
        <!-- 无权访问时重定向到登录页面 -->
        <property name="unauthorizedUrl" value="/login.jhtml" />
        <property name="filters">
            <util:map>
                <entry key="authc" value-ref="ecAuthenticationFilter" />
            </util:map>
        </property>
    </bean>

    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="realm" ref="storeShiroRealm" />
        <!-- Security缓存,只缓存角色和权限,不缓存认证信息 -->
        <property name="cacheManager">
            <bean class="org.apache.shiro.cache.MemoryConstrainedCacheManager"></bean>
        </property>
    </bean>

    <bean id="ecAuthenticationFilter" class="cn.com.dhc.ec.fw.web.shiro.EcAuthenticationFilter">
        <property name="redirectToSavedRequest" value="false"></property>
        <property name="errorJsonView">
            <bean
                class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">
                <property name="extractValueFromSingleKeyModel" value="false"></property>
            </bean>
        </property>
    </bean>

    <bean id="storeShiroRealm" class="cn.com.dhc.ec.web.admin.shiro.StoreShiroRealm">
        <property name="credentialsMatcher" ref="ecCredentialsMatcher"></property>
        <property name="authenticationCachingEnabled" value="false"></property>
    </bean>

    <!-- 密码判断器 -->
    <bean id="ecCredentialsMatcher" class="cn.com.dhc.ec.fw.web.shiro.EcPasswordCrypter">
    </bean>

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

    <bean
        class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="staticMethod"
            value="org.apache.shiro.SecurityUtils.setSecurityManager" />
        <property name="arguments" ref="securityManager" />
    </bean>

</beans>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值