猿问

Spring Session, Spring Security 如何在无权限拦截的url不自动创建session?

我做了一个API服务器提供给手机端调用,用Spring Session连接Redis来做多台tomcat的session共享,用security来做API的权限拦截,并且使用了x-auth-token也就是header的token验证。现在遇到一个问题,有一些API是无权限验证的,但访问这些API时,spring会为每次request都创建session,返回一个新的x-auth-token,这样可能会导致session过多,请问如何配置才能让这种情况无需创建session呢?已经配置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:來實現session -->
    <bean class="org.springframework.session.web.http.HeaderHttpSessionStrategy" />

<!-- This is essential to make sure that the Spring Security session registry
        is notified when the session is destroyed. -->
    <bean
        class="org.springframework.security.web.session.HttpSessionEventPublisher" />

    <bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration" scope="singleton">
        <!-- session为60分钟过期 -->
        <property name="maxInactiveIntervalInSeconds" value="${session.maxInactiveIntervalInSeconds}"></property>
    </bean>

...
省略redis pool配置
偶然的你
浏览 924回答 1
1回答
随时随地看视频慕课网APP

相关分类

Java
我要回答