セッションの使用可否はサーブレット単位に行いないます。設定にはwisdom_config.xmlの設定が必要です。
例) wisdom_config.xmlのサンプル
<wisdom_config> <servletRuntimeConfig className='wisdom.core.runtime.RunTimeServlet' enablesSession="true"> <authenticate required="true" authType="form" authClass="sitool.web.AuthenticateImpl"/> ・・・・ </wisdom_config>
Widsom上で認証ロジックを行うクラスはwisdom.core.application.IAuthenticateインタフェースを実装する必要があります。実行時にはDBへ接続するためのIRequestHandler、入力されたユーザーID、パスワードが引数で渡されるので任意の認証ロジックを実装し、認証されたユーザーに対してwisdom.core.session.UserSessionFactoryクラスを使用してセッション情報を作成してください。認証に失敗した場合、wisdom.core.runtime.AuthenticateExceptionを送出してください。(この例外をキャッチし、ログイン画面へ遷移するようにWisdomを設定します。)
例)IAuthenticateを実装したtutorial.AutenticateImplクラス
package tutorial;
import java.sql.*;
import javax.servlet.http.*;
import wisdom.core.CoreMessageGenerator;
import wisdom.core.Message;
import wisdom.core.application.IAuthenticate;
import wisdom.core.application.IRequestHandler;
import wisdom.core.application.IUserSession;
import wisdom.core.runtime.AuthenticateException;
import wisdom.core.session.UserSessionFactory;
public class AuthenticateImpl implements IAuthenticate {
public static final String sql = "SELECT PASSWORD,USERNAME FROM user WHERE USERID = ? FETCH FIRST 1 ROW ONLY";
/**
* ユーザー認証を行います。
* @param user ユーザーID
* @param password パスワード
* @exception 実行に送出される例外。
*/
public void authenticate(IRequestHandler rh, String user, String password) throws Exception {
if (user != null && user.length() > 10) {// ユーザIDが11桁以上入力
throw new AuthenticateException(CoreMessageGenerator.getInstance().getMessage("WAE0010"));
}
String code = "";
boolean isValidUser = false;
PreparedStatement ps = rh.getConnection(this).prepareStatement(sql);
ps.setString(1, user);
ResultSet rs = ps.executeQuery();
String password_ = null;
String userName = null;
String deptId = null;
if (rs.next()) {
password_ = rs.getString(1).trim();
userName = rs.getString(2).trim();
if (password.equals(password_)) {// パスワードが一致
IUserSession us = UserSessionFactory.create(rh, user );
isValidUser = true;// 認証OK
} else code = "WAE0020";// パスワードが不一致
} else code = "WAE0010";// ユーザ見つからない
rs.close();
ps.close();
if (!isValidUser) throw new AuthenticateException(CoreMessageGenerator.getInstance().getMessage(code));
}
}
以下のように、
wisdom.core.tuntime.AuthenticateException、wisdom.core.session.SessionTimeOutedExceptionが発生した場合、ログイン画面へ遷移するよう設定します。
<wisdom_config> <servletRuntimeConfig className='wisdom.core.runtime.RunTimeServlet' enablesSession="true"> <authenticate required="true" authType="form" authClass="sitool.web.AuthenticateImpl"/> <allowedMethods post="true" get="true"/> <commandFileName name="runtime-command.xml"/> <exception className='ProhibitMethodExceoption' page='error.jsp'/> <exception className='wisdom.core.runtime.ReqidNotFoundException' page='/error.jsp'/> <exception className='wisdom.core.runtime.AuthenticateException' page='/login.jsp'/> <exception className='wisdom.core.session.SessionTimeOutedException' page='/login.jsp'/> ・・・・・