最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

禁用自帶tomcat的session功能

微服務(wù)下的各個服務(wù)都是無狀態(tài)的,所以這個時候tomcat的session管理功能是多余的,即時不用,也會消耗性能,關(guān)閉后tomcat的性能會有提升,但是springboot提供的tomcat沒有配置選項可以直接關(guān)閉,研究了一下,tomcat默認(rèn)的session管理器名字叫:StandardManager,查看tomcat加載源碼發(fā)現(xiàn),如果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 已經(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() {}
}

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

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

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

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

tomcat傳統(tǒng)形式通過配置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使用內(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的更多配置,感興趣可以閱讀官方文檔。

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

相關(guān)文章

  • springboot集成Feign的實現(xiàn)示例

    springboot集成Feign的實現(xiàn)示例

    Feign是聲明式HTTP客戶端,用于簡化微服務(wù)之間的REST調(diào)用,本文就來介紹一下springboot集成Feign的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-11-11
  • 基于IDEA中格式化代碼的快捷鍵分享

    基于IDEA中格式化代碼的快捷鍵分享

    這篇文章主要介紹了基于IDEA中格式化代碼的快捷鍵分享,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • Java中Executor和Executors的區(qū)別小結(jié)

    Java中Executor和Executors的區(qū)別小結(jié)

    在Java并發(fā)編程中,Executor是一個核心接口,提供了任務(wù)執(zhí)行的抽象方法,而Executors是一個工具類,提供了創(chuàng)建各種線程池的工廠方法,Executor關(guān)注任務(wù)的執(zhí)行,而Executors關(guān)注如何創(chuàng)建適合的執(zhí)行器,感興趣的可以了解一下
    2024-10-10
  • selenium-java實現(xiàn)自動登錄跳轉(zhuǎn)頁面方式

    selenium-java實現(xiàn)自動登錄跳轉(zhuǎn)頁面方式

    利用Selenium和Java語言可以編寫一個腳本自動刷新網(wǎng)頁,首先,需要確保Google瀏覽器和Chrome-Driver驅(qū)動的版本一致,通過指定網(wǎng)站下載對應(yīng)版本的瀏覽器和驅(qū)動,在Maven項目中添加依賴,編寫腳本實現(xiàn)網(wǎng)頁的自動刷新,此方法適用于需要頻繁刷新網(wǎng)頁的場景,簡化了操作,提高了效率
    2024-11-11
  • 微信公眾平臺開發(fā)實戰(zhàn)Java版之微信獲取用戶基本信息

    微信公眾平臺開發(fā)實戰(zhàn)Java版之微信獲取用戶基本信息

    這篇文章主要介紹了微信公眾平臺開發(fā)實戰(zhàn)Java版之微信獲取用戶基本信息 的相關(guān)資料,需要的朋友可以參考下
    2015-12-12
  • java組件commons-fileupload文件上傳示例

    java組件commons-fileupload文件上傳示例

    這篇文章主要為大家詳細(xì)介紹了java組件commons-fileupload實現(xiàn)文件上傳,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • SpringBoot中將@Bean方法解析為BeanDefinition詳解

    SpringBoot中將@Bean方法解析為BeanDefinition詳解

    這篇文章主要介紹了SpringBoot中將@Bean方法解析為BeanDefinition詳解,得到的BeanDefinition是ConfigurationClassBeanDefinition類型,會為BeanDefinition設(shè)置factoryMethodName,這意味著當(dāng)實例化這個bean的時候?qū)⒉捎霉S方法,需要的朋友可以參考下
    2023-12-12
  • SpringBoot使用AOP記錄接口操作日志詳解

    SpringBoot使用AOP記錄接口操作日志詳解

    這篇文章主要為大家詳細(xì)介紹了SpringBoot使用AOP記錄接口操作日志,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • JAVA IO的3種類型區(qū)別解析

    JAVA IO的3種類型區(qū)別解析

    這篇文章主要介紹了JAVA IO的3種類型解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-10-10
  • Java并發(fā)編程之常用的輔助類詳解

    Java并發(fā)編程之常用的輔助類詳解

    這篇文章主要給大家介紹了關(guān)于Java并發(fā)編程之常用的輔助類的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01

最新評論

石河子市| 色达县| 呼伦贝尔市| 广汉市| 仁寿县| 普定县| 朔州市| 林州市| 钟山县| 沙河市| 海伦市| 龙泉市| 崇左市| 河源市| 大邑县| 社会| 内黄县| 永胜县| 金昌市| 博湖县| 阜宁县| 镇宁| 苏尼特左旗| 和静县| 团风县| 长沙市| 凤城市| 紫阳县| 凯里市| 瑞金市| 孝义市| 南城县| 体育| 临武县| 博客| 普安县| 山丹县| 蛟河市| 许昌市| 灵台县| 正定县|