Od pewnego czasu borykam się z tym problemem. Znaleziono kilka postów na ten temat, ale żaden nie rozwiązał mojego problemu. Będzie to prawdopodobnie ma coś wspólnego z faktem, że SecurityContext jest Boud do konkretnego wątku, ale nawet wtedy nie wiem jak go rozwiązać:Wiosenne zabezpieczenia - SecurityContext.authentication null w taglib i jsp ale ok w kontrolerze
Rozważmy następujący kod do pobrania użytkownika, który był zalogowany:
SecurityContextHolder.getContext().getAuthentication().getPrincipal()
Uruchomienie tego kodu w kontrolerze spowoduje (poprawnie) zalogowanie użytkownika. Uruchamianie tego kodu z taglib lub jsp zgłasza NPE (authentication = null). Również znacznik sprężyny nie działa (prawdopodobnie z tego samego powodu).
Wyciąg z web.xml:
<filter>
<filter-name>AcegiFilter</filter-name>
<filter-class>org.acegisecurity.util.FilterToBeanProxy</filter-class>
<init-param>
<param-name>targetClass</param-name>
<param-value>org.acegisecurity.util.FilterChainProxy</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>AcegiFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Wyciąg z wiosennej konfiguracji zabezpieczeń pliku:
<bean id="filterChainProxy" class="org.springframework.security.util.FilterChainProxy">
<property name="filterInvocationDefinitionSource">
<value>
CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
PATTERN_TYPE_APACHE_ANT
/**=httpSessionIntegrationFilter,authenticationProcessingFilter,exceptionTranslationFilter,filterSecurityInterceptor
</value>
</property>
</bean>
<bean id="filterSecurityInterceptor"
class="org.springframework.security.intercept.web.FilterSecurityInterceptor">
<property name="authenticationManager" ref="authenticationManager" />
<property name="accessDecisionManager" ref="accessDecisionManager" />
<property name="alwaysReauthenticate" value="true" />
<property name="objectDefinitionSource">
<value>
CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
PATTERN_TYPE_APACHE_ANT
/myaccount.htm=ROLE_CUSTOMER
</value>
</property>
</bean>
Czy widzimy przykładową stronę JSP, która generuje ten stan? Czy importujesz taglib Spring Security w nagłówku strony? – Gandalf