spring mvc 集成 shiro 时 权限配置类无法入注service
配置文件
application.xml 配置启动监听spring
spring-mvc.xml 配置 springmvc功能
spring-shiro.xml 配置 shiro 功能
在启动的过程中 有人 将 spring 监听写成了 spring-mvc ,在启动 springmvc 功能时 又写了一次 spring-mvc.xml 导至初始化两次,定时 段点类跑两次,不信自己试,、
这三个文件不冲突的配置顺序为:
application.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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--本文件 用于 SPRING 监听 和 SPRING MVC 分开加载,避免 spring-mvc.xml 加载两次-->
<!-- 导入配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- druid 数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
spring-mvc.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" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!-- 使用spring组件扫描@controller -->
<mvc:annotation-driven/>
<context:component-scan base-package="com.*"/>
<!-- 通过annotation-driven可以替代下边的处理器映射器和适配器 -->
<!-- 自动注解 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
<!--解决乱码问题-->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >
<property name="messageConverters">
<list>
<bean class = "org.springframework.http.converter.StringHttpMessageConverter">
<property name = "supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
</list>
</property>
</bean>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!--spring 拦截器-->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.utils.MyInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
<!--初始化类 只加载一次-->
<bean id="initclass" class="com.utils.MyInit"/>
<!--加载自定义 proierties 文件-->
<util:properties id="config" location="classpath:config.properties"/>
</beans>
spring-shiro.xml 正常配置shiro 细节如下
<?xml version="1.0" encoding="UTF-8"?>**<import resource="classpath:applicationContext.xml"/>
<!-- 使用spring组件扫描@service -->
<!--<context:component-scan base-package="com.shop.system.login"/>-->
<!-- 自定义域realm -->
<bean id="custom_Realm" class="com.utils.MyShiroRealm">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>**
<!-- 安全管理器 ref对象-->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="custom_Realm"/>
</bean>
<!-- shiro filter -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<!-- 安全管理器必须的 -->
<property name="securityManager" ref="securityManager"/>
<!-- 身份认证失败 认证提交的地址 -->
<property name="loginUrl" value="/index.jsp"/>
<!-- 权限认证失败 没有权限认证提交的地址 -->
<property name="unauthorizedUrl" value="/unauthorized.jsp"/>
<!-- Shiro连接约束配置,即过滤链的定义 -->
<property name="filterChainDefinitions">
<value>
<!-- 对静态资源设置匿名访问 -->
/login = anon
<!-- /** = authc 所有url都必须认证通过才可以访问 -->
/admin* = authc
</value>
</property>
</bean>
<!-- Shiro生命周期处理器 -->
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"></bean>
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager"/>
</bean>
MyShiroRealm 认证类注入写法
@Autowired
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
每一回写东西,写多了你们会觉得烦,不精简,写少了怕你们看不明白