欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 金融 > springmvc用配置类替换xml配置

springmvc用配置类替换xml配置

2024/10/25 4:19:42 来源:https://blog.csdn.net/zwjzone/article/details/142046569  浏览:    关键词:springmvc用配置类替换xml配置

ssm基础项目整合

  • 前言
  • xml配置
    • mybatis.xml
    • spingmvc.xml
    • web.xml
  • 配置类
    • jdbc配置类
    • mybatis配置
    • SpringConfig配置
    • SpringMvcConfig配置
    • shiro配置
    • WebInit

前言

其实这中间用配置类,发现问题还挺多的,所以记录一下,尤其是针对shiro的配置。

xml配置

mybatis.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"><display-name>Archetype Created Web Application</display-name><!--配置前端控制器--><servlet><servlet-name>dispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:springmvc.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>dispatcherServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping><filter><filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping></web-app>

spingmvc.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:tx="http://www.springframework.org/schema/tx"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/mvchttps://www.springframework.org/schema/mvc/spring-mvc.xsd"><bean id="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/><property name="url" value="jdbc:mysql://localhost:3306/ssm?serverTimeZone=Asia/Shanghai"/><property name="username" value="root"/><property name="password" value="123456"/></bean><!-- SqlSessionFactory配置 --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"/><property name="configLocation" value="classpath:mybatis-config.xml"/></bean><!--    扫描mapper.xml,实现mapper接口的方法--><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/><property name="basePackage" value="zxy.study.ssm.mapper"/></bean><!-- 事务管理器配置 --><bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"/></bean><!-- 启用注解事务管理 --><tx:annotation-driven transaction-manager="txManager"/><context:component-scan base-package="zxy.study.ssm"/><!-- 视图解析器对象 --><bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/pages/"/><property name="suffix" value=".jsp"/></bean><!--保证既能访问到静态文件js,也能正常访问接口--><mvc:annotation-driven/><!--保证jsp中的src能正确加载到js静态文件--><mvc:resources mapping="/js/**" location="/js/"/><mvc:resources mapping="/css/**" location="/css/"/>
</beans>

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"><display-name>Archetype Created Web Application</display-name><!--配置前端控制器--><servlet><servlet-name>dispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:springmvc.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>dispatcherServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping><filter><filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping></web-app>

配置类

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;import java.util.List;
import java.util.Properties;@Configuration
//扫描组件
@ComponentScan({"qlq.exam.makeup.ssm"})
@EnableAspectJAutoProxy
//开启mvc注解驱动
@EnableWebMvc
public class SpringMvcConfig implements WebMvcConfigurer {//默认的servlet处理静态资源@Overridepublic void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {configurer.enable();}@Overridepublic void configureMessageConverters(List<HttpMessageConverter<?>> converters) {converters.add(new MappingJackson2HttpMessageConverter());}//配置异常处理器@Overridepublic void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) {SimpleMappingExceptionResolver simpleMappingExceptionResolver = new SimpleMappingExceptionResolver();Properties prop = new Properties();prop.setProperty("java.lang.ArithmeticException", "error");simpleMappingExceptionResolver.setExceptionMappings(prop);simpleMappingExceptionResolver.setExceptionAttribute("ex");resolvers.add(simpleMappingExceptionResolver);}@Beanpublic InternalResourceViewResolver viewResolver() {InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();viewResolver.setPrefix("/pages/");viewResolver.setSuffix(".jsp");return viewResolver;}
}

配置类

jdbc配置类

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;import java.util.HashMap;
import java.util.Map;public class JdbcConfig {@Value("${jdbc.driver}")private String driver;@Value("${jdbc.url}")private String url;@Value("${jdbc.username}")private String username;@Value("${jdbc.password}")private String password;//配置数据源@Beanpublic DruidDataSource dataSource() {DruidDataSource dataSource = new DruidDataSource();dataSource.setDriverClassName(driver);dataSource.setUrl(url);dataSource.setUsername(username);dataSource.setPassword(password);return dataSource;}//开启事务管理注解驱动@Beanpublic PlatformTransactionManager transactionManager(DruidDataSource dataSource) {DataSourceTransactionManager dst = new DataSourceTransactionManager(dataSource);return dst;}//加上配置,否则shiro里注入service无法正常使用@Beanpublic static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {return new PropertySourcesPlaceholderConfigurer();}
}

