差分
このページの2つのバージョン間の差分を表示します。
| 両方とも前のリビジョン前のリビジョン次のリビジョン | 前のリビジョン | ||
| study:java:rememberme [2025/02/03 14:48] – [MySavedRequestAwareAuthenticationSuccessHandler.java] banana | study:java:rememberme [2025/02/07 10:06] (現在) – banana | ||
|---|---|---|---|
| 行 2: | 行 2: | ||
| ログインを維持してくれるremember-meログイン機能をSpring securityを用いて実装する方法を紹介します。\\ | ログインを維持してくれるremember-meログイン機能をSpring securityを用いて実装する方法を紹介します。\\ | ||
| 実装形態は次節で説明しますが、ここでは、persistence token実装形態を選択しています。 | 実装形態は次節で説明しますが、ここでは、persistence token実装形態を選択しています。 | ||
| + | {{keywords> | ||
| ====== remember-me実装形態 ====== | ====== remember-me実装形態 ====== | ||
| 行 37: | 行 38: | ||
| ^ライブラリ^説明^ | ^ライブラリ^説明^ | ||
| - | |jcl-over-slf4j-1.7.36.jar|Spring | + | |jcl-over-slf4j-1.7.36.jar|spring |
| - | |log4j-slf4j-impl-2.17.1.jar|log4j2 | + | |log4j-slf4j-impl-2.17.1.jar|spring security loggingをlog4j2.xmlにて設定可能にする| |
| - | |slf4j-api-1.7.32.jar|loggingライブラリ| | + | |slf4j-api-1.7.32.jar|spring security |
| |spring-web-4.3.30.RELEASE.jar|spring-web| | |spring-web-4.3.30.RELEASE.jar|spring-web| | ||
| |spring-core-4.3.30.RELEASE.jar|spring-webの依存ライブラリ| | |spring-core-4.3.30.RELEASE.jar|spring-webの依存ライブラリ| | ||
| 行 349: | 行 350: | ||
| webSecurityConfig.xmlにて参照名mySavedRequestAwareAuthenticationSuccessHandlerのクラスを以下に示します。 | webSecurityConfig.xmlにて参照名mySavedRequestAwareAuthenticationSuccessHandlerのクラスを以下に示します。 | ||
| <code java> | <code java> | ||
| - | package com.nsk.contoso.security; | + | package com.contoso.web.security; |
| import java.io.IOException; | import java.io.IOException; | ||
| 行 445: | 行 446: | ||
| ★1\\ | ★1\\ | ||
| onAuthenticationSuccessメソッドをオーバーライドします。\\ | onAuthenticationSuccessメソッドをオーバーライドします。\\ | ||
| - | ここでは、認証成功後の権限付与(authentication)、遷移先などの設定を行います。\\ | + | ここでは、認証成功後の権限付与(authorization)、遷移先などの設定を行います。\\ |
| ★2\\ | ★2\\ | ||
| AuthenticationからLDAP認証のusernameが取得可能です。\\ | AuthenticationからLDAP認証のusernameが取得可能です。\\ | ||
| - | usernameを用いてアプリ側のauthentication処理を実装します。\\ | + | usernameを用いてアプリ側のauthorization処理を実装します。\\ |
| ★3\\ | ★3\\ | ||
| 行 461: | 行 462: | ||
| webSecurityConfig.xmlにて参照名rememberMeAuthenticationSuccessHandlerのクラスを以下に示します。 | webSecurityConfig.xmlにて参照名rememberMeAuthenticationSuccessHandlerのクラスを以下に示します。 | ||
| <code java> | <code java> | ||
| - | package com.nsk.epareq.web.security; | + | package com.contoso.web.security; |
| import java.io.IOException; | import java.io.IOException; | ||
| 行 575: | 行 576: | ||
| ★1\\ | ★1\\ | ||
| onAuthenticationSuccessメソッドをオーバーライドします。\\ | onAuthenticationSuccessメソッドをオーバーライドします。\\ | ||
| - | ここでは、認証成功後の権限付与(authentication)、遷移先などの設定を行います。\\ | + | ここでは、認証成功後の権限付与(authorization)、遷移先などの設定を行います。\\ |
| ★2\\ | ★2\\ | ||
| 行 582: | 行 583: | ||
| ★3\\ | ★3\\ | ||
| AuthenticationからLDAP認証のusernameが取得可能です。\\ | AuthenticationからLDAP認証のusernameが取得可能です。\\ | ||
| - | usernameを用いてアプリ側のauthentication処理を実装します。\\をを | + | usernameを用いてアプリ側のauthorization処理を実装します。\\をを |
| ===== NewLogon.jsp ===== | ===== NewLogon.jsp ===== | ||
| Formログイン画面の例を以下に示します。 | Formログイン画面の例を以下に示します。 | ||
| - | < | + | < |
| <%@ page language=" | <%@ page language=" | ||
| <%@ include file="/ | <%@ include file="/ | ||
| 行 614: | 行 615: | ||
| </ | </ | ||
| + | ===== NewLogonAction.java ===== | ||
| + | ログイン画面Actionの例を以下に示します。\\ | ||
| + | ここではStruts1を利用したActionクラスの例です。 | ||
| + | <code java> | ||
| + | package com.contoso.web; | ||
| + | |||
| + | import javax.servlet.http.HttpServletRequest; | ||
| + | import javax.servlet.http.HttpServletResponse; | ||
| + | |||
| + | import org.apache.struts.action.Action; | ||
| + | import org.apache.struts.action.ActionError; | ||
| + | import org.apache.struts.action.ActionErrors; | ||
| + | import org.apache.struts.action.ActionForward; | ||
| + | import org.apache.struts.action.ActionMapping; | ||
| + | |||
| + | /** | ||
| + | * <H3> | ||
| + | * Action for Remember-me Login | ||
| + | * </H3> | ||
| + | * @author ri-su | ||
| + | */ | ||
| + | public class NewLogonAction extends Action { | ||
| + | |||
| + | //***** public method ***** | ||
| + | |||
| + | @Override | ||
| + | public ActionForward execute(ActionMapping mapping, AbstractActionForm form, HttpServletRequest request, | ||
| + | HttpServletResponse response) throws Exception | ||
| + | if (request.getParameter(" | ||
| + | ActionErrors _errors = new ActionErrors(); | ||
| + | _errors.add(" | ||
| + | saveErrors(request, | ||
| + | return mapping.getInputForward(); | ||
| + | } | ||
| + | |||
| + | return mapping.findForward(SUCCESS); | ||
| + | } | ||
| + | |||
| + | //***** protected method ***** | ||
| + | //***** private method ***** | ||
| + | //***** call back method ***** | ||
| + | //***** getter and setter ***** | ||
| + | |||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | ★1\\ | ||
| + | ログインエラー発生時、エラーメッセージをActionErrorにセットして画面に表示します。 | ||
| ====== 参考情報 ====== | ====== 参考情報 ====== | ||