url - Spring Security获取intercept-url中模式的访问属性

标签 url spring-security timeout

我正在使用 Spring Security 3.0.7

如何使用 java 代码获得我在 <intercept-url> 中定义的“模式”的“访问”属性我的安全配置文件的元素?

我需要在我的自定义 session 管理过滤器中获取它们,以便如果请求的 URL 需要匿名访问,我会跳过过滤器并且不检查 session 超时。

现在我正在“手动”执行此操作,通过将请求的 URL 与我知道它们需要匿名访问的那些模式进行比较。它可以工作,但这不是一个好的解决方案,因为如果我更改 xml 配置文件,我必须更改 java 代码。

先感谢您。

最佳答案

我找到了解决方案。如果有人感兴趣,我在这里解释一下。

我在 session 管理过滤器的 doFilter 方法中添加了以下 Java 代码,用于检查是否允许用户(在本例中为匿名用户)访问请求的页面:

...
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 配置文件中注入(inject)的 session 管理过滤器的一个属性:
<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 具有带有模式和访问权限的intercept-url 元素(您不要将这些intercept-url 放在NameSpace 的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" />

注意:我故意省略了其他 bean 的声明,以免这个答案太大。

关于url - Spring Security获取intercept-url中模式的访问属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9219739/

相关文章:

java - 无法使用 Spring Security 保护 AngularJS 页面

java - 最大TCP连接持续时间一般还是在java中?

objective-c - 返回文件夹或卷的卷名称

php - 从浏览器读取 url

java - MockMvc 和 Spring Security - Null FilterChainProxy

java - SecurityContextHolder 线程安全吗?

c# - 加速 File.Exists 用于不存在的网络共享

timeout - WCF 超时问题?

javascript - 如果网站无法使用 "https://",如何在 Javascript 中转发我的页面?

angularjs - 单击刷新时,状态参数值与 angularjs 中 UI-Router 中的 url 混合