1.创建相关表

2.导入maven依赖

    <properties><java.version>1.8</java.version><spring.version>5.0.7.RELEASE</spring.version><aspectj.version>1.8.13</aspectj.version><cglib.version>3.2.6</cglib.version><mybatis.version>3.4.6</mybatis.version><mybatis-spring.version>1.3.2</mybatis-spring.version><mysql.version>5.1.46</mysql.version><c3p0.version>0.9.5.2</c3p0.version><jackson.version>2.9.3</jackson.version><slf4j.version>1.7.25</slf4j.version><shiro.version>1.3.0</shiro.version></properties><dependencies><!-- 缓存依赖 --><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-ehcache</artifactId><version>${shiro.version}</version></dependency>
​<!-- shiro jar包依赖 --><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-core</artifactId><version>${shiro.version}</version></dependency><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-web</artifactId><version>${shiro.version}</version></dependency><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-spring</artifactId><version>${shiro.version}</version></dependency>
​<!-- spring 相关开始 --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>${spring.version}</version></dependency><!-- aop 相关 --><dependency><groupId>org.aspectj</groupId><artifactId>aspectjrt</artifactId><version>${aspectj.version}</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>${aspectj.version}</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjtools</artifactId><version>${aspectj.version}</version></dependency><dependency><groupId>cglib</groupId><artifactId>cglib-nodep</artifactId><version>${cglib.version}</version></dependency><!-- jackson 相关 --><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>${jackson.version}</version></dependency><!-- mybatis 相关 --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>${mybatis.version}</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>${mybatis-spring.version}</version></dependency><dependency><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-core</artifactId><version>1.3.5</version></dependency><!-- c3p0 相关 --><dependency><groupId>com.mchange</groupId><artifactId>c3p0</artifactId><version>${c3p0.version}</version></dependency><!-- MySQL 驱动 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>${mysql.version}</version></dependency><!-- hibernate-validator 相关 --><dependency><groupId>org.hibernate</groupId><artifactId>hibernate-validator</artifactId><version>5.4.2.Final</version></dependency><!-- 日志相关 --><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>${slf4j.version}</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>jcl-over-slf4j</artifactId><version>${slf4j.version}</version></dependency><!-- 单元测试 --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><!-- servlet-api 相关依赖开始 --><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.0.1</version><scope>provided</scope></dependency><dependency><groupId>javax.servlet.jsp</groupId><artifactId>javax.servlet.jsp-api</artifactId><version>2.3.1</version><scope>provided</scope></dependency><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency><dependency><groupId>taglibs</groupId><artifactId>standard</artifactId><version>1.1.2</version></dependency><dependency><groupId>net.sf.json-lib</groupId><artifactId>json-lib</artifactId><version>2.4</version><classifier>jdk15</classifier></dependency></dependencies>

3.编写配置文件

