shiro:集成Spring(四)
基于【加密及密码比对器(三)】项目改造
引入相关依赖环境
shiro-spring已经包含 shiro-core和shiro-web 所以这两个依赖可以删掉
<!--shiro继承spring依赖包-->
<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-spring</artifactId>
    <version>1.4.0</version>
</dependency>
构建shiro配置文件
替代shiro.ini文件
resources\spring-shiro.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">
    <!--注册自定义realm-->
    <bean id="myRealm" class="com.shiro.realm.MyRealm">
        <!--把UserService注册到realm-->
        <property name="userService" ref="userServiceImpl"></property>
        <!--声明密码比对器-->
        <property name="credentialsMatcher">
            <bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
                <property name="hashAlgorithmName" value="SHA-256" />
                <property name="storedCredentialsHexEncoded" value="false" />
                <property name="hashIterations" value="10000" />
            </bean>
        </property>
    </bean>
    <!--声明securityManager-->
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="realm" ref="myRealm"/>
    </bean>
    <!--shiroFilter 角色权限校验-->
    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <!--注入核心对象:securityManager-->
        <property name="securityManager" ref="securityManager" />
        <!--未登录跳转路径-->
        <property name="loginUrl" value="/user/login" />
        <!--没权限的跳转路径-->
        <property name="unauthorizedUrl" value="/user/error" />
        <property name="filterChainDefinitions">
            <value>
                /user/login = anon
                /getuser = anon
                /getrole = anon
                /user/delUser = authc,roles["admin","manager"]
                /user/getallUsers = authc
                /user/logout = logout
            </value>
        </property>
    </bean>
</beans>
把userserivce接口注入到remla中
com\shiro\realm\MyRealm.java
private UserService userService;
public void setUserService(UserService userService) {
    this.userService = userService;
}
在doGetAuthorizationInfo和doGetAuthenticationInfo中直接用userService调用相关方法
把shiro配置文件引入到applicationContext.xml
resources\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">
    <import resource="spring-mapper.xml"/>
    <import resource="spring-service.xml"/>
    <import resource="springmvc-servlet.xml"/>
    <import resource="spring-shiro.xml"/>
</beans>
修改web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <!--注册DispatcherServlet-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--关联一个springmvc的配置文件:【servlet-name】-servlet.xml-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </init-param>
        <!--启动级别-1-->
        <load-on-startup>5</load-on-startup>
    </servlet>
    <!--/ 匹配所有的请求;(不包括.jsp)-->
    <!--/* 匹配所有的请求;(包括.jsp)-->
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <!--注册shiroFilter
       shiroFilter要与spring-shiro.xml中的一致
       -->
    <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>
</web-app>
												
											shiro:集成Spring(四)的更多相关文章
- shiro集成spring&工作流程&DelegatingFilterProxy
		
1.集成Spring 参考文献: 新建web工程: ehcache-core来自Hibernate wen.xml <?xml version="1.0" encoding= ...
 - shiro学习(四、shiro集成spring+springmvc)
		
依赖:spring-context,spring-MVC,shiro-core,shiro-spring,shiro-web 实话实说:web.xml,spring,springmvc配置文件好难 大 ...
 - Shiro集成Spring
		
本篇博客主要讲述的是两者的集成.不涉及到各自的详细细节和功能. 因为官方给出的文档不够具体,对新手而言通过官方文档还不可以非常快的搭建出SpringShiro的webproject.本博客将通过实际的 ...
 - shiro 集成spring 配置 学习记录(一)
		
首先当然是项目中需要增加shiro的架包依赖: <!-- shiro --> <dependency> <groupId>org.apache.shiro</ ...
 - shiro 集成spring  使用  redis作为缓存 学习记录(六)
		
1.在applicationContext-redis.xml配置文件中增加如下: 申明一个cacheManager对象 用来注入到 shiro的 securityManager 属性 cac ...
 - Shiro 集成Spring 使用 redis时  使用redisTemplate替代jedisPool(五)
		
1.添加依赖架包: <dependency> <groupId>org.springframework.data</groupId> <artifactId& ...
 - Apache Shiro 集成Spring(二)
		
1.依赖: <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-cor ...
 - shiro与spring集成
		
简介 Apache Shiro 是 Java 的一个安全(权限)框架.主要提供了认证.授权.加密和会话管理等功能. Authentication:身份认证/登录,验证用户是不是拥有相应的身份:Auth ...
 - Shiro学习笔记四(Shiro集成WEB)
		
这两天由于家里出了点事情,没有准时的进行学习.今天补上之前的笔记 -----没有学不会的技术,只有不停找借口的人 学习到的知识点: 1.Shiro 集成WEB 2.基于角色的权限控制 3.基于权限的控 ...
 
随机推荐
- Jmeter接口测试之用户自定义变量(九)
			
在使用Jmeter做接口自动化测试中,经常会使用到公共的数据,那么就需要对这些公共的数据分离出来,不管是基于测试框架的思想,还是使用工具来进行做自动化测试,公共数据的分离首先是需要思考的.这里就以获取 ...
 - Spring - 事务管理概述
			
什么是事务管理? 第一个问题:什么是事务? 事务一般是相对数据库而言的,对于数据库一次操作就属于一个事务, 一次操作可以是几句 SQL 语句,也可以是若干行 JDBC 的 Java 语句.事务既然 ...
 - ARDUINO UNO数字引脚端口上电后不稳定状态。
			
ARDUINO UNO数字引脚端口上电后不稳定状态. 在使用4*4矩阵键盘时,遇到了输入端的电平无法稳定,一直被识别为高电平. 在发现这一问题后,首先检查程序是否出错.检查后发现程序没有任何问题. 于 ...
 - SQL实战(二)
			
一. 获取所有员工当前的manager,如果当前的manager是自己的话结果不显示,当前表示to_date='9999-01-01'.结果第一列给出当前员工的emp_no,第二列给出其manager ...
 - 无法像程序语言那样写SQL查询语句,提示“数据库中已存在名为 '#temp1' 的对象。”
			
if exists( select exp_count from tbl_expend where exp_valid ),exp_date,) ),) ) begin select exp_coun ...
 - scikit_learn分类器详解
			
1 分类 分类是将事物按特性进行分类,例如将手写数字图片分类为对应的数字. 1.1 MINIST数字图片集分类 MINST就是一个70000张规格较小的手写数字图片,如何将他们分类为对应 ...
 - NKOJ4238 天天爱跑步(【NOIP2016 DAY1】)
			
问题描述 小C同学认为跑步非常有趣,于是决定制作一款叫做<天天爱跑步>的游戏.<天天爱跑步>是一个养成类游戏,需要玩家每天按时上线,完成打卡任务. 这个游戏的地图可以看作一棵包 ...
 - CodeForces - 817B(分类讨论 + 排列组合)
			
题目链接 思路如下 这一题是:最菜的队伍只有三个人组成,我们只需对排序后的数组的 前三个元素进行分类讨论即可: a[3] != a[2] && a[3] != ar[1] a[3] = ...
 - 使用git上传代码到GitHub
			
1.安装git git在Windows上安装很简单,在官网下载git的安装包后打开,然后一路next就好.安装完git之后,在文件夹中右击鼠标,出现Git Bash Here就表示安装完成了. 选择G ...
 - CentOS虚拟机开机显示多内核版本
			
在CentOS进行系统更新后,会保留旧版本的内核.所以,在每次启动时,会有多个内核选项,可以手动删除不用的旧版本内核. 1.查看当前系统内核版本 #uname -a 2.查看系统中存在的全部内 ...