如何在 Spring security 中编辑定时注销

发布于 2024-11-14 13:26:54 字数 741 浏览 2 评论 0原文

我试图找出 Spring 的轻安全性的定时注销功能所在的位置,以及如何编辑它以使用我的自定义注销方法。 我的理解是,确实存在可编辑的定时注销功能,但到目前为止我还无法找到它,并且当/如果我找到它时,我不确定如何使其使用我的注销序列。

谢谢, MirroredFate

为了清晰起见编辑:

它是一个 Web/servlet 应用程序。我正在使用 acegi 安全性。

我现在正在使用 Spring 会话超时:

In web.xml:
<session-config>
    <session-timeout>5</session-timeout>
</session-config>

我需要一种方法来在发生超时时执行一些代码。但是,我不知道该怎么做。

如果我无法使用此方法执行代码,我的理解是 acegi 有办法使会话超时;但是,我也不知道该怎么做。我已经能够使用 acegi 在正常注销时执行代码:

<security:logout invalidate-session="true"
                success-handler-ref="Logout"
                logout-url="/logout.html" />
</security:http>

那么,本质上,我如何使用 acegi 定时注销或发生会话超时时执行相同的操作?

I am trying to figure out where the timed-logout function of Spring's light security is located, and how to edit it to use my custom logout method.
My understanding is that there does exist an editable timed-logout function, I have just thus far been unable to find it, and when/if I do, I am unsure how to make it use my logout sequence.

Thanks,
MirroredFate

EDIT FOR CLARITY:

It is a web/servlet application. I am using acegi security.

I am using the Spring session timeout right now:

In web.xml:
<session-config>
    <session-timeout>5</session-timeout>
</session-config>

I need a way to execute some code when this timeout occurs. However, I have NO idea how to do this.

If I am unable to execute code using this method, my understanding is that acegi has a way to make a session timeout; however, I have no idea how to do that either. I already have the ability to execute the code on a normal logout using acegi:

<security:logout invalidate-session="true"
                success-handler-ref="Logout"
                logout-url="/logout.html" />
</security:http>

So, essentially, how do I do this same thing either with an acegi timed logout or when a session timeout occurs?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浊酒尽余欢 2024-11-21 13:26:54

HttpSessionListener 可能就是您正在寻找的。依赖 Spring 的会话管理的问题是,如果用户只是关闭浏览器而不注销,则永远不会到达 invalid-session-url(因为他们永远不会发出另一个请求)。

像这样:

public class MySessionListner implements HttpSessionListener {
     public void sessionCreated(HttpSessionEvent se) {
          return; //or maybe do something, depends on what you need
     }

     public void sessionDestroyed(HttpSessionEvent se) {
          HttpSession session = se.getSession();
          //do whatever you need to do
     }
 }

然后在 web.xml 中:

 <listener>
      <listener-class>com.foo.MySessionListener</listener-class>
 </listener>

这样每次会话被销毁时都会调用您的代码,而不仅仅是当用户在超时后尝试访问页面时。希望有帮助。

The HttpSessionListener might be what you are looking for. The problem with depending on Spring's session management is that if a user simply closes his browser without logging out, the invalid-session-url will never be reached (because they never make another request).

Something like this:

public class MySessionListner implements HttpSessionListener {
     public void sessionCreated(HttpSessionEvent se) {
          return; //or maybe do something, depends on what you need
     }

     public void sessionDestroyed(HttpSessionEvent se) {
          HttpSession session = se.getSession();
          //do whatever you need to do
     }
 }

Then in web.xml:

 <listener>
      <listener-class>com.foo.MySessionListener</listener-class>
 </listener>

That way your code will be called every time a session is destroyed, not just when a user tries to access a page after timing out. Hope that helps.

蓝戈者 2024-11-21 13:26:54

会话超时期限由您的应用程序服务器管理(就像您现在在 web.xml 中一样)。可以在 Spring 中指定会话超时时的处理方式。例如,Spring 3.0 可以指定当用户在会话失效后发出请求时将用户重定向到哪个页面。见下文。

<?xml version="1.0" encoding="UTF-8"?>

<b:beans xmlns:b="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:sec="http://www.springframework.org/schema/security"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

        <sec:http auto-config="true" access-denied-page="/login">
            <sec:session-management invalid-session-url="/session-timeout" />
            <sec:anonymous enabled="true" />
            <sec:intercept-url ... />
            <sec:form-login ... />
            <sec:logout invalidate-session="true" logout-success-url="/login" />
        </sec:http>
    <!-- Other bean declarations --> 
</b:beans>

The session timeout period is managed by your application server (just as you have it now in the web.xml). The handling for what happens when the session timeout occurs can be specified in Spring. For instance, Spring 3.0 can specify what page to redirect the user to when they make a request after their session has been invalidated. See below.

<?xml version="1.0" encoding="UTF-8"?>

<b:beans xmlns:b="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:sec="http://www.springframework.org/schema/security"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

        <sec:http auto-config="true" access-denied-page="/login">
            <sec:session-management invalid-session-url="/session-timeout" />
            <sec:anonymous enabled="true" />
            <sec:intercept-url ... />
            <sec:form-login ... />
            <sec:logout invalidate-session="true" logout-success-url="/login" />
        </sec:http>
    <!-- Other bean declarations --> 
</b:beans>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文