如何在春季退出前进行操作?

Ani*_*gra 6 java spring spring-mvc spring-security

首先,这不是一个重复的问题,我在这里检查答案!而 在这里!但无法让它发挥作用.

我也想在注销之前执行它,所以不能使用logoutSuccessHandler.

所以我需要创建一个自定义LOGOUT_FILTER,我真的很难让它工作.

这是我的spring-security xml,其中我首先尝试了两种方法: -

 <custom-filter ref="logoutFilter" position="LOGOUT_FILTER" />

<beans:bean id="logoutFilter" class="org.springframework.security.web.authentication.logout.LogoutFilter">
<beans:constructor-arg index="0" value="/logoutSuccess" />
<beans:constructor-arg index="1">
    <beans:list>
        <beans:bean id="securityContextLogoutHandler"
        class="org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler" />
    <beans:bean id="myLogoutHandler" class="com.fe.cms.listener.SimpleLogoutHandler" />
    </beans:list>
</beans:constructor-arg>
<beans:property name="filterProcessesUrl" value="/logout" />
</beans:bean>
Run Code Online (Sandbox Code Playgroud)

但这给了我错误

Configuration problem: Security namespace does not support decoration of element [custom-filter] 
Run Code Online (Sandbox Code Playgroud)

然后我试过..

<beans:bean id="logoutFilter" class="org.springframework.security.web.authentication.logout.LogoutFilter">
    <beans:constructor-arg index="0" value="/logout" />
    <beans:constructor-arg index="1">
        <beans:ref bean="securityContextLogoutHandler" />
        <beans:ref bean="myLogoutHandler" />
    </beans:constructor-arg>
    <beans:property name="filterProcessesUrl" value="/logout" />
</beans:bean>

<beans:bean id="securityContextLogoutHandler"
    class="org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler" />

<beans:bean id="myLogoutHandler" class="com.fe.cms.listener.SimpleLogoutHandler" />

<http auto-config="false" entry-point-ref="authenticationManger">
    <custom-filter ref="logoutFilter" position="LOGOUT_FILTER" />
</http>
Run Code Online (Sandbox Code Playgroud)

但这给了我错误: -

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.filterChains': Cannot resolve reference to bean 'org.springframework.security.web.DefaultSecurityFilterChain#48' while setting bean property 'sourceList' with key [48]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.web.DefaultSecurityFilterChain#48': Cannot create inner bean '(inner bean)' of type [org.springframework.security.web.access.ExceptionTranslationFilter] while setting constructor argument with key [6]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#181': Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)
Run Code Online (Sandbox Code Playgroud)

请任何人都可以告诉我在哪里做错了...如果需要,我会发布完整的xml文件

M. *_*num 2

根据您的澄清,您想要的是访问会话并执行一些逻辑。不用乱搞一个自定义,LogoutFilter只需编写一个ApplicationListener监听HttpSessionDestroyedEvents 的即可。

@Component
public class SessionListener implements ApplicationListener<HttpSessionDestroyedEvent> {

    public void onApplicationEvent(HttpSessionDestroyedEvent evt) {
        HttpSession session = evt.getSession();
        // Your logic here
    }
}
Run Code Online (Sandbox Code Playgroud)

为了能够重新接收事件,请确保您HttpSessionEventPublisher在 web.xml 中注册了事件。

<listener>
    <listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
</listener>
Run Code Online (Sandbox Code Playgroud)

该解决方案的主要优点是您还能够处理超时的会话,而不仅仅是定期注销。