0

春のセキュリティは初めてです。私はこのチュートリアルに従っていました

http://www.mkyong.com/spring-security/spring-security-form-login-example/

ウェルカム ページをリクエストすると、ログイン フォームにリダイレクトされ、資格情報を入力するとウェルカム ページが表示されます。 /welcome ログインを要求しません。セッションに関連していると思いますが、これを解決する方法がわかりません。delete-cookies="JSESSIONID" も使用しましたが、機能していません。

ユーザーがログアウトしてウェルカムURLを再度ヒットしたときに、認証が必要なため、ウェルカムページではなくログインフォームに誘導する必要があります。すぐに助けが必要です私のセキュリティxmlは次のとおりです

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/security
    http://www.springframework.org/schema/security/spring-security-3.0.3.xsd">

    <http auto-config="true">
        <intercept-url pattern="/welcome*" access="ROLE_USER" />
        <form-login login-page="/login" default-target-url="/welcome"
            authentication-failure-url="/loginfailed" />
        <logout 
        logout-success-url="/logout" />

    </http>

    <authentication-manager>
      <authentication-provider>
        <user-service>
            <user name="mkyong" password="123456" authorities="ROLE_USER" />
        </user-service>
      </authentication-provider>
    </authentication-manager>

</beans:beans>
4

1 に答える 1

0

最良の方法は、次のようにSessionFilterクラスを実装してクラスを記述することjavax.servlet.Filterです。

package com.filter;
public class SessionFilter implements Filter {
    @Override
    public void destroy() {
        // TODO Auto-generated method stub

    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse res,
            FilterChain arg2) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;
        HttpSession session = request.getSession(false);
        if(null == session){
            response.sendRedirect("/login.html");
        }
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {
        // TODO Auto-generated method stub

    }
}

また、次のようにweb.xmlファイルで言及します

<filter>
    <filter-name>SessionFilter</filter-name>
    <filter-class>com.filter.SessionFilter   
</filter-class>

次のステートメントを使用して、ログアウトコントローラーでセッションを無効にすることを忘れないでください

request.getSession().invalidate();

これがあなたの問題を解決することを願っています

于 2013-09-05T08:34:30.510 に答える