HttpSession 为 SPRING_SECURITY_CONTEXT 返回 null 对象

2023-11-27

我正在尝试整合Spring Saml 库在示例 Web 应用程序中,使用 Shibboleth 作为 IDP。 我能够加载登录页面、登录并显示索引页面。

问题是,当我单击其他链接时,Web 应用程序会将我重定向到登录页面,然后 IDP 会识别我并重定向到所请求的页面(如果网络速度很快,则很难看到这一点)。就好像我没有登录 Spring 安全性。

我检查了日志,发现了这一点:

org.springframework.security.web.context.HttpSessionSecurityContextRepository - HttpSession为SPRING_SECURITY_CONTEXT返回空对象 org.springframework.security.web.context.HttpSessionSecurityContextRepository - HttpSession 中没有可用的 SecurityContext:org.apache.catalina.session.StandardSessionFacade@fde8fb。将创建一个新的。

这是 web.xml

<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/spring/root-context.xml
        /WEB-INF/spring/security/securityContext.xml
    </param-value>
</context-param>
<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Processes application requests -->
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>



<!-- Custom error pages -->
<error-page>
    <error-code>400</error-code>
    <location>/errors/missing-en.html</location>
</error-page>
<error-page>
    <error-code>401</error-code>
    <location>/errors/restricted-en.html</location>
</error-page>
<error-page>
    <error-code>403</error-code>
    <location>/errors/restricted-en.html</location>
</error-page>
<error-page>
    <error-code>404</error-code>
    <location>/errors/missing-en.html</location>
</error-page>
<error-page>
    <error-code>500</error-code>
    <location>/errors/missing-en.html</location>
</error-page>
<error-page>
    <error-code>503</error-code>
    <location>/errors/missing-en.html</location>
</error-page>

和安全上下文

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:security="http://www.springframework.org/schema/security" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/security 
        http://www.springframework.org/schema/security/spring-security-3.1.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">

<!-- Enable autowiring -->
<context:annotation-config />
<context:component-scan base-package="org.springframework.security.saml" />

<security:http pattern="/logout.jsp" security="none" />
<security:http pattern="/login.jsp" security="none" />
<security:http pattern="/index.html" security="none" />

<security:http entry-point-ref="samlEntryPoint">
    <security:intercept-url pattern="/**" access="IS_AUTHENTICATED_FULLY" />
    <security:custom-filter before="FIRST" ref="metadataGeneratorFilter" />
    <security:custom-filter after="BASIC_AUTH_FILTER" ref="samlFilter" />
</security:http>

<bean id="samlFilter" class="org.springframework.security.web.FilterChainProxy">
    <security:filter-chain-map path-type="ant">
        <security:filter-chain pattern="/saml/login/**" filters="samlEntryPoint" />
        <security:filter-chain pattern="/saml/logout/**" filters="samlLogoutFilter" />
        <security:filter-chain pattern="/saml/SSO/**" filters="samlWebSSOProcessingFilter" />
        <security:filter-chain pattern="/saml/SSOHoK/**" filters="samlWebSSOHoKProcessingFilter" />
        <security:filter-chain pattern="/saml/SingleLogout/**" filters="samlLogoutProcessingFilter" />
    </security:filter-chain-map>
</bean>

<!-- Handler deciding where to redirect user after successful login -->
<bean id="successRedirectHandler" class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler">
    <property name="defaultTargetUrl" value="/" />
</bean>
<!-- Use the following for interpreting RelayState coming from unsolicited response as redirect URL: <bean id="successRedirectHandler" class="org.springframework.security.saml.SAMLRelayStateSuccessHandler"> 
    <property name="defaultTargetUrl" value="/" /> </bean> -->

<!-- Handler for successful logout -->
<bean id="successLogoutHandler" class="org.springframework.security.web.authentication.logout.SimpleUrlLogoutSuccessHandler">
    <property name="defaultTargetUrl" value="/logout.jsp" />
</bean>

<!-- Register authentication manager with SAML provider -->
<security:authentication-manager alias="authenticationManager">
    <security:authentication-provider ref="samlAuthenticationProvider" />
</security:authentication-manager>

