我找到了解決方案。如果有人有興趣,我在這裏解釋一下。
我添加下面的Java代碼的doFilter方法在我的會話管理過濾器,用於檢查,如果用戶(在這種情況下,匿名用戶)被允許訪問請求的網頁:
...
private WebInvocationPrivilegeEvaluator webPrivilegeEvaluator;
...
// Before this I have checked that the session is invalid and that the invalidSessionUrl parameter isn't null
String uri = request.getRequestURI();
String cPath = request.getContextPath();
int longCPath = cPath.length();
String pagSolicitada = uri.substring(longCPath);
Authentication autenticacion = SecurityContextHolder.getContext().getAuthentication();
if (!webPrivilegeEvaluator.isAllowed(pagSolicitada, autenticacion)) {
// Redirect to the invalidSessionUrl
redirectStrategy.sendRedirect(request, response, invalidSessionUrl);
return;
}
// Do nothing, just skip this filter
chain.doFilter(request, response);
return;
...
的webPrivilegeEvaluator是我在XML配置文件中注入會話管理過濾器的屬性:
<beans:bean id="filtroGestionSesion" class="springSecurity.FiltroGestionSesion">
<beans:constructor-arg name="securityContextRepository" ref="securityContextRepository" />
<beans:property name="sessionAuthenticationStrategy" ref="sas" />
<beans:property name="invalidSessionUrl" value="/faces/paginas/autenticacion/login.xhtml?error=timeout" />
<beans:property name="webPrivilegeEvaluator" ref="webPrivilegeEvaluator" />
</beans:bean>
和bean,這個屬性引用是:
<beans:bean id="webPrivilegeEvaluator" class="org.springframework.security.web.access.DefaultWebInvocationPrivilegeEvaluator">
<beans:constructor-arg ref="filterSecurityInterceptor" />
</beans:bean>
最後,filterSecurityInterceptor與他們所需要的模式和接入截距-url元素(你不把這些攔截的URL命名空間中的http元素,只是把他們在這裏):
<beans:bean id="filterSecurityInterceptor" class="org.springframework.security.web.access.intercept.FilterSecurityInterceptor">
<beans:property name="securityMetadataSource">
<filter-security-metadata-source use-expressions="true">
<!-- IMPORTANTE: Poner las URLs más específicas primero -->
<intercept-url pattern="/" access="permitAll"/> <!-- Página inicio al arrancar la aplic (contextPath) -->
<intercept-url pattern="/faces/inicio.xhtml" access="permitAll"/>
<intercept-url pattern="/faces/paginas/autenticacion/login.xhtml*" access="permitAll"/>
<intercept-url pattern="/faces/paginas/autenticacion/**" access="isAuthenticated()"/>
<intercept-url pattern="/faces/paginas/administracion/**" access="isAuthenticated()"/>
<intercept-url pattern="/faces/paginas/barco/**" access="isAuthenticated()"/>
<intercept-url pattern="/faces/paginas/catalogo/**" access="permitAll"/>
<intercept-url pattern="/faces/paginas/error/**" access="permitAll"/>
<intercept-url pattern="/faces/paginas/plantillas/**" access="permitAll"/>
<intercept-url pattern="/**" access="denyAll" />
</filter-security-metadata-source>
</beans:property>
<beans:property name="authenticationManager" ref="authenticationManager" />
<beans:property name="accessDecisionManager" ref="httpRequestAccessDecisionManager" />
<beans:property name="observeOncePerRequest" value="false" />
</beans:bean>
這個過濾器都將被聲明爲過濾器鏈的最後一個,是這樣的:
<custom-filter position="LAST" ref="filterSecurityInterceptor" />
注:我故意省略了其它豆類的聲明,爲了不使這個答案太大。
我有一個問題爲choquero70。你知道WebInvocationPrivilegeEvaluator是否具有控制器方法中@PreAutorize註釋的知識嗎? – 2015-03-04 11:11:52