非推奨定数のドキュメントは、あなたが何をすべきかを正確に伝えます。このような
/**
* @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>
をログインで.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}"/>"/>
ありがとうございました!それは必要なように動作します! – droidpl
問題ありません。いくつかの点で改善の余地があることに注意してください。例えば。 .jsp内のセッション属性の名前をハードコードするのはエレガントではありません。理想的には、 'UserNameCachingAuthenticationFailureHandler'で定義された定数を参照するべきです。 (私はこのインダイレクションを達成する方法がわからないことに気付かなければならない) – zagyi
これは達成できるかどうかわからなかった!しかし、今はそれが私が必要とするすべてのものです。これはセキュリティではなく、ユーザーの生活をより良くするための小さなトリックです。私はこのような別の簡単な解決策を見つけることができないと信じられないほどです! ;) – droidpl