<!-- Logger for SAML messages and events -->
<bean id="samlLogger" class="org.springframework.security.saml.log.SAMLDefaultLogger" />

<!-- Central storage of cryptographic keys -->
<bean id="keyManager" class="org.springframework.security.saml.key.JKSKeyManager">
    <constructor-arg value="/WEB-INF/spring/security/myKeystore.jks" />
    <constructor-arg type="java.lang.String" value="betfair" />
    <constructor-arg>
        <map>
            <entry key="tomcat" value="betfair" />
        </map>
    </constructor-arg>
    <constructor-arg type="java.lang.String" value="tomcat" />
</bean>

<!-- Entry point to initialize authentication, default values taken from properties file -->
<bean id="samlEntryPoint" class="org.springframework.security.saml.SAMLEntryPoint">
    <property name="defaultProfileOptions">
        <bean class="org.springframework.security.saml.websso.WebSSOProfileOptions">
            <property name="includeScoping" value="false" />
        </bean>
    </property>
</bean>

<!-- IDP Discovery Service -->
<bean id="samlIDPDiscovery" class="org.springframework.security.saml.SAMLDiscovery">
    <property name="idpSelectionPath" value="/WEB-INF/security/idpSelection.jsp" />
</bean>

<!-- Filter automatically generates default SP metadata -->
<bean id="metadataGeneratorFilter" class="org.springframework.security.saml.metadata.MetadataGeneratorFilter">
    <constructor-arg>
        <bean class="org.springframework.security.saml.metadata.MetadataGenerator" />
    </constructor-arg>
</bean>

<!-- The filter is waiting for connections on URL suffixed with filterSuffix and presents SP metadata there -->
<bean id="metadataDisplayFilter" class="org.springframework.security.saml.metadata.MetadataDisplayFilter" />

<!-- IDP Metadata configuration - paths to metadata of IDPs in circle of trust is here -->
<!-- Do no forget to call iniitalize method on providers -->
<bean id="metadata" class="org.springframework.security.saml.metadata.CachingMetadataManager">
    <constructor-arg>
        <list>

            <bean class="org.springframework.security.saml.metadata.ExtendedMetadataDelegate">
                <constructor-arg>
                    <bean class="org.opensaml.saml2.metadata.provider.FilesystemMetadataProvider">
                        <constructor-arg>
                            <value type="java.io.File">/WEB-INF/spring/security/shibboleth.xml</value>
                        </constructor-arg>
                        <property name="parserPool" ref="parserPool" />
                    </bean>
                </constructor-arg>
                <constructor-arg>
                    <bean class="org.springframework.security.saml.metadata.ExtendedMetadata">
                    </bean>
                </constructor-arg>
            </bean>
            <bean class="org.springframework.security.saml.metadata.ExtendedMetadataDelegate">
                <constructor-arg>
                    <bean class="org.opensaml.saml2.metadata.provider.FilesystemMetadataProvider">
                        <constructor-arg>
                            <value type="java.io.File">/WEB-INF/spring/security/localhost_sp.xml</value>
                        </constructor-arg>
                        <property name="parserPool" ref="parserPool" />
                    </bean>
                </constructor-arg>
                <constructor-arg>
                    <bean class="org.springframework.security.saml.metadata.ExtendedMetadata">
                        <property name="local" value="true" />
                        <property name="alias" value="localhost" />
                        <property name="securityProfile" value="metaiop" />
                        <property name="sslSecurityProfile" value="pkix" />
                        <property name="signingKey" value="tomcat" />
                        <property name="encryptionKey" value="tomcat" />
                        <property name="tlsKey" value="tomcat" />
                        <property name="requireArtifactResolveSigned" value="false" />
                        <property name="requireLogoutRequestSigned" value="false" />
                        <property name="requireLogoutResponseSigned" value="false" />
                    </bean>
                </constructor-arg>
            </bean>
        </list>
    </constructor-arg>      
    <!-- OPTIONAL used when one of the metadata files contains information about this service provider -->
    <property name="hostedSPName" value="localhost"/>
    <!-- OPTIONAL property: can tell the system which IDP should be used for authenticating user by default. -->
    <!-- <property name="defaultIDP" value="http://localhost:8080/opensso"/> -->
