亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時(shí)間:2021年11月09日 14:47:31   作者:song.xl  
這篇文章主要介紹了springboot2如何禁用自帶tomcat的session功能,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

禁用自帶tomcat的session功能

微服務(wù)下的各個(gè)服務(wù)都是無狀態(tài)的,所以這個(gè)時(shí)候tomcat的session管理功能是多余的,即時(shí)不用,也會(huì)消耗性能,關(guān)閉后tomcat的性能會(huì)有提升,但是springboot提供的tomcat沒有配置選項(xiàng)可以直接關(guān)閉,研究了一下,tomcat默認(rèn)的session管理器名字叫:StandardManager,查看tomcat加載源碼發(fā)現(xiàn),如果context中沒有Manager的時(shí)候,直接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()獲取到對象,所以就可以從這里入手解決,我的解決辦法如下:自定義一個(gè)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 已經(jīng)關(guān)閉,若開啟請配置: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() {}
}

兩個(gè)類解決問題,這樣通過request獲取session就是空了,tomcat擺脫session這層處理性能有所提升。

禁用內(nèi)置Tomcat的不安全請求方法

起因:安全組針對接口測試提出的要求,需要關(guān)閉不安全的請求方法,例如put、delete等方法,防止服務(wù)端資源被惡意篡改。

用過springMvc都知道可以使用@PostMapping、@GetMapping等這種注解限定單個(gè)接口方法類型,或者是在@RequestMapping中指定method屬性。這種方式比較麻煩,那么有沒有比較通用的方法,通過查閱相關(guān)資料,答案是肯定的。

tomcat傳統(tǒng)形式通過配置web.xml達(dá)到禁止不安全的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使用內(nèi)置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;
}

關(guān)于內(nèi)嵌tomcat的更多配置,感興趣可以閱讀官方文檔。

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解Java中native方法的使用

    詳解Java中native方法的使用

    native是與C++聯(lián)合開發(fā)的時(shí)候用的!使用native關(guān)鍵字說明這個(gè)方法是原生函數(shù),也就是這個(gè)方法是用C/C++語言實(shí)現(xiàn)的,并且被編譯成了DLL,由java去調(diào)用。本文給大家介紹java 中native方法使用,感興趣的朋友一起看看吧
    2020-09-09
  • 詳解Mybatis中的CRUD

    詳解Mybatis中的CRUD

    這篇文章主要介紹了Mybatis中的CRUD的相關(guān)知識,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-03-03
  • idea中l(wèi)ombok的用法

    idea中l(wèi)ombok的用法

    lombok是開源的代碼生成庫,是一款非常實(shí)用的小工具,在更改實(shí)體類時(shí)只需要修改屬性即可,減少了很多重復(fù)代碼的編寫工作,今天小編給大家介紹idea中l(wèi)ombok的用法,感興趣的朋友一起看看吧
    2021-12-12
  • springboot 使用ThreadLocal的實(shí)例代碼

    springboot 使用ThreadLocal的實(shí)例代碼

    這篇文章主要介紹了springboot 使用ThreadLocal的實(shí)例代碼,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Spring后處理器詳細(xì)介紹

    Spring后處理器詳細(xì)介紹

    Bean后置處理器允許在調(diào)用初始化方法前后對Bean進(jìn)行額外的處理。可以在?Spring容器通過插入一個(gè)或多個(gè)BeanPostProcessor的實(shí)現(xiàn)來完成實(shí)例化,配置和初始化一個(gè)?bean?之后實(shí)現(xiàn)一些自定義邏輯回調(diào)方法
    2023-02-02
  • SSH框架網(wǎng)上商城項(xiàng)目第8戰(zhàn)之查詢和刪除商品類別功能實(shí)現(xiàn)

    SSH框架網(wǎng)上商城項(xiàng)目第8戰(zhàn)之查詢和刪除商品類別功能實(shí)現(xiàn)

    SSH框架網(wǎng)上商城項(xiàng)目第8戰(zhàn)之查詢和刪除商品類別功能實(shí)現(xiàn),為項(xiàng)目增加功能,添加、更新、刪除和查詢操作,感興趣的小伙伴們可以參考一下
    2016-05-05
  • 修改Android應(yīng)用的樣式的一些關(guān)鍵點(diǎn)解析

    修改Android應(yīng)用的樣式的一些關(guān)鍵點(diǎn)解析

    這篇文章主要介紹了修改Android應(yīng)用的樣式的一些關(guān)鍵點(diǎn),即對影響外觀的theme跟style的相關(guān)修改,需要的朋友可以參考下
    2015-12-12
  • eclipse創(chuàng)建一個(gè)基于maven的web項(xiàng)目詳細(xì)步驟

    eclipse創(chuàng)建一個(gè)基于maven的web項(xiàng)目詳細(xì)步驟

    開始學(xué)習(xí)maven,并用maven創(chuàng)建了第一個(gè)屬于自己的web項(xiàng)目,下面這篇文章主要給大家介紹了關(guān)于eclipse創(chuàng)建一個(gè)基于maven的web項(xiàng)目的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2023-12-12
  • Java內(nèi)存模型的深入講解

    Java內(nèi)存模型的深入講解

    這篇文章主要給大家介紹了關(guān)于Java內(nèi)存模型的相關(guān)資料,我們常說的JVM內(nèi)存模式指的是JVM的內(nèi)存分區(qū),而Java內(nèi)存模式是一種虛擬機(jī)規(guī)范,需要的朋友可以參考下
    2021-07-07
  • SpringBoot項(xiàng)目使用slf4j的MDC日志打點(diǎn)功能(最新推薦)

    SpringBoot項(xiàng)目使用slf4j的MDC日志打點(diǎn)功能(最新推薦)

    這篇文章主要介紹了SpringBoot項(xiàng)目使用slf4j的MDC日志打點(diǎn)功能,本文通過示例代碼給大家介紹非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-06-06

最新評論