2012-03-20 26 views
2

私はlogin screenがあり、私はデータベースから資格情報をチェックすることでユーザーを認証しています。しかし、どうすればRemember me check boxを実装できますか? gmailのようにremember me(stay signed in)が存在します。私はsign.jspAuth servlet (doPost)oracle 10g eeを認証に使用しています。jspログインページで私を覚えている

答えて

5

この目的でcookiesを使用できます。 (doGetメソッドなどのdoPost、)サーブレット応答ハンドラで

次のようにクッキーを作る -

Cookie[] cookies = request.getCookies();  // request is an instance of type 
              //HttpServletRequest 
boolean foundCookie = false; 

for(int i = 0; i < cookies.length; i++) 
{ 
    Cookie c = cookies[i]; 
    if (c.getName().equals("userid")) 
    { 
     string userId= c.getValue(); 
     foundCookie = true; 
    } 
} 

Here -

if(remember_me_is_checked) 
{ 
    Cookie c = new Cookie("userid", userId.toString()); 
    c.setMaxAge(24*60*60); 
    response.addCookie(c); // response is an instance of type HttpServletReponse 
} 

それらを読むには、あなたがこのようなものを使用することができますCookieクラスの公式文書です。

+0

あなたの返事に感謝していますが、この「クッキー」を実装して「サインインしたまま」どうすればいいのですか? – Tom

+0

このリンクを参照してください、これはjsfにありますが、その背後にあるロジックを参照してください。http://www.roseindia.net/jsf/RememberMeLogin.shtml –

+0

@RakeshPatel ya有用なものですが、ブラウザからクッキーを削除する場合は、チェックボックスをもう一度チェックして、 'stay sign in 'を実装してください。 – Tom

0

Cookieを使用して実装を支援することができます。このようなもの 。

String userIdendificationKey="UserName"; 

Cookie cookie = new Cookie ("userIdendificationKey",userIdendificationKey); 
// Set the age of the cokkie 
cookie.setMaxAge(365 * 24 * 60 * 60); 
//Then add the cookies to the response 
    response.addCookie(cookie); 

その後、特定の値を確認してください。

-2

は、それは強力かつ高度にカスタマイズ認証とアクセス制御フレームワークですSpring SecurityIt

を見てみましょう。

また、Rose Indiaからコードを確認することもできます。これはより便利です。

0

安全かどうかはわかりませんが、これは私が行ったことです。 bodyタグに

login.jspをヘッドタグで

<script type="text/javascript"> 
var isLoggedIn = "${isLoggedIn}"; 
if(isLoggedIn === true) 
window.location.href="Home.jsp"; 
</script> 

iは

if(userdetails are verified) 
      {   
       if(request.getParameter("rememberMe")!=null){ 
        request.getSession().setAttribute("isLoggedIn", true); 
       } 
       RequestDispatcher rs = request.getRequestDispatcher("Home.jsp"); 
       rs.forward(request, response);     
      } 
      else 
      { 
       RequestDispatcher rs = request.getRequestDispatcher("fail.jsp"); 
       rs.include(request, response); 
      } 
以下のコードを追加

サーブレットdoPostメソッドで
<input type="checkbox" id="RememberMe" name="rememberMe"> 
<label for="RememberMe">Remember Me</label> 

以下のように私は私を記憶するためのチェックボックスを追加

これを使用すると、初めてログインするときに資格情報を要求し、ログイン情報をsessiに保存します2番目の時間にサイトにアクセスしようとすると、自動的に "login.jsp"の代わりに "Home.jsp"に行きます。

このメソッドが良い習慣であるかどうかは、他の変更が可能かどうかをご確認ください。 提案を歓迎します。

関連する問題