SSM整合shiro完整版
来源:互联网 发布:数据结构图的实验报告 编辑:程序博客网 时间:2024/05/01 15:24
导包maven的pom文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.study</groupId> <artifactId>shiro</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>shiro Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> <scope>test</scope> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.3</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.25</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core</artifactId> <version>1.2.1</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-web</artifactId> <version>1.2.1</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-ehcache</artifactId> <version>1.2.1</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>1.2.1</version> </dependency> <dependency> <groupId>org.ehcache</groupId> <artifactId>ehcache</artifactId> <version>3.1.3</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>0.2.23</version> </dependency> <!-- spring的相关依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.3.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.3.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.3.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.3.9.RELEASE</version> </dependency> <!-- mybatis相关依赖 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.4</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.1</version> </dependency> <!-- AspectJ是一个面向切面的框架 --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.10</version> </dependency> <!-- 日志包 --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!-- jsp相关 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>3.0-alpha-1</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.2-b02</version> </dependency> <!-- 校验器,校验不能为空什么的 --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>5.2.4.Final</version> </dependency> <!-- JSP标签库 --> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <!-- 通用Mapper --> <dependency> <groupId>com.github.abel533</groupId> <artifactId>mapper</artifactId> <version>2.3.4</version> </dependency> <!-- Apache工具组件 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.3.2</version> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.10</version> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </dependency> <!-- json的相关的依赖 --> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.2</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.41</version> </dependency> </dependencies> <build> <plugins> <!-- java编译插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.2</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>2.6</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </build></project>
Spring配置 Spring-cfg
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" 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/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!-- 使用spring自带的占位符替换功能 --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <!-- 允许JVM参数覆盖 --> <!-- java -Djdbc.url=123 -jar xxx.jar --> <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" /> <!-- 忽略没有找到的资源文件 --> <property name="ignoreResourceNotFound" value="true" /> <!-- 配置资源文件 --> <property name="locations"> <list> <value>classpath:db.properties</value> </list> </property> </bean> <context:component-scan base-package="com.study"/> <!-- 配置数据库连接池,这里使用druid --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="maxActive" value="30" /> <property name="maxIdle" value="5" /> </bean></beans>
springMybatis配置文件
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <bean class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 数据源 --> <property name="dataSource" ref="dataSource" /> <!-- 配置Mybatis的全局配置文件 --> <property name="configLocation" value="classpath:mybatis/mybatis-config.xml" /> <!-- 配置mapper.xml文件 --> <property name="mapperLocations" value="classpath:mybatis/mappers/*.xml"/> <!-- 别名包 --> <property name="typeAliasesPackage" value="com.study.shiro.model" /> </bean> <!-- mapper接口的扫描器 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.study.shiro.mapping" /> </bean></beans>
Springmvc配置前后缀访问路径
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <mvc:annotation-driven/> <context:component-scan base-package="com.study.shiro.controller"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean></beans>
web.xml配置
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance http://www.springmodules.org/schema/cache/springmodules-cache.xsd http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="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" version="3.0" > <display-name>Archetype Created Web Application</display-name> <!-- spring监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 加载spring容器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/spring/application*.xml</param-value> </context-param> <!-- 字符集过滤器 --> <filter> <description>字符集过滤器</description> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <description>字符集编码</description> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <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/springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> <url-pattern>*.json</url-pattern> <url-pattern>*.jhtml</url-pattern> </servlet-mapping> <!-- shiro过滤器 --> <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> <!-- DelegatingFilterProxy作用是自动到Spring容器查找名字为shiroFilter的bean并把所有的Filter操作委托给它, 然后将shiro配置到Spring容器即可。 --> <welcome-file-list> <welcome-file>/admin/login.do</welcome-file> <!-- 设置session过期时间为一分钟 --> <session-config> <session-timeout>1</session-timeout> </session-config> </welcome-file-list></web-app>
以上是ssm配置,接下来整合shiro
<?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:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation="http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:property-placeholder location="classpath*:/conf.properties" ignore-resource-not-found="true" ignore-unresolvable="true" /> <!-- remember me 功能 --> <bean id="rememberMeCookie" class="org.apache.shiro.web.servlet.SimpleCookie"> <constructor-arg value="rememberMe" /> <property name="httpOnly" value="true" /> <property name="maxAge" value="2592000" /><!-- 30天 --> </bean> <!-- rememberMe管理器 --> <bean id="rememberMeManager" class="org.apache.shiro.web.mgt.CookieRememberMeManager"> <property name="cipherKey" value=" #{T(org.apache.shiro.codec.Base64).decode('4AvVhmFLUs0KTA3Kprsdag==')}" /> <property name="cookie" ref="rememberMeCookie" /> </bean> <!-- rememberMeParam,即rememberMe请求参数名,请求参数是boolean类型,true表示rememberMe。 --> <bean id="formAuthenticationFilter" class="org.apache.shiro.web.filter.authc.FormAuthenticationFilter"> <!-- 记住我input的名称 --> <property name="rememberMeParam" value="rememberMe" /> </bean> <!-- 这里是shiro拦截器的配置,用来拦截url。配置url及相应的拦截器之间的关系 --> <!-- 格式:“url=拦截器[参数],拦截器[参数] 例如: --> <!-- /admin/** = authc, roles[admin], perms["permission1"] --> <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <property name="securityManager" ref="securityManager" /> <property name="loginUrl" value="/admin/login.do" /> <!-- 登录页面 --> <property name="successUrl" value="/admin/main.do" /> <!-- 登录成功跳转的页面 --> <property name="unauthorizedUrl" value="/admin/login.do" /> <property name="filterChainDefinitions"> <value> <!--如果当前请求的url匹配下面的某个url模式,将会执行其配置的拦截器。 比如: anon拦截器表示匿名访问(即不需要登录即可访问) authc拦截器表示需要身份认证通过后才能访问 roles[admin]拦截器表示需要有admin角色授权才能访问 perms["user:create"]拦截器表示需要有"user:create"权限才能访问。 --> /logout.do = logout <!-- 退出登录,直接指定拦截路径,之后无需写退出登录的控制器,即controller --> /admin/login.do = anon /admin/syslogin.json = anon /admin/main.do = loginRoles /admin/** = anon <!-- /admin/** = authc --> /admin/roleLists.json = anon <!-- /admin/roleLists.json = perms["admin:system:role"] --> /** = user <!-- /** = user”表示访问该地址的用户是身份验证通过或RememberMe登录的都可以 --> </value> </property> <property name="filters"> <map> <entry key="loginRoles" value-ref="loginFilter" /> </map> </property> </bean> <!-- shiro授权管理器 --> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="realm" ref="authenticationRealm" /> <property name="cacheManager" ref="shiroCacheManager" /> <!-- 设置securityManager安全管理器的rememberMeManager --> <property name="rememberMeManager" ref="rememberMeManager" /> <property name="sessionManager" ref="sessionManager" /> </bean> <!-- 自定义授权类 --> <bean id="authenticationRealm" class="com.study.shiro.AuthenticationRealm"> </bean> <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="staticMethod" value="org.apache.shiro.SecurityUtils.setSecurityManager" /> <property name="arguments" ref="securityManager" /> </bean> <bean id="loginFilter" class="com.study.filter.LoginFilter"></bean> <!-- 启用缓存注解功能 --> <cache:annotation-driven cache-manager="cacheManager" /> <!-- cacheManager工厂类,指定ehcache.xml的位置 --> <bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation" value="classpath:/ehcache.xml" /> <property name="shared" value="true" /> </bean> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> <property name="cacheManager" ref="ehCacheManager" /> </bean> <bean id="shiroCacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager"> <property name="cacheManager" ref="ehCacheManager" /> </bean> <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"></bean> <!-- shiro 管理的session dao --> <bean id="sessionDAO" class="org.apache.shiro.session.mgt.eis.MemorySessionDAO" /> <!-- shiro session管理器 --> <bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager"> <property name="sessionDAO" ref="sessionDAO" /> <property name="sessionIdCookieEnabled" value="true"/> <property name="sessionListeners" ref="sessionListenerOne"></property> </bean> <!-- 自定义的session监听器 --> <bean id="sessionListenerOne" class="com.study.listener.SessionListenerOne"></bean></beans>
shiro前端设置
<shiro:user> 欢迎[<shiro:principal/>]登录,<a href="${pageContext.request.contextPath}/logout.do">退出</a> <br> </shiro:user> <shiro:hasPermission name="admin:system:user"> <input type="button" value="用户管理" onclick="window.location='${pageContext.request.contextPath}/admin/user_management.do'"> </shiro:hasPermission> <shiro:hasPermission name="admin:system:auth"> <input type="button" value="权限管理" onclick="window.location='${pageContext.request.contextPath}/admin/permission_management.do'"> </shiro:hasPermission> <shiro:hasPermission name="admin:system:role"> <input type="button" value="角色管理" onclick="window.location='${pageContext.request.contextPath}/admin/Role_management.do'"> </shiro:hasPermission>
**shiro是管理用户权限的,用户必须满足某些权限才显示,其相对应的内容,<shiro:hasPermission name="admin:system:user">
**
用户必须有admin:system:user这样的权限,这里才显示</shiro:hasPermission>
hasPermission标签 :
验证当前用户是否拥有指定权限。
2, lacksPermission标签 :
与hasPermission标签逻辑相反,当前用户没有制定权限时,验证通过。
3,hasAnyRole标签 :
验证当前用户是否属于以下任意一个角色。
4,hasRole标签 :
验证当前用户是否属于该角色。
5,lacksRole标签 :
与hasRole标签逻辑相反,当用户不属于该角色时验证通过。**
阅读全文
0 0
- SSM整合shiro完整版
- ssm整合shiro
- ssm+shiro+UEditor整合
- SSM+shiro整合
- 7、ssm整合shiro
- shiro与SSM项目整合
- shiro与SSM项目整合
- Maven+SSM+Shiro整合配置
- ssm整合shiro实现权限
- Maven+SSM+Shiro整合配置
- SSM+shiro+maven整合配置
- Maven+SSM+Shiro整合配置
- springmvc SSM java redis shiro ehcache 整合
- SSM springmvc mybatis 整合 bootstrap maven shiro
- 把Shiro整合到SSM框架中
- ssm+shiro整合 (未测试版本)
- SSM + Shiro 整合 (6)- Shiro 集成 Spring
- idea ssm整合shiro shiro的基本用法
- 9种高性能可用高并发的技术架构
- 欢迎使用CSDN-markdown编辑器
- 2017Linux安装node,linux安装npm,linux淘宝镜像,linux全局npm,linux安装pm2,linux安装git
- dos命令行总结
- POCO线程类分析
- SSM整合shiro完整版
- Mac升级到10.13.1 之后cocoapods出现问题的解决办法
- PSNR峰值信噪比和 DMOS算法介绍
- 深度机器学习56G视频资源分享
- myeclipse项目导入到eclipse
- 我的项目经验总结——CDN镜像:1(初探)
- 队列的顺序结构和链式结构定义
- Vmware安装CentOS7后访问不了外网
- 利用公网ip盒子搭建自己的kod云资源管理器