maven springboot根据环境profile设置请求地址是否允许访问

1.情景展示

   对于不同环境使用不同配置文件,大家可能用的很溜,现在有个需求就是:

  特定请求只对特定环境可见,比方说:请求地址是/test/*,只允许开发环境和测试环境访问,禁止生产环境访问。如何实现?

  有人可能会想到:设置访问密码,此方案可行,但是如果对方知道密码,还是能够访问得到;

  这个问题困扰了我很久,网上也没有现成的东西,今天想到了答案:

  可以结合过滤器实现。

2.准备工作

  我的多环境使用方式可能与大众不同,所以,最好看一下。

  环境切换的决定权交由pom.xml profile标签来管理;

  测试环境、生产环境等,都在pom.xml中通过添加profile标签实现;

  在这里起第二关键作用的就是:environment标签,这是我自己定义的,名称和值可以随便写,只要你一会能对应上就行。

  在配置文件中引入设置自定义属性,并引用上述标签的值。

 

  yml文件引用pom.xml的属性,使用@@,properties文件使用${}。 

3.解决方案

  SpringBoot配置Filter过滤器共有两种实现方式,这里只介绍一种,重点不是实现方式。

  创建过滤器

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import java.io.IOException;
/**
 * 环境过滤器
 * @description: 根据环境来设置可访问的请求
 *  特定请求地址只允许特定环境访问
 * @author: Marydon
 * @date: 2020-12-27 8:55
 * @version: 1.0
 * @email: marydon20170307@163.com
 */
@Slf4j
@Component
public class ProfileAuthFilter implements Filter {
    // @Value从application-*.xml文件获取
    // 当前运行的环境
    @Value("${runtimeEnvironment}")
    private String RUNTIME_ENVIRONMENT;

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        log.debug("开始环境校验...");

        if ("test".equals(this.RUNTIME_ENVIRONMENT) || RUNTIME_ENVIRONMENT.startsWith("dev")) { // 开发环境或测试环境
            log.info("校验通过,允许访问!");
            // 调用拦截器链(继续往下走)
            chain.doFilter(request, response);
        } else {
            log.error("校验失败,禁止访问!");
            request.getRequestDispatcher("/404.do").forward(request, response);
        }

    }
}

  这个过滤器的关键点在于:

  从配置文件中取得当前运行环境的值;

  开发环境和测试环境正常调用Controller,其它环境转发到404请求上。

  添加过滤器总配置

import com.xyh.bill.web.filter.ProfileAuthFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
/**
 * url过滤器
 * @description: 对指定请求地址进行过滤,匹配上就进入对应的过滤器
 * @author: Marydon
 * @date: 2020-12-27 8:54
 * @version: 1.0
 * @email: marydon20170307@163.com
 */
@Configuration
public class FilterConfig {
    // 注入环境认证过滤器
    @Resource
    private ProfileAuthFilter profileFilter;

    /*
     * 把自定义过滤器包装成Spring的一个Bean
     * @attention:
     * @date: 2020年12月27日 0027 9:20
     * @param:
     * @return: org.springframework.boot.web.servlet.FilterRegistrationBean
     */
    @Bean
    public FilterRegistrationBean registerProfileAuthFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(profileFilter);
        // 设置拦截路径(只过滤以test开头的请求)
        registration.addUrlPatterns("/test/*");
        registration.setName("authFilter");
        //值越小,Filter越靠前
        registration.setOrder(1);
        return registration;
    }

    //如果有多个Filter,再写一个public FilterRegistrationBean registerOtherFilter(){...}即可。
}

  引入具体的过滤器,并定义该过滤器拦截的请求地址规则。

  配置404请求及跳转页面。

4.效果展示

  测试环境

  响应结果:

  生产环境

  响应结果:

 

2020-12-28

5.可能会出现的异常

  将项目打成war包部署到tomcat上,可能会抛出的异常:

  如果是这种异常的话,解决办法见文末推荐。

写在最后

  哪位大佬如若发现文章存在纰漏之处或需要补充更多内容,欢迎留言!!!

 相关推荐:

原文地址:https://www.cnblogs.com/Marydon20170307/p/14198028.html