shiro 整合spring

来源:互联网 发布:ubuntu 16.04 syslog 编辑:程序博客网 时间:2024/06/06 07:11

Shiro 集成 Spring
1. 加入 Spring 的开发环境.
2. 加入 SpringMVC 的配置.

  1. Spring 集成 Shiro.
1). 打开 shiro\shiro-root-1.2.2\samples\spring\src\main\webapp\WEB-INF\applicationContext.xml 文件.2). 加入 ehcache-core-2.4.3.jar3). 自定义的 Realm 需要继承 org.apache.shiro.realm.AuthorizingRealm 类①. 其中 doGetAuthorizationInfo 方法用于授权。 doGetAuthenticationInfo 方法用于认证.

4). 在 web.xml 文件中配置 Shiro 的 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>
  1. 登陆的流程:
    1). 需要发送请求到 SpringMVC 的 Handler 或 Struts2 的 Action 或 Servlet. 而不是直接到 Shiro 的某一个组件.
    2). 在 Handler 的方法中完成:
    ①. 获取前端输入的用户名和密码
    ②. 其他操作来自于 QuickStart 中的代码.

3). 完成 realm 的 doGetAuthenticationInfo 方法
4). 还可以做得: 密码的盐值加密. 根据用户在数据表中的情况抛出对应的异常.

5). 密码的加密: 可以为当前的 Realm 配置一个 CredentialsMatcher 类型的 bean.

6). 加盐值: 在 doGetAuthenticationInfo 方法构建返回值对象时使用如下构造器即可.

SimpleAuthenticationInfo(Object principal, Object hashedCredentials,    ByteSource credentialsSalt, String realmName)

其中 credentialsSalt 参数可以调用 ByteSource.Util.bytes(salt); 工具方法来生成
7). 如何计算盐值加密后的密码呢 ?

     new SimpleHash(hashAlgorithmName, credentials, salt, hashIterations);

8). 配置受保护的资源: 使用 shiro 内置的过滤器.
anon
authc
logout

9). 注意: URL 权限采取第一次匹配优先的方式

10). 使用 shiro 的标签来隐藏受保护的资源
在jsp页面可以使用如下taglib指令加入shiro标签

<%@ taglib prefix ="shiro" uri="http://shiro.apache.org/tags" %>

使用shiro标签保护资源

<shiro:hasRole name= "admin">     <a href="admin.jsp"> To admin</a > <br>< br>

11). 使用 shiro 的注解来进行权限的保护.
在java方法中可以加入注解的方式对java方法进行权限保护
@RequiresRoles ({ “admin”})

在spring配置shiro的信息

     <!-- 配置 cacheManager  -->     <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager" >        <property name= "cacheManagerConfigFile" value="classpath:ehcache.xml" />    </bean >    <!-- 配置 Realm 实例. 以完成自定义的认证和授权 -->     <bean id="realm"           class= "com.atguigu.shiro.spring.MyRealm" >            <property name= "credentialsMatcher">               <bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher" >                    <property name= "hashAlgorithmName" value="MD5" ></property>                    <property name= "hashIterations" value="1024" ></property>               </bean>           </property>        </bean>     <!-- 配置 SecurityManager -->     <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager" >        <property name= "cacheManager" ref="cacheManager" />        <property name= "realm" ref ="realm"/>    </bean >    <!-- 配置  LifecycleBeanPostProcessor 实例. 会自动的调用 Shiro 的 bean 的 init 和 destroy 方法 -->    <bean id="lifecycleBeanPostProcessor"         class ="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>     <!-- 启用 shiro 注解的两个 bean. 注意: 必须在配置 LifecycleBeanPostProcessor 的前提下使用 -->     <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 ><!--创建工厂方法为filterChainDefinitionMap属性创建map--><bean id="filterChainDefinitionMap" factory-bean="filterChainDefinitionMapBuilder"        factory-method="buildFilterChainDefinitionMap"></bean>    <!-- 配置 WEB 环境下的 Shiro Filter! 将配置哪些资源被拦截, 哪个是登陆页面, 如何登出 -->    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean" >        <property name= "securityManager" ref="securityManager" />        <!-- 配置登陆页面 -->        <property name= "loginUrl" value ="/login.jsp"/>        <!-- 配置没有权限访问时的响应页面 -->        <property name= "unauthorizedUrl" value="/unauthorized.jsp" />    <!-- 可以使用实例工厂方法的方式为 ShiroFilterFactoryBean 的  filterChainDefinitionMap 属性配置 bean-->        <!--        <property name="filterChainDefinitionMap" ref="filterChainDefinitionMap"></property>        -->        <!-- 配置哪些面需要被拦截 -->        <property name= "filterChainDefinitions" >            <value>                # 匿名即可访问的资源                /login.jsp = anon                / shiro-login=anon                #登出                /logout=logout                /admin.jsp = roles[ admin]                /user.jsp = roles[user]                # 其他页面必须经过授权才可以访问                /** = authc            </value>        </property>    </bean >
0 0
原创粉丝点击