4

私はSpring Frameworkの初心者です。私と友人は、ポズナン工科大学でエンジニアの論文を書いていますが、Spring Security (3.1.0) に問題があります。うまくログアウトできません。再度ログインしようとすると、「ユーザーはすでにログインしています」というメッセージが表示されます (標準の Spring Security エラー メッセージをオーバーライドしました)。SecurityContextHolder のコンテキストをクリアしようとしましたが、まだ機能しません。

spring-security.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:security="http://www.springframework.org/schema/security"
    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.1.xsd
    http://www.springframework.org/schema/security
    http://www.springframework.org/schema/security/spring-security-3.1.xsd">

    <security:http auto-config="true" create-session="ifRequired">
        <security:intercept-url pattern="/start"
            access="IS_AUTHENTICATED_ANONYMOUSLY" />
        <security:intercept-url pattern="/home" access="ROLE_USER" />       
        <security:session-management>
            <security:concurrency-control 
                max-sessions="1" error-if-maximum-exceeded="true" />
        </security:session-management>
        <security:form-login login-page="/start"
            default-target-url="/home" authentication-failure-url="/login_error?error=true"
            always-use-default-target="true" />
        <security:logout invalidate-session="true" logout-success-url="/start" logout-url="/j_spring_security_logout"/>
    </security:http>
    <security:authentication-manager>
        <security:authentication-provider ref="myAuthenticationProvider"/>
    </security:authentication-manager>


    <bean id="myAuthenticationProvider" name="myAuthenticationProvider" class="org.pp.web.Authentication.XtbAuthenticationProvider"/>
</beans>`

web.xml

<!-- Spring Security -->
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>
                  org.springframework.web.filter.DelegatingFilterProxy
        </filter-class>
    </filter>



    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

home.jsp

<a href="<c:url value="/logout" />">Logout</a>

コントローラー.java

@RequestMapping(value = "logout")
public String logout() {
    SecurityContextHolder.clearContext();
    return "redirect:/j_spring_security_logout";
}

@RequestMapping(value = "start")
public String start(Model model, HttpServletRequest request) {
    // sprawdzenie czy uzytkownik nie jest juz zalogowany
    if (request.getRemoteUser() == null) {

        return "start";
    } else {

        return "redirect:/home";
    }
}

ログインとパスワードを確認するための独自のプロバイダーがあります。

認証プロバイダー.java

public class AuthenticationProvider implements AuthenticationProvider{

private Logger logger = Logger.getLogger(AuthenticationProvider.class);

@Override
public Authentication authenticate(Authentication authentication)
        throws AuthenticationException {

    List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
    authorities.add(new GrantedAuthorityImpl("ROLE_USER"));

    UsernamePasswordAuthenticationToken auth = (UsernamePasswordAuthenticationToken) authentication;
    String username = String.valueOf(auth.getPrincipal());
    String password = String.valueOf(auth.getCredentials());

    if(username.length()<4)
    {
        logger.warn("Error: Login is to short for username: "+ username);
        throw new BadCredentialsException("Login is to short!");
    }
    else if(password.length()<4)
    {
        logger.warn("Error: Password is to short for username: "+ username);
        throw new BadCredentialsException("Password is to short!");

    }
    else if(!(  (username.equals("login") & password.equals("password"))|
            (username.equals("login2") & password.equals("password2"))) ) {
        logger.warn("Error: Incorrect data for username: "+ username);
        throw new BadCredentialsException("Incorrect data!");
    }

    return new UsernamePasswordAuthenticationToken(
        authentication.getName(), authentication.getCredentials(),
        authorities);
}

@Override
public boolean supports(Class<?> authentication) {
    return authentication.equals(UsernamePasswordAuthenticationToken.class);
}

}

私はそれを修正しようとしていて、長い間探していましたが、解決策が見つかりません。

あなたが私を助けてくれることを願っています。

マテウシュ・ヤルムゼク、ルカシュ・グジボウスキ

編集: 標準の Spring Security エラー メッセージを上書きしました。

変更後のコード。

コントローラー.java

    @RequestMapping(value = "dummy")
public String dummy() {
    //SecurityContextHolder.clearContext();
    return "redirect:/dummy";
}


@RequestMapping(value = "logout")
public String logout() {
    //SecurityContextHolder.clearContext();
    return "redirect:/start";
}

ダミー.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>

<% 

session.invalidate(); 
// String redirectURL = "http://localhost:8080/start";
// response.sendRedirect(redirectURL);

%>

<body>
<%-- <c:redirect url='http://localhost:8080/start' /> --%>
</body>

</html>

home.jsp

<a href="<c:url value='/dummy' />">Logout</a>
4

2 に答える 2

4

JSPリダイレクトでは問題ありませんが、設定では問題ありません。

これを試して:

web.xml に追加

<listener> 
<listener-class>
org.springframework.security.web.session.HttpSessionEventPublisher
</listener-class> 
</listener>
于 2012-12-18T19:07:46.077 に答える
2

Springセキュリティログアウトの標準は次のとおりです。

SecurityContextHolder.clearContext();

編集

jspリダイレクトを使用している場合、発生する必要があるのは、次のことを行う空のjspが必要なことです。

1)セッションを無効にし
ます2)ランディングページにリダイレクトします

私が空と言うとき、私はその中の唯一のコンテンツが上記の2つの部分を実行するスクリプトレットであることを意味します。したがって、プロセスは次のようになります。

1)ユーザーがログアウトを押します
2)上記のダミーページへのリダイレクトが発生します
3)ダミーページがそのコードを実行します
4)ユーザーがシステムからログアウトしました。

JSPコード

<html>  
    <%session.invalidate()%>    
    //redirect logic
</html>  
于 2012-12-17T19:26:03.563 に答える