springboot2如何禁用自带tomcat的session功能

目录
  • 禁用自带tomcat的session功能
  • 禁用内置Tomcat的不安全请求方法

禁用自带tomcat的session功能

微服务下的各个服务都是无状态的,所以这个时候tomcat的session管理功能是多余的,即时不用,也会消耗性能,关闭后tomcat的性能会有提升,但是springboot提供的tomcat没有配置选项可以直接关闭,研究了一下,tomcat默认的session管理器名字叫:StandardManager,查看tomcat加载源码发现,如果context中没有Manager的时候,直接new StandardManager(),源码片段如下:

               Manager contextManager = null;
                Manager manager = getManager();
                if (manager == null) {
                    if (log.isDebugEnabled()) {
                        log.debug(sm.getString("standardContext.cluster.noManager",
                                Boolean.valueOf((getCluster() != null)),
                                Boolean.valueOf(distributable)));
                    }
                    if ((getCluster() != null) && distributable) {
                        try {
                            contextManager = getCluster().createManager(getName());
                        } catch (Exception ex) {
                            log.error(sm.getString("standardContext.cluster.managerError"), ex);
                            ok = false;
                        }
                    } else {
                        contextManager = new StandardManager();
                    }
                }

                // Configure default manager if none was specified
                if (contextManager != null) {
                    if (log.isDebugEnabled()) {
                        log.debug(sm.getString("standardContext.manager",
                                contextManager.getClass().getName()));
                    }
                    setManager(contextManager);
                }

为了不让tomcat去new自己的管理器,必须让第二行的getManager()获取到对象,所以就可以从这里入手解决,我的解决办法如下:自定义一个tomcat工厂,继承原来的工厂,context中加入自己写的manager

@Component
public class TomcatServletWebServerFactorySelf extends TomcatServletWebServerFactory {
    protected void postProcessContext(Context context) {
        context.setManager(new NoSessionManager());
    }
}
public class NoSessionManager extends ManagerBase implements Lifecycle {
    @Override
    protected synchronized void startInternal() throws LifecycleException {
        super.startInternal();
        try {
            load();
        } catch (Throwable t) {
            ExceptionUtils.handleThrowable(t);
            t.printStackTrace();
        }
        setState(LifecycleState.STARTING);
    }

    @Override
    protected synchronized void stopInternal() throws LifecycleException {
        setState(LifecycleState.STOPPING);
        try {
            unload();
        } catch (Throwable t) {
            ExceptionUtils.handleThrowable(t);
            t.printStackTrace();
        }
        super.stopInternal();
    }

    @Override
    public void load() throws ClassNotFoundException, IOException {
        log.info("HttpSession 已经关闭,若开启请配置:seeyon.tomcat.disableSession=false");
    }

    @Override
    public void unload() throws IOException {}
    @Override
    public Session createSession(String sessionId) {
        return null;
    }

    @Override
    public Session createEmptySession() {
        return null;
    }

    @Override
    public void add(Session session) {}
    @Override
    public Session findSession(String id) throws IOException {
        return null;
    }
    @Override
    public Session[] findSessions(){
        return null;
    }
    @Override
    public void processExpires() {}
}

两个类解决问题,这样通过request获取session就是空了,tomcat摆脱session这层处理性能有所提升。

禁用内置Tomcat的不安全请求方法

起因:安全组针对接口测试提出的要求,需要关闭不安全的请求方法,例如put、delete等方法,防止服务端资源被恶意篡改。

用过springMvc都知道可以使用@PostMapping、@GetMapping等这种注解限定单个接口方法类型,或者是在@RequestMapping中指定method属性。这种方式比较麻烦,那么有没有比较通用的方法,通过查阅相关资料,答案是肯定的。

