11

私は春のフレームワークが初めてです。Web アプリケーションのログイン ページを作成しています。アプリケーションでアクションを実行する前に、ユーザーがログインできるようにします。ユーザーが適切な資格情報を入力した場合はすべて問題なく動作しますが、悪い資格情報を入力した場合はメッセージを表示し、入力要素にユーザー名を保持したいと考えています。メッセージの表示は問題ありませんが、非推奨の変数 SPRING_SECURITY_LAST_USERNAME を使用しないと、jps ファイルにユーザー名を保持できません。

誰かが私を助けてくれることを願っています.Spring 3を使用しています.

更新:要件には、URLにユーザー名を表示したくないと書かれています。

4

2 に答える 2

22

非推奨の定数のドキュメントには、何をすべきかが正確に示されています。

/**
 * @deprecated If you want to retain the username, cache it in a customized {@code AuthenticationFailureHandler}
 */
@Deprecated
public static final String SPRING_SECURITY_LAST_USERNAME_KEY =
           "SPRING_SECURITY_LAST_USERNAME";

このようなもの:

public class UserNameCachingAuthenticationFailureHandler
    extends SimpleUrlAuthenticationFailureHandler {

    public static final String LAST_USERNAME_KEY = "LAST_USERNAME";

    @Autowired
    private UsernamePasswordAuthenticationFilter usernamePasswordAuthenticationFilter;

    @Override
    public void onAuthenticationFailure(
            HttpServletRequest request, HttpServletResponse response,
            AuthenticationException exception)
            throws IOException, ServletException {

        super.onAuthenticationFailure(request, response, exception);

        String usernameParameter =
            usernamePasswordAuthenticationFilter.getUsernameParameter();
        String lastUserName = request.getParameter(usernameParameter);

        HttpSession session = request.getSession(false);
        if (session != null || isAllowSessionCreation()) {
            request.getSession().setAttribute(LAST_USERNAME_KEY, lastUserName);
        }
    }
}

セキュリティ設定で:

<security:http ...>
    ...
    <security:form-login
        authentication-failure-handler-ref="userNameCachingAuthenticationFailureHandler"
    ...
    />
</security:http>

<bean 
    id="userNameCachingAuthenticationFailureHandler"
    class="so.UserNameCachingAuthenticationFailureHandler">
    <property name="defaultFailureUrl" value="/url/to/login?error=true"/>
</bean>

login.jsp:

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

...

<%--in the login form definition--%>
<input id="j_username" name="j_username" type="text" 
    value="<c:out value="${sessionScope.LAST_USERNAME}"/>"/>
于 2013-02-24T15:58:12.013 に答える
4

Grails Spring セキュリティでこの問題を抱えている人がここに来たら。以下を使用できるようになりました-

import grails.plugin.springsecurity.SpringSecurityUtils;

現在、SpringSecurityUtils.SPRING_SECURITY_LAST_USERNAME_KEYこれを使用すると機能し、非推奨ではありません。

于 2015-02-11T11:46:56.350 に答える