Spring Security 報(bào)錯(cuò): No ContextLoaderListener registered

今天我在配置 Spring Security 的OAuth是遇到了一個(gè)錯(cuò)誤:

java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?

需要注意一下幾點(diǎn):

1. 添加 spring-context 依賴

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${org.springframework.version}</version>
</dependency>

2. 注冊(cè) ContextLoaderListener

修改web.xml如下

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

注意,如果使用了Log4jConfigListener,需要把ContextLoaderListener放在后面。

3. 注意Spring Servlet 名稱

參考OAuth 2 Developers Guide知道,我們需要在web.xml的securityFilterChain中添加如下配置:

    <init-param>
        <param-name>contextAttribute</param-name>
        <param-value>org.springframework.web.servlet.FrameworkServlet.CONTEXT.spring</param-value>
    </init-param>

特別需要注意的是:參數(shù)值最后的spring不是亂蓋的,它要和SpringDispatchServlet的名稱一致。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容