Spring security - SecurityContext.authentication null w taglib i jsp, ale ok w kontrolerze
Od jakiegoś czasu zmagam się z tym problemem. Znaleziono kilka postów na ten temat, ale żaden nie rozwiązał mojego problemu. Prawdopodobnie będzie to miało coś wspólnego z faktem, że SecurityContext jest boud do konkretnego wątku, ale nawet wtedy nie wiem, jak go rozwiązać:
Rozważ następujący kod, aby pobrać użytkownika, który był zalogowany:
SecurityContextHolder.getContext().getAuthentication().getPrincipal()
Uruchomienie tego kodu w kontrolerze spowodowałoby powrót (poprawnie) zalogowanego użytkownika. Uruchomienie tego kodu z taglib lub jsp zgłasza NPE (authentication = null). Również tag sprężynowy 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 wiosennego pliku konfiguracyjnego bezpieczeństwa:
<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>