</bean>

<!-- SAML Authentication Provider responsible for validating of received SAML messages -->
<bean id="samlAuthenticationProvider" class="org.springframework.security.saml.SAMLAuthenticationProvider">
    <!-- OPTIONAL property: can be used to store/load user data after login -->
    <!-- <property name="userDetails" ref="bean" /> -->
</bean>

<!-- Provider of default SAML Context -->
<bean id="contextProvider" class="org.springframework.security.saml.context.SAMLContextProviderImpl" />

<!-- Processing filter for WebSSO profile messages -->
<bean id="samlWebSSOProcessingFilter" class="org.springframework.security.saml.SAMLProcessingFilter">
    <property name="authenticationManager" ref="authenticationManager" />
    <property name="authenticationSuccessHandler" ref="successRedirectHandler" />
</bean>

<!-- Processing filter for WebSSO Holder-of-Key profile -->
<bean id="samlWebSSOHoKProcessingFilter" class="org.springframework.security.saml.SAMLWebSSOHoKProcessingFilter">
    <property name="authenticationManager" ref="authenticationManager" />
    <property name="authenticationSuccessHandler" ref="successRedirectHandler" />
</bean>

<!-- Logout handler terminating local session -->
<bean id="logoutHandler" class="org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler">
    <property name="invalidateHttpSession" value="false" />
</bean>

<!-- Override default logout processing filter with the one processing SAML messages -->
<bean id="samlLogoutFilter" class="org.springframework.security.saml.SAMLLogoutFilter">
    <constructor-arg ref="successLogoutHandler" />
    <constructor-arg ref="logoutHandler" />
    <constructor-arg ref="logoutHandler" />
</bean>

<!-- Filter processing incoming logout messages -->
<!-- First argument determines URL user will be redirected to after successful global logout -->
<bean id="samlLogoutProcessingFilter" class="org.springframework.security.saml.SAMLLogoutProcessingFilter">
    <constructor-arg ref="successLogoutHandler" />
    <constructor-arg ref="logoutHandler" />
</bean>

<!-- Class loading incoming SAML messages from httpRequest stream -->
<bean id="processor" class="org.springframework.security.saml.processor.SAMLProcessorImpl">
    <constructor-arg>
        <list>
            <ref bean="redirectBinding" />
            <ref bean="postBinding" />
            <ref bean="artifactBinding" />
            <ref bean="soapBinding" />
            <ref bean="paosBinding" />
        </list>
    </constructor-arg>
</bean>

<!-- SAML 2.0 WebSSO Assertion Consumer -->
<bean id="webSSOprofileConsumer" class="org.springframework.security.saml.websso.WebSSOProfileConsumerImpl" />

<!-- SAML 2.0 Holder-of-Key WebSSO Assertion Consumer -->
<bean id="hokWebSSOprofileConsumer" class="org.springframework.security.saml.websso.WebSSOProfileConsumerHoKImpl" />

<!-- SAML 2.0 Web SSO profile -->
<bean id="webSSOprofile" class="org.springframework.security.saml.websso.WebSSOProfileImpl" />

<!-- SAML 2.0 Holder-of-Key Web SSO profile -->
<bean id="hokWebSSOProfile" class="org.springframework.security.saml.websso.WebSSOProfileConsumerHoKImpl" />

<!-- SAML 2.0 ECP profile -->
<bean id="ecpprofile" class="org.springframework.security.saml.websso.WebSSOProfileECPImpl" />

<!-- SAML 2.0 Logout Profile -->
<bean id="logoutprofile" class="org.springframework.security.saml.websso.SingleLogoutProfileImpl" />

<!-- Bindings, encoders and decoders used for creating and parsing messages -->
<bean id="postBinding" class="org.springframework.security.saml.processor.HTTPPostBinding">
    <constructor-arg ref="parserPool" />
    <constructor-arg ref="velocityEngine" />
</bean>

<bean id="redirectBinding" class="org.springframework.security.saml.processor.HTTPRedirectDeflateBinding">
    <constructor-arg ref="parserPool" />