mybatis配置

import com.alibaba.druid.pool.DruidDataSource;
import com.github.pagehelper.PageInterceptor;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;import java.io.IOException;
import java.util.Properties;public class MybatisConfig {@Beanpublic PageInterceptor sqlSessionFactoryBean() {// 配置PageHelper分页插件PageInterceptor pageInterceptor = new PageInterceptor();Properties properties = new Properties();properties.setProperty("helperDialect", "mysql");properties.setProperty("reasonable", "true");properties.setProperty("supportMethodsArguments", "true");pageInterceptor.setProperties(properties);return pageInterceptor;}@Bean(name = "sqlSessionFactory")public SqlSessionFactoryBean sqlSessionFactoryBean(DruidDataSource dataSource, PageInterceptor pageInterceptor) {SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();sqlSessionFactoryBean.setDataSource(dataSource);sqlSessionFactoryBean.setTypeAliasesPackage("qlq.exam.makeup.ssm.entity");// 添加分页插件sqlSessionFactoryBean.setPlugins(pageInterceptor);return sqlSessionFactoryBean;}@Beanpublic MapperScannerConfigurer mapperScannerConfigurer() {MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();mapperScannerConfigurer.setBasePackage("qlq.exam.makeup.ssm.mapper");return mapperScannerConfigurer;}
}

SpringConfig配置

import org.springframework.context.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.EnableTransactionManagement;@Configuration
//扫描除控制层组件
@ComponentScan(value = "qlq.exam.makeup.ssm", excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, value = {Controller.class})
})
//开启事务管理
@EnableTransactionManagement
//导入jdbc配置文件
@PropertySource("classpath:jdbc.properties")
//导入配置类
@Import({JdbcConfig.class,MybatisConfig.class})
public class SpringConfig {}

SpringMvcConfig配置

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;import java.util.List;
import java.util.Properties;@Configuration
//扫描组件
@ComponentScan({"qlq.exam.makeup.ssm"})
@EnableAspectJAutoProxy
//开启mvc注解驱动
@EnableWebMvc
public class SpringMvcConfig implements WebMvcConfigurer {//默认的servlet处理静态资源@Overridepublic void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {configurer.enable();}@Overridepublic void configureMessageConverters(List<HttpMessageConverter<?>> converters) {converters.add(new MappingJackson2HttpMessageConverter());}//配置异常处理器@Overridepublic void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) {SimpleMappingExceptionResolver simpleMappingExceptionResolver = new SimpleMappingExceptionResolver();Properties prop = new Properties();prop.setProperty("java.lang.ArithmeticException", "error");simpleMappingExceptionResolver.setExceptionMappings(prop);simpleMappingExceptionResolver.setExceptionAttribute("ex");resolvers.add(simpleMappingExceptionResolver);}@Beanpublic InternalResourceViewResolver viewResolver() {InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();viewResolver.setPrefix("/pages/");viewResolver.setSuffix(".jsp");return viewResolver;}
}

shiro配置