applicationContext-mybatis.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" xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"><!-- 添加扫描所有的包 -->    <context:component-scan base-package="com.demo.service" /> <!-- 加载资源文件 --><context:property-placeholder location="classpath:jdbc.properties"/><!-- 配置c3p0连接池 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${jdbc.driver}" /><property name="jdbcUrl" value="${jdbc.url}" /><property name="user" value="${jdbc.user}" /><property name="password" value="${jdbc.password}" /><!-- 数据源个性化配置,根据实际项目需求进行: --><property name="initialPoolSize" value="3" /><property name="minPoolSize" value="3" /><property name="maxPoolSize" value="15" /><property name="maxConnectionAge" value="28800" /><!-- 最大闲置时间 单位秒 设置为6小时,主要目的避免mysql8小时陷阱 --><property name="maxIdleTime" value="21600" /></bean><!-- 配置SQLSessionFactoryBean --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 配置数据源 --><property name="dataSource" ref="dataSource" /><!-- 配置Mybatis配置文件位置 --><property name="configLocation" value="classpath:mybatis-config.xml" /><!-- 配置映射文件位置,该属性也可以在Mybatis配置文件中进行 --><property name="mapperLocations" value="classpath:mapper/*.xml" /></bean><!-- 把sqlSessionFactory自动注入到mapper --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.demo.dao" /><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /></bean><!-- 配置事务管理器 --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource" /></bean><!-- 定义事务通知以及事务属性 --><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="save*" propagation="REQUIRED"/><tx:method name="add*" propagation="REQUIRED"/><tx:method name="create*" propagation="REQUIRED"/><tx:method name="insert*" propagation="REQUIRED"/><tx:method name="update*" propagation="REQUIRED"/><tx:method name="edit*" propagation="REQUIRED"/><tx:method name="modify*" propagation="REQUIRED"/><tx:method name="merge*" propagation="REQUIRED"/><tx:method name="put*" propagation="REQUIRED"/><tx:method name="remove*" propagation="REQUIRED"/><tx:method name="delete*" propagation="REQUIRED"/><tx:method name="find*" propagation="SUPPORTS" read-only="true"/><tx:method name="get*" propagation="SUPPORTS" read-only="true"/><tx:method name="count*" propagation="SUPPORTS" read-only="true"/><tx:method name="list*" propagation="SUPPORTS" read-only="true"/><tx:method name="*" propagation="SUPPORTS" read-only="true"/></tx:attributes></tx:advice><!-- 定义事务切面 --><aop:config><aop:pointcut expression="execution(* com.demo.service..*.*(..))" id="pc1"/><aop:advisor advice-ref="txAdvice" pointcut-ref="pc1"/></aop:config><!-- 开启cglib代理方式 ,当目标类未实现接口时,利用cglib产生代理对象 --><aop:aspectj-autoproxy proxy-target-class="true"/></beans>

applicationContext-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" xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"><!-- web.xml中shiro的filter对应的bean --><!-- Shiro 的Web过滤器 --><bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"><property name="securityManager" ref="securityManager" /><!-- loginUrl认证提交地址,如果没有认证将会请求此地址进行认证,请求此地址将由formAuthenticationFilter进行表单认证 --><property name="loginUrl" value="/login.do" /><!-- 认证成功统一跳转到first.action,建议不配置,shiro认证成功自动到上一个请求路径 --><property name="successUrl" value="/index.do" /><!-- 通过unauthorizedUrl指定没有权限操作时跳转页面 --><property name="unauthorizedUrl" value="/nofunc.jsp" />
​<!-- 过虑器链定义,从上向下顺序执行,一般将/**放在最下边 --><property name="filterChainDefinitions"><value><!-- 对静态资源设置匿名访问 -->/static/* = anon/login.do = anon/toLogin.do=anon<!-- 过滤器对资源进行验证 -->/index.do = authc<!-- 请求 logout地址,shiro去清除session -->/logout.do = logout<!-- 具有指定的权限才能访问 -->/userManager.do = perms[user:manager]/roleManager.do = perms[role:manager]/toUserRole.do = perms[user:pression]<!-- /* = authc 所有url都必须认证通过才可以访问 -->/* = authc</value></property></bean><!-- securityManager安全管理器 --><bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"><property name="realm" ref="authenRealm" /><property name="cacheManager" ref="cacheManager" /><property name="sessionManager" ref="sessionManager"></property></bean><!-- realm --><bean id="authenRealm" class="com.demo.realm.AuthenRealms"><!-- 将凭证匹配器设置到realm中,realm按照凭证匹配器的要求进行散列 --><property name="credentialsMatcher" ref="credentialsMatcher" /></bean><!-- 凭证匹配器 --><bean id="credentialsMatcher"class="org.apache.shiro.authc.credential.HashedCredentialsMatcher"><property name="hashAlgorithmName" value="md5" /><property name="hashIterations" value="1" /></bean>
​<!-- 缓存的配置 --><bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager"><property name="cacheManagerConfigFile" value="classpath:shiro-ehcache.xml" /></bean>
​<!-- session管理器 --><bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager"><!-- 配置session的失效时长,单位是毫秒 --><property name="globalSessionTimeout" value="100000"></property><!-- 删除失效的session --><property name="deleteInvalidSessions" value="true"></property></bean>
</beans>

spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.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.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.2.xsd"><!-- 扫描基本包 --><context:component-scan base-package="com.demo.controller" />
​<!-- 开启aop,对类代理 --><aop:config proxy-target-class="true"></aop:config><!-- 开启shiro注解支持 --><beanclass="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"><property name="securityManager" ref="securityManager"></property></bean>
​<!-- 异常统一处理 --><bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"><property name="defaultErrorView" value="index"></property><property name="exceptionAttribute" value="ex"></property></bean><!-- 添加注解驱动 --><mvc:annotation-driven /><!-- 视图解析器 --><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/jsp/" /><property name="suffix" value=".jsp" /></bean><!-- 将springmvc不能处理的请求交给tomcat --><mvc:default-servlet-handler />
​
</beans>

jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.user=root
jdbc.password=root
pool.initSize=3
pool.minSize=3
pool.maxSize=15
pool.MaxIdleTime=256800 

log4j.properties

log4j.rootCategory=INFO, stdout
​
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %t %c{2}:%L - %m%n
​
log4j.category.org.springframework.beans.factory=DEBUG
log4j.category.org.springframework=DEBUG

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><settings><!-- 开启二级缓存 --><setting name="cacheEnabled" value="true"/><setting name="lazyLoadingEnabled" value="true"/><setting name="aggressiveLazyLoading" value="false"/></settings>
</configuration>

shiro-ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="../config/ehcache.xsd"><!--diskStore:缓存数据持久化的目录 地址  --><diskStore path="E:\workspace\ehcache" /><defaultCache maxElementsInMemory="1000" maxElementsOnDisk="10000000"eternal="false" overflowToDisk="false" diskPersistent="false"timeToIdleSeconds="120"timeToLiveSeconds="120" diskExpiryThreadIntervalSeconds="120"memoryStoreEvictionPolicy="LRU"></defaultCache>
</ehcache>
​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_3_0.xsd"id="WebApp_ID" version="3.0"><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><!-- 加载spring的核心配置文件 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring/applicationContext-*.xml</param-value></context-param><!-- 配置pringmvc的监听器 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 添加springmvc的核心控制器 --><servlet><servlet-name>springmvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring/spring-mvc.xml</param-value></init-param></servlet><servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>*.do</url-pattern></servlet-mapping>
​<!-- 配置shiro框架的过滤器 --><filter><filter-name>shiroFilter</filter-name><filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
​<!-- 设置true由servlet容器控制filter的生命周期 --><init-param><param-name>targetFilterLifecycle</param-name><param-value>true</param-value></init-param><!-- 设置spring容器filter的bean id,如果不设置则找与filter-name一致的bean --><init-param><param-name>targetBeanName</param-name><param-value>shiroFilter</param-value></init-param></filter><filter-mapping><filter-name>shiroFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><!-- 添加字符集过滤器 --><filter><filter-name>encoding</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param><init-param><param-name>forceEncoding</param-name><param-value>true</param-value></init-param></filter><filter-mapping><filter-name>encoding</filter-name><url-pattern>/*</url-pattern></filter-mapping>
​
</web-app>

具体Controller层、Service层、Dao层代码省略。

SSM整合shiro框架相关配置文件相关推荐

  1. SSM整合shiro权限框架

    一.SSM整合shiro框架 1.步骤 1.添加shiro框架需要的jar包,包括shiro-core.shiro-web.shiro-spring的关系依赖 <!-- shiro jar包依赖 ...

  2. SSM整合Shiro进行登陆认证和授权详细配置

    本篇博客将进行详细介绍Shiro+Spring+SpringMVC+Mybatis+数据库整合并进行登陆认证和授权详细配置. SSM的整合可以参考:https://blog.csdn.net/a745 ...

  3. SpringBoot2.0 整合 Shiro 框架,实现用户权限管理

    GitHub源码地址:知了一笑 https://github.com/cicadasmile/middle-ware-parent 一.Shiro简介 1.基础概念 Apache Shiro是一个强大 ...

  4. Idea中Spring整合MyBatis框架中配置文件中对象注入问题解决方案

    运行环境:Spring框架整合MaBitis框架 问题叙述: 在Spring配置文件applicationContext-mybatis.xml中配置好mybatis之后 <?xml versi ...

  5. ssm整合shiro

    1.导入shiro相应jar包,也可下载shiro-all.jar; 2.web.xml添加shiroFilter配置,类似于mvc <!-- shiro 安全过滤器--><filt ...

  6. 狂神说Java 之SpringBoot整合Shiro框架笔记!

    参考:https://www.bilibili.com/video/BV1NE411i7S8 1,通过·subject获取用户.还有session获取! 2,判断登录用户. 未注册 密码不对 账号被锁 ...

  7. SSM整合Shiro 身份验证及密码加密简单实现

    1.导入maven的相关依赖 <!-- shiro --><dependency><groupId>org.apache.shiro</groupId> ...

  8. SSM整合activiti框架

    首先在maven项目的pom.xml文件中引入activiti工作流的jar包: <dependency> <groupId>org.activiti</groupId& ...

  9. SSM中shiro的基本使用

    shiro 用以网站的授权和认证 配置: 一.shiro基本配置文件 所用的entity user和role 实体类 1 @Entity 2 @Table(name="USER_P" ...

最新文章

  1. fsum函数测试以及分析
  2. 观点速递:大模型落地产业,存在什么问题?
  3. 使用VMware桥接模式组建局域网测试MSMQ(二)
  4. Linux753权限,linux的chmod与chown命令详解
  5. 几个名校学霸、大厂前辈的原创公众号
  6. Spring Boot中使用多数据库
  7. oracle utf8 varchar,Oracle中字符集的类型决定varchar2的字符长度
  8. 配置了坐标还是找不到serv_为什么老人家总是这疼那疼,还找不到原因?是矫情还是另有原因...
  9. (转)用DynamicMethod提升ORM系统转换业务数据的性能
  10. Linux电驴客户端,ubuntu装电驴
  11. @Controller与@RestController有何区别
  12. 文强+光裕+唐骏,告诉我们什么?
  13. Git bash使用中...
  14. CentOS下通过代理安装rails
  15. Devexpress 各版本中文语言包
  16. 如何让iframe背景色透明
  17. FPGA实现实时运动目标检测verilog
  18. jsonp跨域获取数据
  19. 计算机外文文献PDF,computer network 计算机 网络 外文文献.pdf
  20. android隐藏虚拟按键的几种方式

热门文章

  1. 数学建模——逻辑回归模型Python代码
  2. 【Flask项目】sqlalchemy原生sql查询,返回字典形式数据
  3. [register]-TCR(Translation Control Register)寄存器详解
  4. optee中的thread_vector_table线程向量表
  5. [ARM-assembly]-ARM ASM内联汇编学习
  6. 【渗透测试】一次运气很好的文件上传
  7. 【安全实战】红队攻防技术
  8. hook虚表监控虚表
  9. 【Prometheus + Grafana】 使用 topk 在 grafana 绘制 前 n 个时间序列
  10. python爬取快代理IP并测试IP的可用性