</bean>

<bean id="artifactBinding" class="org.springframework.security.saml.processor.HTTPArtifactBinding">
    <constructor-arg ref="parserPool" />
    <constructor-arg ref="velocityEngine" />
    <constructor-arg>
        <bean class="org.springframework.security.saml.websso.ArtifactResolutionProfileImpl">
            <constructor-arg>
                <bean class="org.apache.commons.httpclient.HttpClient" />
            </constructor-arg>
            <property name="processor">
                <bean id="soapProcessor" class="org.springframework.security.saml.processor.SAMLProcessorImpl">
                    <constructor-arg ref="soapBinding" />
                </bean>
            </property>
        </bean>
    </constructor-arg>
</bean>

<bean id="soapBinding" class="org.springframework.security.saml.processor.HTTPSOAP11Binding">
    <constructor-arg ref="parserPool" />
</bean>

<bean id="paosBinding" class="org.springframework.security.saml.processor.HTTPPAOS11Binding">
    <constructor-arg ref="parserPool" />
</bean>

<!-- Initialization of OpenSAML library -->
<bean class="org.springframework.security.saml.SAMLBootstrap" />

<!-- Initialization of the velocity engine -->
<bean id="velocityEngine" class="org.springframework.security.saml.util.VelocityFactory" factory-method="getEngine" />

<!-- XML parser pool needed for OpenSAML parsing -->
<bean id="parserPool" class="org.opensaml.xml.parse.BasicParserPool" scope="singleton" />

任何想法?

谢谢 埃马努埃莱


我也遇到过同样的问题。调试后发现SecurityContext在持久化之前就被清除了。谷歌搜索后我发现了这个:https://jira.springsource.org/browse/SEC-2027迁移到 Spring Security 版本 3.1.2 为我解决了这个问题。

我希望它能帮助某人。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

HttpSession 为 SPRING_SECURITY_CONTEXT 返回 null 对象 的相关文章

