新建Java Daynamic Web项目

导入Spring、SpringMVC依赖包:

导入Spring & Spring MVC包(导入如下所有开发包):

Spring AOP依赖扩展包:

配置Spring :

1)修改web.xml导入“#contextLoaderListener”

配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"id="WebApp_ID" version="2.5"><display-name>shiro-web-01</display-name><!-- 配置Spring的 ContextLoaderListener --><!-- needed for ContextLoaderListener --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><!-- Bootstraps the root web application context before servlet initialization --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
</web-app>

View Code

2)在src下添加Spring Bean配置文件applicationContext.xml

配置Spring MVC

1)在web.xm中导入“#dispatcherservlet”

配置后web.xml文件内容为:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"id="WebApp_ID" version="2.5"><display-name>shiro-web-01</display-name><!-- 配置Spring的 ContextLoaderListener --><!-- needed for ContextLoaderListener --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><!-- Bootstraps the root web application context before servlet initialization --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 配置Spring MVC --><!-- The front controller of this Spring Web application, responsible for handling all application requests --><servlet><servlet-name>spring</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- <init-param><param-name>contextConfigLocation</param-name><param-value>location</param-value></init-param>--><load-on-startup>1</load-on-startup></servlet><!-- Map all requests to the DispatcherServlet for handling --><servlet-mapping><servlet-name>spring</servlet-name><url-pattern>/</url-pattern></servlet-mapping>
</web-app>

View Code

2)在WEB-INF下新建Spring MVC配置文件spring-servlet.xml,并添加spring mvc配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"><context:component-scan base-package="com.dx.shiro"></context:component-scan><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/"></property><property name="suffix" value=".jsp"></property></bean><mvc:annotation-driven></mvc:annotation-driven><mvc:default-servlet-handler />
</beans>

View Code

配置Shiro环境

1)导入shiro jar包

导入Shiro开发包:

或者通过pom.xml配置:

        <dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-all</artifactId><version>1.3.2</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.13</version></dependency>

2)配置web.xml,导入shiroFilter

可以参考

配置后web.xml内容为:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"id="WebApp_ID" version="2.5"><display-name>shiro-web-01</display-name><!-- 配置Spring的 ContextLoaderListener --><!-- needed for ContextLoaderListener --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><!-- Bootstraps the root web application context before servlet initialization --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 配置Spring MVC --><!-- The front controller of this Spring Web application, responsible for handling all application requests --><servlet><servlet-name>spring</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- <init-param> <param-name>contextConfigLocation</param-name> <param-value>location</param-value> </init-param> --><load-on-startup>1</load-on-startup></servlet><!-- Map all requests to the DispatcherServlet for handling --><servlet-mapping><servlet-name>spring</servlet-name><url-pattern>/</url-pattern></servlet-mapping><!-- Shiro配置 --><!-- ================================================================== Filters ================================================================== --><!-- Shiro Filter is defined in the spring application context: --><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>

View Code

3)配置Spring的配置文件中来配置Shiro,即在Src下来的applicationContext.xml中配置shiro

配置后的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"><!-- 1.配置SecurityManager --><!-- 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="sessionMode" value="native" /><property name="realm" ref="jdbcRealm" /></bean><!-- 2.配置CacheManager 2.1.配置ehcache的jar包及ehcache的配置文件 --><!-- 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"><!-- <property name="cacheManager" ref="ehCacheManager"/> --><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. --><!-- 3.配置Realm --><bean id="jdbcRealm" class="com.dx.shiro.realms.MyRealm"></bean><!-- 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. --><!-- 4.配置org.apache.shiro.spring.LifecycleBeanPostProcessor,可以自动的来调用配置在Spring IOC容器中的 shiro bean的生命周期方法。 --><bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" /><!-- 5.启用IOC容器中使用shiro的注解,但必须在配置了DefaultAdvisorAutoProxyCreator之后才可以使用 --><!-- Enable Shiro Annotations for Spring-configured beans. Only run after the lifecycleBeanProcessor has run: --><beanclass="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"depends-on="lifecycleBeanPostProcessor" /><beanclass="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"><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: --><!-- 6.配置shiroFilter 6.1. bean的id必须和web.xml中配置的DelegatingFilterProxy的<filter-name>一致 6.2. anon/authc是过滤器,anon允许匿名访问,authc需要认证才可以访问的页面。 --><bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"><property name="securityManager" ref="securityManager" /><property name="loginUrl" value="/login.jsp" /><property name="successUrl" value="/list.jsp" /><property name="unauthorizedUrl" value="/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> --><property name="filterChainDefinitions"><value>/login.jsp = anon# everything else requires authentication:/** = authc</value></property></bean>
</beans>

3.1)配置ehcache的jar包及ehcache的配置文件

ehcache.xml从hibernate中找:

将其拷贝到src下

ehcache.jar也可以从hibernate开发包中找到:

将其拷贝到WEB-INF/lib下,导入到项目中。

3.2)添加shiro realm

