こんにちは私は、HttpSessionにユーザーpojoを保存し、セッションが終了したらそのHttpSessionオブジェクトを無効にすることで、ユーザーが認証する独自の実装を使用するアプリケーションを持っていますセキュリティコンテキストを使用してユーザーを認証することです。 のiはサーブレットAuthenticateUserServletを持っているとしましょう:セキュリティコンテキストでプログラムでセッションを認証して作成する方法
public void doPost(HttpServletRequest req,HttpServletResponse resp)
throws ServletException,IOException{
String username=req.getParameter("username");
String password=req.getParameter("password");
if(Authenticator.check(username,password)){
HttpSession session=req.getSession(true);
session.setAttribute("user",Authenticator.getUser(username));
PrintWriter out= req.getWriter();
out.println("<h2>Welcome</h2>");
}else{
PrintWriter out= req.getWriter();
out.println("<h2>the password or username are incorrect</h2>");
}
}
コード上で私のセキュリティコンテキストの力を与えることはありませんので、私は、ユーザーが何らかの方法で伝えるログインしても大丈夫であることを確認したとき、私はされてwan't何このユーザーは、ここにアクセスできるというセキュリティコンテキストの彼の役割は私のAuthenticateUserServlet内でこのような なものです:
public void doPost(HttpServletRequest req,HttpServletResponse resp)
throws ServletException,IOException{
String username=req.getParameter("username");
String password=req.getParameter("password");
LoginContext lc = new LoginContext("my-jaas",new MyCallbackHandler(username,password));
try{
lc.login();
//notice i have not save any thing in the HTTPSeession
//i want my container to remember this user like what happens in the
// form based authentication where nothing gets saved in the httpSession
// but the user keeps logged in(cartalina uses a session object not httpsession for that)
PrintWriter out= req.getWriter();
out.println("<h2>Welcome</h2>");
}
catch(LoginException e){
PrintWriter out= req.getWriter();
out.println(e.getMessage());
}
}
私は自分のLoginModule(「私の-JAAS」)を作成していると私はフォームベース認証を設定すると、それが正常に動作しますtomcat7でそれを処理します。
サーブレット3.0と
男のようにログインすることができ、それは私の目の前にありました。十分にあなたに感謝することはできません – achabahe
私は助けることができる嬉しい:-) – flo