基于【加密及密码比对器(三)】项目改造

引入相关依赖环境

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(四)的更多相关文章

  1. shiro集成spring&工作流程&DelegatingFilterProxy

    1.集成Spring 参考文献: 新建web工程: ehcache-core来自Hibernate wen.xml <?xml version="1.0" encoding= ...

  2. shiro学习(四、shiro集成spring+springmvc)

    依赖:spring-context,spring-MVC,shiro-core,shiro-spring,shiro-web 实话实说:web.xml,spring,springmvc配置文件好难 大 ...

  3. Shiro集成Spring

    本篇博客主要讲述的是两者的集成.不涉及到各自的详细细节和功能. 因为官方给出的文档不够具体,对新手而言通过官方文档还不可以非常快的搭建出SpringShiro的webproject.本博客将通过实际的 ...

  4. shiro 集成spring 配置 学习记录(一)

    首先当然是项目中需要增加shiro的架包依赖: <!-- shiro --> <dependency> <groupId>org.apache.shiro</ ...

  5. shiro 集成spring 使用 redis作为缓存 学习记录(六)

    1.在applicationContext-redis.xml配置文件中增加如下: 申明一个cacheManager对象 用来注入到  shiro的   securityManager 属性  cac ...

  6. Shiro 集成Spring 使用 redis时 使用redisTemplate替代jedisPool(五)

    1.添加依赖架包: <dependency> <groupId>org.springframework.data</groupId> <artifactId& ...

  7. Apache Shiro 集成Spring(二)

    1.依赖: <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-cor ...

  8. shiro与spring集成

    简介 Apache Shiro 是 Java 的一个安全(权限)框架.主要提供了认证.授权.加密和会话管理等功能. Authentication:身份认证/登录,验证用户是不是拥有相应的身份:Auth ...

  9. Shiro学习笔记四(Shiro集成WEB)

    这两天由于家里出了点事情,没有准时的进行学习.今天补上之前的笔记 -----没有学不会的技术,只有不停找借口的人 学习到的知识点: 1.Shiro 集成WEB 2.基于角色的权限控制 3.基于权限的控 ...

随机推荐

  1. Android菜单(menu)

    Android  菜单 我们继续来进行学习,今天写一下在软件中用的还算较多的菜单. 1.Menu 菜单,很显然,作用就是点击不同的选项触发不同的方法.现在在安卓使用中推荐使用ActionBar,但这里 ...

  2. GitHub也会断供:美国制裁地区帐号都受限,毫无预警,个人页面直接404

    请注意,GitHub也有断供危机. 如果你有GitHub私有库,是时候重新思考安全性,也是时候制定备份策略. 这不是杞人忧天,也不只温馨提示,而是已经发生的事实. 一位伊朗程序员,一觉醒来GitHub ...

  3. 用卷积神经网络和自注意力机制实现QANet(问答网络)

    欢迎大家关注我们的网站和系列教程:http://www.tensorflownews.com/ ,学习更多的机器学习.深度学习的知识! 在这篇文章中,我们将解决自然语言处理(具体是指问答)中最具挑战性 ...

  4. CDN 内容分发

    1,传统架构访问服务器资源: www.aiyuesheng.com/page/logo.png 这是部署在服务器上的一张图片,因为服务器部署在上海,所以在上海或周边的人访问要稍微快一点,但是,若是云南 ...

  5. implements Serializable

    implements Serializable 1. 序列化和反序列化 序列化: 把对象转换为字节序列的过程称为对象的序列化. 反序列化: 把字节序列恢复为对象的过程称为对象的反序列化. 在Java和 ...

  6. Fiddler插件---将Mapi请求自动转为HTTPRunner测试用例(YAML格式)

    背景 继之前鼓捣出了Mapi解密插件之后,在团队内已经使用了三年之久,一跃成为团队最爱欢迎的测试工具之一(加个之一,低调谦虚一点). 随着团队推行HttpRunner搞接口自动化:编写和维护Case带 ...

  7. 万字长文带你入门Zookeeper!!!

    导读 文章首发于微信公众号[码猿技术专栏],原创不易,谢谢支持. Zookeeper 相信大家都听说过,最典型的使用就是作为服务注册中心.今天陈某带大家从零基础入门 Zookeeper,看了本文,你将 ...

  8. php静态变量的运用

    <?php $count = 5; function get_count() { static $count = 0; return $count++; } echo $count; echo ...

  9. Git常用命令(一)

    $ git init 初始化仓库(会生成一个隐藏文件.git) $ git add + (文件名) 实现对指定文件的跟踪 $ git commit 提交更新$ git clone + URL 克隆远程 ...

  10. Python设计模式(6)-原型模式

    import copy class Employee: age = 10 employee_name = 'zxc' class Company: name = '' num = 0 def __in ...