新建包com.dx.shiro.realms,在包下新建一个MyRealm.java

package com.dx.shiro.realms;import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.realm.Realm;public class MyRealm implements Realm {public AuthenticationInfo getAuthenticationInfo(AuthenticationToken arg0) throws AuthenticationException {// TODO Auto-generated method stubreturn null;}public String getName() {// TODO Auto-generated method stubreturn null;}public boolean supports(AuthenticationToken arg0) {// TODO Auto-generated method stubreturn false;}}

View Code

这里采用咱不实现具体的接口的方式。

3.3)在webcontent下添加页面

添加页面login.jsp,list.jsp,unauthorized.jsp

测试

此时访问网址:

其他任何页面都不允许访问。

Java-Shiro(三):Shiro与Spring MVC集成相关推荐

  1. Spring MVC集成slf4j-logback

    转自: Spring MVC集成slf4j-logback 1.  Spring MVC集成slf4j-log4j 关于slf4j和log4j的相关介绍和用法,网上有很多文章可供参考,但是关于logb ...

  2. Spring MVC集成slf4j-logback - 我想跟代码谈谈 - 博客频道 - CSDN.NET

    Spring MVC集成slf4j-logback - 我想跟代码谈谈 - 博客频道 - CSDN.NET

  3. Spring MVC集成Tiles使用方法

    Spring MVC集成Tiles使用方法 转载于:https://www.cnblogs.com/zhujiabin/p/5012129.html

  4. java springmvc https_【Java Web开发学习】Spring MVC 使用HTTP信息转换器

    [Java Web开发学习]Spring MVC 使用HTTP信息转换器 @ResponseBody和@RequestBody是启用消息转换的一种简洁和强大方式 消息转换(message conver ...

  5. 【Java Web开发学习】Spring MVC 拦截器HandlerInterceptor

    [Java Web开发学习]Spring MVC 拦截器HandlerInterceptor 转载:https://www.cnblogs.com/yangchongxing/p/9324119.ht ...

  6. Spring MVC 集成 jackson-dataformat-xml 问题

    Spring MVC 集成 jackson-dataformat-xml 问题 HttpMessageNotWritableException Could not write content 注:如果 ...

  7. Spring MVC集成Swagger2.0

    在集成Swagger之前,得先说说什么是Swagger,它是用来做什么的,然后再讲讲怎么集成,怎么使用,当然,在这之前,需要了解一下OpenAPI. OpenAPI OpenAPI 3.0规范定义了一 ...

  8. [Spring mvc 深度解析(三)] 创建Spring MVC之器

    第9章 创建Spring MVC之器 ​ 本章将分析Spring MVC自身的创建过程.首先分析Spring MVC的整体结构,然后具体分析每一层的创建过程. 1 整体结构介绍 Spring MVC中 ...

  9. Spring MVC集成Spring Data Reids和Spring Session实现Session共享

    说明:Spring MVC中集成Spring Data Redis和Spring Session时版本是一个坑点,比如最新版本的Spring Data Redis已经不包含Jedis了,需要自行引入. ...

最新文章

  1. C# 四舍五入round函数使用的代码
  2. mysql 分组 字符串_MySQL查询以字符串字段中的数字字符对行进行分组?
  3. js实现html页面倒计30秒,javascript实现简单页面倒计时
  4. 【.Net Micro Framework PortingKit – 12】SysTick驱动开发
  5. Android 8.0(29)---Android 8.0 获取当前的activity
  6. Java字符串equals()
  7. hdu 3853 概率dp
  8. EazyDraw for Mac(矢量图绘制软件)
  9. 数据挖掘中最易犯的10个错误,请绕行!
  10. 冠层、叶片和光系统尺度的日光诱导叶绿素荧光SIF模拟的辐射传输模型:SCOPE模型介绍
  11. 《动手学深度学习》学习总结
  12. 智能指针手表_反对智能手表
  13. sin的傅里叶变换公式_正弦信号傅里叶变换
  14. 遥远记忆中的美好时光
  15. 分治算法 循环比赛日程表
  16. MyBatis框架学习笔记01:初入MyBatis(一)
  17. PhotoShop CS6实现照片背景虚化效果
  18. OPA 论坛为流程控制设备接口扩展了开放性、互操作性标准 - 第一部分
  19. fail-fast 机制是什么?(详解)
  20. 设置CentOS开机启动程序及定时关机

热门文章

  1. python专科找工作难吗-本人小白,想学python,大专不知道好不好找工作?
  2. python编程100例头条-今日头条python面试题之编程篇
  3. 爬虫好学吗python-python爬虫容易学吗
  4. python从小到大的顺序输出-python从小到大的顺序输出
  5. python基础教程第二版下载-Python基础教程(第2版)
  6. rest-framework 视图
  7. 2018-2019 20165237网络对抗 Exp5 MSF基础应用
  8. Analysis of the Clustering Properties of the Hilbert Space-Filling Curve 论文笔记
  9. ReactNative环境配置的坑
  10. Pyhton学习——Day3