tomcat传统形式通过配置web.xml达到禁止不安全的http方法

    <security-constraint>
       <web-resource-collection>
          <url-pattern>/*</url-pattern>
          <http-method>PUT</http-method>
       <http-method>DELETE</http-method>
       <http-method>HEAD</http-method>
       <http-method>OPTIONS</http-method>
       <http-method>TRACE</http-method>
       </web-resource-collection>
       <auth-constraint>
       </auth-constraint>
    </security-constraint>
    <login-config>
      <auth-method>BASIC</auth-method>
    </login-config>

Spring boot使用内置tomcat,2.0版本以前使用如下形式

@Bean
public EmbeddedServletContainerFactory servletContainer() {
    TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {// 1
        protected void postProcessContext(Context context) {
            SecurityConstraint securityConstraint = new SecurityConstraint();
            securityConstraint.setUserConstraint("CONFIDENTIAL");
            SecurityCollection collection = new SecurityCollection();
            collection.addPattern("/*");
            collection.addMethod("HEAD");
            collection.addMethod("PUT");
            collection.addMethod("DELETE");
            collection.addMethod("OPTIONS");
            collection.addMethod("TRACE");
            collection.addMethod("COPY");
            collection.addMethod("SEARCH");
            collection.addMethod("PROPFIND");
            securityConstraint.addCollection(collection);
            context.addConstraint(securityConstraint);
        }
    };

2.0版本使用以下形式

@Bean
public ConfigurableServletWebServerFactory configurableServletWebServerFactory() {
    TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
    factory.addContextCustomizers(context -> {
        SecurityConstraint securityConstraint = new SecurityConstraint();
        securityConstraint.setUserConstraint("CONFIDENTIAL");
        SecurityCollection collection = new SecurityCollection();
        collection.addPattern("/*");
        collection.addMethod("HEAD");
        collection.addMethod("PUT");
        collection.addMethod("DELETE");
        collection.addMethod("OPTIONS");
        collection.addMethod("TRACE");
        collection.addMethod("COPY");
        collection.addMethod("SEARCH");
        collection.addMethod("PROPFIND");
        securityConstraint.addCollection(collection);
        context.addConstraint(securityConstraint);
    });
    return factory;
}

关于内嵌tomcat的更多配置,感兴趣可以阅读官方文档

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • SpringBoot2.x版本中,使用SpringSession踩的坑及解决

    SpringBoot2.x SpringSession踩坑 Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.session.Ses

  • SpringBoot2.x 整合Spring-Session实现Session共享功能

    1.前言 发展至今,已经很少还存在单服务的应用架构,不说都使用分布式架构部署, 至少也是多点高可用服务.在多个服务器的情况下,Seession共享就是必须面对的问题了. 解决Session共享问题,大多数人的思路都是比较清晰的, 将需要共享的数据存在某个公共的服务中,如缓存.很多人都采用的Redis,手动将Session存在Redis,需要使用时,再从Redsi中读取数据.毫无疑问,这种方案是可行的,只是在手动操作的工作量确实不少. LZ在这里采用的Spring-Session来实现.它使用代理

  • 详解SpringBoot2 使用Spring Session集群

    有几种办法: 1.扩展指定server 利用Servlet容器提供的插件功能,自定义HttpSession的创建和管理策略,并通过配置的方式替换掉默认的策略.缺点:耦合Tomcat/Jetty等Servlet容器,不能随意更换容器. 2.利用Filter 利用HttpServletRequestWrapper,实现自己的 getSession()方法,接管创建和管理Session数据的工作.spring-session就是通过这样的思路实现的. 3 利用spring session Spring

  • SpringBoot去除内嵌tomcat的实现

    SpringBoot内嵌tomcat,直接run Application即可,那么我们如何去除内嵌的tomcat,使用自己的呢? 一.POM(去除内嵌tomcat后,需要添加servlet依赖) <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!-- 去除内嵌tomcat --

  • Springboot 使用内置tomcat禁止不安全HTTP的方法

    Springboot 内置tomcat禁止不安全HTTP方法 1.在tomcat的web.xml中可以配置如下内容 让tomcat禁止不安全的HTTP方法 <security-constraint> <web-resource-collection> <url-pattern>/*</url-pattern> <http-method>PUT</http-method> <http-method>DELETE</ht

  • springboot2如何禁用自带tomcat的session功能

    目录 禁用自带tomcat的session功能 禁用内置Tomcat的不安全请求方法 禁用自带tomcat的session功能 微服务下的各个服务都是无状态的,所以这个时候tomcat的session管理功能是多余的,即时不用,也会消耗性能,关闭后tomcat的性能会有提升,但是springboot提供的tomcat没有配置选项可以直接关闭,研究了一下,tomcat默认的session管理器名字叫:StandardManager,查看tomcat加载源码发现,如果context中没有Manage

  • Tomcat实现session共享(session 会话复制)

    一.如何保持session会话 目前,为了使web能适应大规模的访问,需要实现应用的集群部署.集群最有效的方案就是负载均衡,而实现负载均衡用户每一个请求都有可能被分配到不固定的服务器上,这样我们首先要解决session的统一来保证无论用户的请求被转发到哪个服务器上都能保证用户的正常使用,即需要实现session的共享机制. 在集群系统下实现session统一的有如下几种方案: 1.请求精确定位:session sticky,例如基于访问ip的hash策略,即当前用户的请求都集中定位到一台服务器中

  • Java中tomcat memecached session 共享同步问题的解决办法

    事件缘由:一个主项目"图说美物",另外一个子功能是品牌商的入驻功能,是跟主项目分开的项目,为了共享登录的用户信息,而实现session共享,俩个tomcat,一个tomcat6,一个tomcat7 web项目windows系统下实现session的共享 第一个步: 在俩个tomcat的context.xml这个文件中配置如下代码: <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManage

  • Tomcat中session的管理机制

    详细描述Tomcat中session的管理机制: 1. 请求过程中的session操作: 简述:在请求过程中首先要解析请求中的sessionId信息,然后将sessionId存储到request的参数列表中.然后再从 request获取session的时候,如果存在sessionId那么就根据Id从session池中获取session,如果sessionId不 存在或者session失效,那么则新建session并且将session信息放入session池,供下次使用. (1)SessionId

  • Nginx+Tomcat关于Session的管理的实现

    前言 Nginx+Tomcat对Session的管理一直有了解,但是一直没有实际操作一遍,本文从最简单的安装启动开始,通过实例的方式循序渐进的介绍了几种管理session的方式. nginx安装配置 1.安装nginx [root@localhost ~]# yum install nginx 提示报如下错误: No package nginx available. 解决办法安装epel:EPEL是企业版 Linux 附加软件包的简称,EPEL是一个由Fedora特别兴趣小组创建.维护并管理的,

  • 详解springboot项目带Tomcat和不带Tomcat的两种打包方式

    1,带Tomcat的打包方式 1.1, 在pom.xml文件添加以下配置(目的:自定main入口和跳过Junit代码) <build> <plugins> <!--打包为jar时指定main入口--> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <

  • 微信小程序实现Session功能及无法获取session问题的解决方法

    因为小程序原生不支持Cookie,因此也不支持Session. 网上找到的的一些方法有缺陷,而且很多累赘,估计没有实际测试过,在此直接给出实测可用的代码. 大概思路就是借助小程序本地储存+网络请求的header可读可写来实现类似浏览器的cookies保存session功能. 直接上代码 function NetRequest({url, data, success, fail, complete, method = "POST", header = { 'Content-type':

  • PHP封装的数据库保存session功能类

    本文实例讲述了PHP封装的数据库保存session功能类.分享给大家供大家参考,具体如下: PHP用数据库保存session类: <?php class SafeSessionHandler implements SessionHandlerInterface { public $save_path; public $session_name; public $table; public function __construct() { $this->table = new Table(&qu

  • php4的session功能评述(三)

    总的感觉php的session功能基本上还能用一用,还算方便,省了很多自己处理  cookie的麻烦事情.php的session管理还是基于文件的,默认的是在/tmp目录  下生成一个和session id同名的session文件,在这个文件里保存register的  session data.session有生存期,在/usr/local/lib/php.ini里可以设置. 效率应该算马马虎虎了,毕竟还是要文件操作,虽然是/tmp的.对于服务量很  大的应用会在/tmp里生成很多session

  • 微信小程序实现带参数的分享功能(两种方法)

    微信小程序分享功能的实现方法有两种: 第一种 在page.js中实现onShareAppMessage,便可在小程序右上角选择分享该页面 onShareAppMessage: function () { return { title: '弹出分享时显示的分享标题', desc: '分享页面的内容', path: '/page/user?id=123' // 路径,传递参数到指定页面. } } 第二种 自定义按钮实现分享,在page中添加一个带有open-type='share'的button标签

随机推荐