1

したがって、ユーザーログイン->ブラウザを閉じます- >ブラウザをもう一度開きます->エラーが表示されます:

HTTP Status 401 - Authentication Failed: Maximum sessions of 1 for this principal exceeded

必要なのは、セッションが無効であるというこのイベントをキャプチャし、このユーザーのすべてのセッションを削除して、通常のログインページにリダイレクトすることです。

春のセキュリティ構成:

        <http auto-config="true" use-expressions="true">
                <session-management session-fixation-protection="migrateSession">
                    <concurrency-control max-sessions="1" error-if-maximum-exceeded="true"/>
                </session-management>   
                <intercept-url pattern="/login" access="hasRole('ROLE_ANONYMOUS')" requires-channel="any"/> 

    <!--<custom-filter after="CONCURRENT_SESSION_FILTER" ref="sessionExpiration" /> -->
    <!-- .... -->

        </http>

<beans:bean id="sessionExpiration" class="com.test.security.SessionExpirationFilter">
    <beans:property name="expiredUrl">
            <beans:value>/login</beans:value>
        </beans:property>
 </beans:bean>

フィルタを実装しようとしましたが、セッションがnullであることが常に表示されます。

public class SessionExpirationFilter implements Filter, InitializingBean {
    private String expiredUrl;

    public void destroy() {
    }

    public void doFilter(ServletRequest request, ServletResponse response,
        FilterChain chain) throws IOException, ServletException {

        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpServletResponse httpResponse = (HttpServletResponse) response;

        String path = httpRequest.getServletPath();
        HttpSession session = httpRequest.getSession(false);

        System.out.println(session);
        if (session == null && !httpRequest.isRequestedSessionIdValid()) {              
            SecurityContextHolder.getContext().setAuthentication(null);
            String targetUrl = httpRequest.getContextPath()
                    + expiredUrl;
            httpResponse.sendRedirect(httpResponse.encodeRedirectURL(targetUrl));
            return;
        }
        chain.doFilter(request, response);
    }

    public void setExpiredUrl(String expiredUrl) {
        this.expiredUrl = expiredUrl;
    }
}
4

4 に答える 4

6

私が理解したところによると、ユーザーのセッションが「max-sessions」を超えた場合は、前のセッションを無効にします。プロパティ'error-if-maximum-exceeded'をfalseに設定します。Springセキュリティは前のセッションを自動的に無効にします。

あなたが何か違うことをしようとしているなら、

  1. ConcurrentSessionControlStrategyクラスを拡張し、「allowableSessionsExceeded」メソッドをオーバーライドします。
  2. 上記のBean参照を「session-management」の「session-authentication-strategy-ref」属性値として指定します。

于 2013-02-21T21:16:20.710 に答える
2

設定error-if-maximum-exceeded="false"すると、2番目のセッションが許可され、最初のセッションが無効になります。max-sessions="1"

持っている場合はmax-sessions="2"、セッションも許可し、すべてのセッションNthを無効にしますN-2

<session-management session-fixation-protection="migrateSession">
    <concurrency-control max-sessions="1" error-if-maximum-exceeded="false"/>
</session-management> 

設定error-if-maximum-exceeded="true"すると、2番目のセッションがNOT許可され、2番目のセッションが無効になります。max-sessions="1"

持っている場合は、セッションをmax-sessions="2"許可せず、無効にします。2+

于 2016-09-28T07:13:07.123 に答える
0
于 2018-09-28T14:28:57.890 に答える
-1

<session-management session-authentication-strategy-ref="sas"/>
<beans:bean id="sas" class="org.springframework.security.web.authentication.session.ConcurrentSessionControlStrategy">
        <beans:property name="maximumSessions" value="1"/>
        <beans:property name="exceptionIfMaximumExceeded" value="true"/>
        <beans:constructor-arg name="sessionRegistry" ref="sessionRegistry"/>
    </beans:bean>
    <beans:bean id="sessionRegistry" class="org.springframework.security.core.session.SessionRegistryImpl"/>

于 2017-04-04T15:34:08.217 に答える