色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

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

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

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

問題描述

我做了一個API服務(wù)器提供給手機端調(diào)用,用Spring Session連接Redis來做多臺tomcat的session共享,用security來做API的權(quán)限攔截,并且使用了x-auth-token也就是header的token驗證。現(xiàn)在遇到一個問題,有一些API是無權(quán)限驗證的,但訪問這些API時,spring會為每次request都創(chuàng)建session,返回一個新的x-auth-token,這樣可能會導(dǎo)致session過多,請問如何配置才能讓這種情況無需創(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:來實現(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分鐘過期 --><property name='maxInactiveIntervalInSeconds' value='${session.maxInactiveIntervalInSeconds}'></property> </bean>...省略redis pool配置

問題解答

回答1:

找到原因了,首先打開log的trace,然后trace org.springframework,這個時候可以看到每次創(chuàng)建新session時都會有日志,spring會打印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仍然會創(chuàng)建一個全新的session。盡量不要在filter等全局攔截器里調(diào)用req.getSession(),否則會隨時創(chuàng)建一個新的session

標簽: java
主站蜘蛛池模板: 台湾黄三级高清在线观看播放 | 成人亚洲欧美综合 | 国产高清成人mv在线观看 | 亚洲美女视频网址 | 一级性片| 日本一在线中文字幕天堂 | 国产孕妇孕交视频在线观看 | 国产乱码精品一区二区三区卡 | 午夜神马视频 | 日本高清不卡在线观看 | 国产永久免费高清动作片www | 成人免费一区二区三区视频软件 | 成人国产三级精品 | a毛片在线播放 | 日韩视频在线观看一区二区 | 国产一级免费视频 | 午夜性福 | 91刘亦菲精品福利在线 | 国产妇乱子伦视频免费 | 亚洲欧美日韩在线不卡中文 | 国产一级毛片亚洲久留木玲 | 成人久久18免费软件 | 一a一片一级一片啪啪 | 美女免费黄视频 | 日韩一及片 | 久久99国产乱子伦精品免费 | 国产一区二区三区精品久久呦 | 永久免费毛片在线播放 | 欧美一区二区三区久久综合 | 毛片网站在线播放 | 日本高清一本二本三本如色坊 | 国产日韩精品一区二区三区 | 欧美高清正版在线 | 18性欧美69 | 曰本黄页| 久久精品国产午夜伦班片 | 日本免费一区二区三区毛片 | 欧美69视频| 国产精品路边足疗店按摩 | 在线视频亚洲欧美 | 国产99久久精品 |