亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

java - Spring Session, Spring Security 如何在無(wú)權(quán)限攔截的url不自動(dòng)創(chuàng)建session?

瀏覽:49日期:2023-10-24 09:08:00

問(wèn)題描述

我做了一個(gè)API服務(wù)器提供給手機(jī)端調(diào)用,用Spring Session連接Redis來(lái)做多臺(tái)tomcat的session共享,用security來(lái)做API的權(quán)限攔截,并且使用了x-auth-token也就是header的token驗(yàn)證。現(xiàn)在遇到一個(gè)問(wèn)題,有一些API是無(wú)權(quán)限驗(yàn)證的,但訪問(wèn)這些API時(shí),spring會(huì)為每次request都創(chuàng)建session,返回一個(gè)新的x-auth-token,這樣可能會(huì)導(dǎo)致session過(guò)多,請(qǐng)問(wèn)如何配置才能讓這種情況無(wú)需創(chuàng)建session呢?已經(jīng)配置create-session='never',但不管用。以下是security配置

<http realm='Protected API' use-expressions='true' auto-config='false'create-session='never' entry-point-ref='customAuthenticationEntryPoint'><intercept-url pattern='/auth/login/phone' access='permitAll()' /><intercept-url pattern='/**' access='isAuthenticated()' /><access-denied-handler ref='customAccessDeniedHandler' /> </http>

spring session

<!-- 在HTTP的header中使用x-auth-token:來(lái)實(shí)現(xiàn)session --> <bean /><!-- This is essential to make sure that the Spring Security session registryis notified when the session is destroyed. --> <bean /> <bean scope='singleton'><!-- session為60分鐘過(guò)期 --><property name='maxInactiveIntervalInSeconds' value='${session.maxInactiveIntervalInSeconds}'></property> </bean>...省略redis pool配置

問(wèn)題解答

回答1:

找到原因了,首先打開(kāi)log的trace,然后trace org.springframework,這個(gè)時(shí)候可以看到每次創(chuàng)建新session時(shí)都會(huì)有日志,spring會(huì)打印session的創(chuàng)建棧

java.lang.RuntimeException: For debugging purposes only (not an error) at org.springframework.session.web.http.SessionRepositoryFilter$SessionRepositoryRequestWrapper.getSession(SessionRepositoryFilter.java:368) at org.springframework.session.web.http.SessionRepositoryFilter$SessionRepositoryRequestWrapper.getSession(SessionRepositoryFilter.java:390) at org.springframework.session.web.http.SessionRepositoryFilter$SessionRepositoryRequestWrapper.getSession(SessionRepositoryFilter.java:217) at javax.servlet.http.HttpServletRequestWrapper.getSession(HttpServletRequestWrapper.java:238) at xxx.xxxxxxxx.LogFilter.doFilterInternal(LogFilter.java:52) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:208) at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:177) at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.springframework.session.web.http.SessionRepositoryFilter.doFilterInternal(SessionRepositoryFilter.java:167) at org.springframework.session.web.http.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:80) at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745)

其中可以找到xxx.xxxx這行,LogFilter第52行查看代碼發(fā)現(xiàn)調(diào)用了req.getSession(),雖然create-session配置了never,但若有代碼調(diào)用req.getSession(),spring仍然會(huì)創(chuàng)建一個(gè)全新的session。盡量不要在filter等全局?jǐn)r截器里調(diào)用req.getSession(),否則會(huì)隨時(shí)創(chuàng)建一個(gè)新的session

標(biāo)簽: java
主站蜘蛛池模板: 国产精品密蕾丝视频 | 国产大片好看免费播放 | 国产福利毛片 | 久久久网 | 久久亚洲天堂 | 日本免费在线一区 | 岛国一级毛片 | 国产视频一区二区在线播放 | 久久香蕉精品视频 | 免费网站在线观看国产v片 免费网站成人亚洲 | 亚洲欧美成人一区二区在线电影 | 欧亚色视频 | 欧美大片aaa| 国产精品久久久久不卡绿巨人 | 日韩一级欧美一级毛片在 | 国产自精品在线 | 女人被狂躁的免费视频网站软件 | 久久91精品国产99久久yfo | 又粗又大又爽 真人一级毛片 | 黄色一级影视 | 久久国产一区二区三区 | 国产呦系列 | 在线观看免费黄色小视频 | 久久久一区二区三区不卡 | 香蕉97超级碰碰碰碰碰久 | 在线免费一级片 | 午夜国产大片免费观看 | 国产免费片 | 97视频总站 | 高清影院|精品秒播3 | 91精品国产综合久 | 成年性网站| 婷婷四房色播 | 国产免费一级视频 | 91亚洲精品久久 | 午夜国产精品不卡在线观看 | 一级毛片一级毛片一级级毛片 | 久久草在线视频 | 亚洲成人一级片 | 免费的色视频 | 啪啪精品|