目录
Spring MVC常用配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--查找使用构造型注解所标注的类,如@Component(组件),@Service(服务),@Controller(控制器),@Repository(数据仓库)-->
<context:component-scan base-package="com.github.cakin" use-default-filters="false">
<!-- annotation是对注解进行扫描 -->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:include-filter type="annotation"
expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
<!--使用aop的专用标签来完成相关的配置-->
<aop:config proxy-target-class="true"></aop:config>
<!--支持Shiro的注解-->
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager"/>
</bean>
<!--自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,
是spring MVC为@Controllers分发请求所必须的,即解决了@Controller注解使用的前提配置-->
<mvc:annotation-driven/>
<!--如果发送的请求不想通过controller,只想直接地跳转到目标页面,这时候就可以使用mvc:view-controller标签
https://www.jianshu.com/p/558981c597bd
-->
<mvc:view-controller path="/" view-name="index"/>
<!-- 默认的视图解析器 -->
<bean id="defaultViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="contentType" value="text/html"/>
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 控制器异常处理 -->
<bean id="exceptionHandlerExceptionResolver"
class="org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver">
</bean>
<!-- 自定义异常处理-->
<bean class="com.github.cakin.shiro.chapter12.web.exception.DefaultExceptionHandler"/>
</beans>
Spring shiro的常用配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:util="http://www.springframework.org/schema/util"
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
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!-- 缓存管理器 使用Ehcache实现 -->
<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<!-- 设置 ehcache 缓存的配置文件-->
<property name="cacheManagerConfigFile" value="classpath:ehcache.xml"/>
</bean>
<!-- 凭证匹配器 -->
<bean id="credentialsMatcher" class="com.github.cakin.shiro.chapter12.credentials.RetryLimitHashedCredentialsMatcher">
<!--驱动Spring执行带参数的构造器,注入缓存管理器-->
<constructor-arg ref="cacheManager"/>
<!-- 使用的hash算法-->
<property name="hashAlgorithmName" value="md5"/>
<!-- 迭代次数-->
<property name="hashIterations" value="2"/>
<!-- 以16进制存储-->
<property name="storedCredentialsHexEncoded" value="true"/>
</bean>
<!-- Realm实现 -->
<bean id="userRealm" class="com.github.cakin.shiro.chapter12.realm.UserRealm">
<!-- 注入用户服务-->
<property name="userService" ref="userService"/>
<!-- 注入凭证匹配器-->
<property name="credentialsMatcher" ref="credentialsMatcher"/>
<!-- 启动缓存功能-->
<property name="cachingEnabled" value="true"/>
<!-- 认证启动缓存功能-->
<property name="authenticationCachingEnabled" value="true"/>
<!-- 认证缓存名字-->
<property name="authenticationCacheName" value="authenticationCache"/>
<!-- 授权启动缓存功能-->
<property name="authorizationCachingEnabled" value="true"/>
<!-- 授权缓存名字-->
<property name="authorizationCacheName" value="authorizationCache"/>
</bean>
<!-- 会话ID生成器 -->
<bean id="sessionIdGenerator" class="org.apache.shiro.session.mgt.eis.JavaUuidSessionIdGenerator"/>
<!-- 会话Cookie模板,用于生产 Session ID Cookie 的模板 -->
<bean id="sessionIdCookie" class="org.apache.shiro.web.servlet.SimpleCookie">
<!--驱动Spring执行带参数的构造器,注入cookie的name-->
<constructor-arg value="sid"/>
<!-- 如果设置为 true,则客户端不会暴露给客户端脚本代码,使用 HttpOnly cookie 有助于减少某些类型的跨站点脚本攻击-->
<property name="httpOnly" value="true"/>
<!-- 设置 Cookie 的过期时间,秒为单位,默认 - 1 表示关闭浏览器就失效-->
<property name="maxAge" value="180000"/>
</bean>
<!-- 会话DAO -->
<bean id="sessionDAO" class="org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO">
<!-- 设置 Session 缓存名字,默认就是 shiro-activeSessionCache-->
<property name="activeSessionsCacheName" value="shiro-activeSessionCache"/>
<!-- 注入会话id生成器-->
<property name="sessionIdGenerator" ref="sessionIdGenerator"/>
</bean>
<!-- 会话验证调度器 -->
<bean id="sessionValidationScheduler" class="org.apache.shiro.session.mgt.quartz.QuartzSessionValidationScheduler">
<!-- 会话调度器的验证周期-->
<property name="sessionValidationInterval" value="1800000"/>
<!-- 注入会话管理器-->
<property name="sessionManager" ref="sessionManager"/>
</bean>
<!-- 会话管理器 -->
<bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
<!-- 设置会话的全局过期时间(毫秒为单位),默认 30 分钟-->
<property name="globalSessionTimeout" value="1800000"/>
<!-- 在会话过期时不想删除过期的会话,可设置为false,默认设置为true-->
<property name="deleteInvalidSessions" value="true"/>
<!-- 是否开启会话验证器,默认是开启的-->
<property name="sessionValidationSchedulerEnabled" value="true"/>
<!-- 注入会话验证调度器-->
<property name="sessionValidationScheduler" ref="sessionValidationScheduler"/>
<!-- 注入会话DAO-->
<property name="sessionDAO" ref="sessionDAO"/>
<!-- 是否启用 / 禁用 Session Id Cookie,默认是启用的;如果禁用后将不会设置 Session Id Cookie-->
<property name="sessionIdCookieEnabled" value="true"/>
<!-- 注入sessionIdCookie-->
<property name="sessionIdCookie" ref="sessionIdCookie"/>
</bean>
<!-- 安全管理器 -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<!-- 注入Realm-->
<property name="realm" ref="userRealm"/>
<!-- 注入会话管理器-->
<property name="sessionManager" ref="sessionManager"/>
<!-- 注入缓存管理器-->
<property name="cacheManager" ref="cacheManager"/>
</bean>
<!-- 相当于调用SecurityUtils.setSecurityManager(securityManager) -->
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="staticMethod" value="org.apache.shiro.SecurityUtils.setSecurityManager"/>
<property name="arguments" ref="securityManager"/>
</bean>
<!-- 基于Form表单的身份验证过滤器 -->
<bean id="formAuthenticationFilter" class="org.apache.shiro.web.filter.authc.FormAuthenticationFilter">
<!-- 注入用户名参-->
<property name="usernameParam" value="username"/>
<!-- 注入密码参数-->
<property name="passwordParam" value="password"/>
<!-- 设置登录URL-->
<property name="loginUrl" value="/login.jsp"/>
</bean>
<!-- Shiro的Web过滤器 -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<!-- 注入安全管理器-->
<property name="securityManager" ref="securityManager"/>
<!-- 设置登录URL-->
<property name="loginUrl" value="/login.jsp"/>
<!-- 设置未授权URl-->
<property name="unauthorizedUrl" value="/unauthorized.jsp"/>
<!--filters 属性用于自定义的过滤器,即 ini 配置中的 [filters] 部分-->
<property name="filters">
<util:map>
<entry key="authc" value-ref="formAuthenticationFilter"/>
</util:map>
</property>
<!--用于声明 url 和 filter 的关系,即 ini 配置中的 [urls] 部分-->
<property name="filterChainDefinitions">
<value>
/index.jsp = anon
/unauthorized.jsp = anon
/login.jsp = authc
/logout = logout
/** = user
</value>
</property>
</bean>
<!-- Shiro生命周期处理器-->
<!-- LifecycleBeanPostProcessor 用于在实现了 Initializable 接口的 Shiro bean 初始化时调用 Initializable 接口回调,
在实现了 Destroyable 接口的 Shiro bean 销毁时调用 Destroyable 接口回调。
如 UserRealm 就实现了 Initializable,而 DefaultSecurityManager 实现了 Destroyable。-->
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
</beans>
常用基础组件配置
<?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">
<!-- 数据库连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/shiro"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!-- Base DAO -->
<bean id="baseDao" abstract="true">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- DAO -->
<!-- 下面的parent表示:这个child的bean的父亲是id=baseDao的这个类-->
<!-- 用法参考:https://www.cnblogs.com/1540340840qls/p/7909430.html-->
<bean id="permissionDao" class="com.github.cakin.shiro.chapter12.dao.PermissionDaoImpl" parent="baseDao"/>
<bean id="roleDao" class="com.github.cakin.shiro.chapter12.dao.RoleDaoImpl" parent="baseDao"/>
<bean id="userDao" class="com.github.cakin.shiro.chapter12.dao.UserDaoImpl" parent="baseDao"/>
<!-- Service -->
<bean id="permissionService" class="com.github.cakin.shiro.chapter12.service.PermissionServiceImpl">
<property name="permissionDao" ref="permissionDao"/>
</bean>
<bean id="roleService" class="com.github.cakin.shiro.chapter12.service.RoleServiceImpl">
<property name="roleDao" ref="roleDao"/>
</bean>
<bean id="passwordHelper" class="com.github.cakin.shiro.chapter12.service.PasswordHelper">
<property name="algorithmName" value="md5"/>
<property name="hashIterations" value="2"/>
</bean>
<bean id="userService" class="com.github.cakin.shiro.chapter12.service.UserServiceImpl">
<property name="userDao" ref="userDao"/>
<property name="passwordHelper" ref="passwordHelper"/>
</bean>
</beans>