import com.alibaba.druid.util.StringUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import qlq.exam.makeup.ssm.entity.Permission;
import qlq.exam.makeup.ssm.entity.User;
import qlq.exam.makeup.ssm.service.PermissionService;
import qlq.exam.makeup.ssm.service.UserService;import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;public class UserRealm extends AuthorizingRealm {@Autowiredprivate UserService userService;@Autowiredprivate PermissionService permissionService;@Override//授权protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {User user = (User) principalCollection.getPrimaryPrincipal();Permission permission = permissionService.getPermByUserId(user.getId());String perm = permission.getPerm();SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();if (!StringUtils.isEmpty(perm)){HashSet<String> roles = new HashSet<>();roles.add("admin");authorizationInfo.setRoles(roles);Set<String> set = new HashSet<>(Arrays.asList(perm.split(",")));authorizationInfo.setStringPermissions(set);}return authorizationInfo;//判断用户是否有执行某个操作的权限}@Override//认证protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {String principal = (String) token.getPrincipal();User user = userService.getUserByUserName(principal);//登录信息和数据库是否匹配if (!ObjectUtils.isEmpty(user)) {return new SimpleAuthenticationInfo(user, user.getPassword(),ByteSource.Util.bytes(user.getSalt()),getName());}return null;}
}
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import java.util.LinkedHashMap;
import java.util.Map;@Configuration
public class ShiroConfig {@Bean("shiroFilterFactoryBean")public ShiroFilterFactoryBean getFilterFactoryBean(DefaultWebSecurityManager securityManager) {ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();shiroFilterFactoryBean.setSecurityManager(securityManager);Map<String, String> filterMap = new LinkedHashMap<>();//创建一个filterMap来定义URL模式及其对应的安全设置。filterMap.put("/", "anon");//URL对所有人开放(不需要身份验证)。filterMap.put("/login", "anon");//URL对所有人开放(不需要身份验证)。filterMap.put("/**/*.js", "anon");filterMap.put("/**/*.jsp", "anon");filterMap.put("/**/*.css", "anon");filterMap.put("/**/*.html", "anon");filterMap.put("/**/*.png", "anon");//所有其他URL(/**)需要进行身份验证(authc),放在最下边,否则上边的放开的路径会被拦截。filterMap.put("/**", "authc");
//        shiroFilterFactoryBean.setLoginUrl("/pages/login.jsp");shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);return shiroFilterFactoryBean;}@Bean("securityManager")public DefaultWebSecurityManager getSecurityManager(UserRealm userRealm) {DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();defaultWebSecurityManager.setRealm(userRealm);return defaultWebSecurityManager;}@Beanpublic UserRealm userRealm() {UserRealm userRealm = new UserRealm();//修改凭证校验匹配器HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher();//设置加密算法为md5credentialsMatcher.setHashAlgorithmName("MD5");//设置散列次数,用于配置密码匹配器的安全设置,以确保密码的安全性和正确性credentialsMatcher.setHashIterations(1024);userRealm.setCredentialsMatcher(credentialsMatcher);return userRealm;}@Bean("lifecycleBeanPostProcessor")public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {return new LifecycleBeanPostProcessor();}@Beanpublic DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator() {DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();advisorAutoProxyCreator.setProxyTargetClass(true);return advisorAutoProxyCreator;}@Beanpublic AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();advisor.setSecurityManager(securityManager);return advisor;}
}

WebInit

类代替了web.xml配置文件

import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.filter.DelegatingFilterProxy;
import org.springframework.web.filter.HiddenHttpMethodFilter;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;import javax.servlet.*;
import java.util.EnumSet;/** 此配置类代替了web.xml配置文件** */
public class WebInit extends AbstractAnnotationConfigDispatcherServletInitializer {//设置一个配置类代替Spring的配置文件@Overrideprotected Class<?>[] getRootConfigClasses() {return new Class[]{SpringConfig.class};}//设置一个配置类代替SpringMvc的配置文件@Overrideprotected Class<?>[] getServletConfigClasses() {return new Class[]{SpringMvcConfig.class};}//设置SpringMVC的前端控制器Dispatcherservlet的url-pattern@Overrideprotected String[] getServletMappings() {return new String[]{"/"};}//设置当前的过滤器@Overrideprotected Filter[] getServletFilters() {//创建编码过滤器CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter("UTF-8", true);//创建处理请求方式的过滤器HiddenHttpMethodFilter hiddenHttpMethodFilter = new HiddenHttpMethodFilter();return new Filter[]{characterEncodingFilter, hiddenHttpMethodFilter};}//参考地址https://houbb.github.io/2016/08/11/shiro-21-intergrations-springmvc,不加这个配置shiro不能正常拦截使用@Overridepublic void onStartup(ServletContext servletContext) throws ServletException {super.onStartup(servletContext);FilterRegistration.Dynamic shiroFilter = servletContext.addFilter("shiroFilterFactoryBean", DelegatingFilterProxy.class);shiroFilter.setInitParameter("targetFilterLifecycle", "true");shiroFilter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), false, "/*");}
}

里边最费劲的就是shiro的配置,jdbc和webinit中的两处一定要配置,还有就是拦截路径的前后关系,否则都会出现问题。
springmvc+jsp+shiro和springmvc+thymeleaf+shiro完整基础项目

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com