springboot 干掉 web.xml

原始的web.xml里有4个配置,上篇文章已经把springmvc的解决了

 

1. spring redis session的filter

2. shiro的filter

3. 编码字符集filter       

4. 和一个监听器

代码如下,其中第1,3,4个默认已经集成,不需要再配置,

<?xml version="1.0" encoding="UTF-8"?>  
 <web-app xmlns="http://java.sun.com/xml/ns/javaee"  
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
 version="2.5">  
<!-- needed for ContextLoaderListener -->
    
<filter>
    <filter-name>springSessionRepositoryFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>springSessionRepositoryFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  
    <!-- <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param> -->

    <!-- Bootstraps the root web application context before servlet initialization -->
     <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <!-- The front controller of this Spring Web application, responsible for handling all application requests -->
    <servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Map all requests to the DispatcherServlet for handling -->
     <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>/</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>
    字符集转码
    <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>

 

需要配置的是 shiro的过滤器

@Bean
    public FilterRegistrationBean<DelegatingFilterProxy> filterRegistrationBean(){
        FilterRegistrationBean<DelegatingFilterProxy> filterRegistrationBean = new FilterRegistrationBean<>();
        // 新建一个代理对象

DelegatingFilterProxy proxy
= new DelegatingFilterProxy(); proxy.setTargetBeanName("shiroFilter"); proxy.setTargetFilterLifecycle(true); //注册, filterRegistrationBean.setFilter(proxy);
// //只要经过前端过滤器的,也都经过这个过滤器 "dispatcherServlet” Collection
<String> servletNames = new ArrayList<String>(); servletNames.add(DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_BEAN_NAME); //默认是"dispatcherServlet" filterRegistrationBean.setServletNames(servletNames);
    //上面这段,测试时不加也可以,直接返回也能得到filter
return filterRegistrationBean; }

 

另外如果发现mybatis出现 查询结果toomany的异常,检测数据库的结果是否是唯一,比如我添加了两条重复的作者和yaoid的解释,就报这个错误。

posted @ 2021-02-22 16:50  琴声清幽  阅读(109)  评论(0编辑  收藏  举报