首页 > 解决方案 > SecurityContextHolder.getContext().getAuthentication() 在服务层返回 Null

问题描述

在服务层,我使用了 getAuthentication()但返回 null。但在控制器中,它运行良好。

请帮我。

更新:

我有security.xml:

<beans>
    <security:http realm="Protected API"
                   use-expressions="true"
                   auto-config="false"
                   create-session="stateless"
                   entry-point-ref="JvCustomAuthenticationEntryPoint">
        <security:custom-filter ref="authenticationTokenProcessingFilter"
                                position="FORM_LOGIN_FILTER"/>
        <security:intercept-url pattern="/**"
                                access="isAuthenticated()"/>

        <security:csrf disabled="true"/>
    </security:http>

    <bean id="customAuthenticationProvider" class="vn.jupviec.api.cleanerapp.auth.JvCustomAuthenticationProviderImpl"/>

    <security:authentication-manager alias="authenticationManager">
        <security:authentication-provider
                ref="customAuthenticationProvider">
        </security:authentication-provider>
    </security:authentication-manager>


    <bean id="JvCustomAuthenticationEntryPoint"
          class="vn.jupviec.api.cleanerapp.auth.JvCustomAuthenticationEntryPoint"/>

    <bean id="authenticationTokenProcessingFilter"
          class="vn.jupviec.api.cleanerapp.auth.AuthenticationTokenProcessingFilter">
        <constructor-arg ref="authenticationManager"/>
    </bean>


</beans>

布拉

标签: spring-bootspring-security

解决方案


推荐阅读