随机推荐

  • 如何在没有 _class 属性的情况下将 spring 数据与 couchbase 一起使用

    有没有一种简单的方法可以将 spring data couchbase 与没有的文档一起使用 class属性 在沙发底座上我有类似的东西sampledata bucket username alice created 14732928000
  • CSS 选择器和 jQuery 过滤器之间的区别?

    可以将 CSS 选择器传递给 jQuery 函数 例如 jQuery h1 h2 jQuery 还有一些过滤器 例如 even and odd jQuery tr even 我正在寻找某种区分两者的语法规则 我想也许 jQuery 过滤器总
  • SQL Server中Group By、Having和Where子句的执行顺序?

    当我们使用时 我只是对 SQL 查询的执行顺序感到困惑GROUP BY and HAVING with a WHERE条款 哪个先被处决 顺序是什么 为了 FROM JOINs 确定和过滤行 WHERE行上有更多过滤器 GROUP BY将这
  • gRPC 设置出现问题。出现间歇性 RPC 不可用错误

    我有一个 grpc 服务器和客户端 大部分时间都按预期工作 但偶尔会收到 传输正在关闭 错误 rpc error code Unavailable desc transport is closing 我想知道是不是我的设置有问题 客户端非常
  • 如何创建二维动态长度数组?

    我想创建一个二维数组 而不知道第一维的大小 例如 当我创建数组时 我的行数未知 每一行代表一个帐户 每行退出 4 列 ID 名称 用户 密码 我尝试使用锯齿状数组 但不可能 int jaggedArray new int 3 我也找过Arr
  • 在android上的列表视图下方添加一个按钮

    所以我一直在尝试在android中的列表视图下添加一个按钮 问题是该按钮没有出现
  • 为通过 Azure 函数输出绑定添加到 Azure 队列的消息设置 VisibilityTimeout

    我有一个 TimerTrigger 函数 输出绑定是一个 Azure 队列 这个想法是 计时器每 10 分钟运行一次 它将查看数据库中的视图 并迭代返回的任何行 将它们作为消息添加到队列中 下面是我的示例 TimerTrigger 将消息添
  • 在javascript中初始化和填充多维数组

    我们如何初始化和创建新的多维数组 让我们想象一下 如果我想初始化一个 4x4 多维数组并用 0 填充它 理想情况下 在二维数组中 我们会这样做 let oned array new Array 10 fill 0 将创建大小为 10 的数组
  • 如何使用 Selenium 查找表中的特定行?

    这是一个示例代码 div table class table gradient myPage 因此 productOrderContainer 中的这个表有几列 并且根据某些情况 将有几行 这些行都有几列 一个例子是 例如 我想做的是获取该
  • 在任何应用程序中使用 info.plist 隐藏状态栏

    需要澄清的是 我不想在我自己的应用程序上执行此操作 因此我无权访问源代码 我正在尝试编辑 info plist 或设备上已安装的 app 文件中的任何其他文件 以从应用程序 即 Google Chrome 中删除状态栏 我正在尝试隐藏 Go
  • RDD 在内存中保留多长时间?

    考虑到内存有限 我有一种感觉 spark会自动从每个节点中删除RDD 我想知道这个时间可以配置吗 Spark如何决定何时从内存中驱逐RDD 注意 我不是在谈论rdd cache 我想知道这个时间可以配置吗 Spark 如何决定何时 从内存中
  • 如何在 Tensorflow 中暂停/恢复训练

    这个问题是在保存和恢复文档可用之前提出的 现在我认为这个问题已被弃用 并建议人们依赖官方文档保存和恢复 老问题要点 I got TF工作正常CIFAR教程 我已经改变了 保存的代码train dir 带有检查点的目录和 模型 到已知位置 这
  • 如何使用 javascript aws-sdk 支持 dynamoDB 中的事务?

    我们有一个用 Node js 编写的微服务 并使用 dynamoDB 进行数据存储 值以 json 格式存储在键中 在更新服务调用中 我们获取键的值 更新 json 并保存它 最近 我们遇到了两个调用想要更新同一个键的值的情况 因此 第一次
  • 将文件资源注入 Spring bean

    将一些文件资源注入 Spring bean 的好方法是什么 现在我自动装配 ServletContext 并使用如下所示 在 Spring MVC 中是否有更优雅的方法 Controller public class SomeControl
  • 逐行读取文本文件的最快方法是什么?

    我想逐行读取文本文件 我想知道我是否在 NET C 范围内尽可能高效地完成工作 到目前为止 这就是我正在尝试的 var filestream new System IO FileStream textFilePath System IO F
  • 如何使用Javascript测试用户计算机的处理能力?

    我用大量 CSS3 和 Javascript 制作了一个 CPU 密集型网页 我想使用 Javascript 来测试用户的计算机是否能够处理脚本 我认为一种可能的方法是运行一些 CPU 密集型脚本 看看需要多长时间 但是 我不知道如何实际实
  • 如何在 Laravel 中创建自定义辅助函数

    我想创建辅助函数以避免在 Laravel 中的视图之间重复代码 例如 视图 blade php p Foo Formated text fooFormatText text p 它们基本上是文本格式化函数 我应该如何定义全局可用的辅助函数
  • 如何在 MATLAB 中对齐子图中的图/图形?

    我有 3 个对象 一张照片和 2 个绘图 要放入一个图形的子图中 它应该看起来像这样 但正如人们所注意到的 照片不应该是正方形 而是矩形 我尝试这样做 在这里找到Matlab 当子图之一包含颜色条时如何对齐子图的轴 main subplot
  • C# 使用 xpath 解析 html

    我正在尝试使用 HTML 文档中的一段简单的 C 代码来解析股票交易信息 问题是我无法理解语法 tr 类 LomakeTaustaVari 被解析出来 但我如何获得没有 tr 类的第二位 这是 HTML 的一部分 它使用不同的值重复自身 t
  • HttpSession 为 SPRING_SECURITY_CONTEXT 返回 null 对象

    我正在尝试整合Spring Saml 库在示例 Web 应用程序中 使用 Shibboleth 作为 IDP 我能够加载登录页面 登录并显示索引页面 问题是 当我单击其他链接时 Web 应用程序会将我重定向到登录页面 然后 IDP 会识别我