2011-07-15 1 views
8

Cách tôi có thể xác thực người dùng của mình trong AppEngine bằng Tài khoản Google thật đơn giản.Google AppEngine: xác thực tùy chỉnh

Tuy nhiên, tôi cần sử dụng hệ thống đăng nhập xác thực tùy chỉnh của mình.

Tôi sẽ có bảng AppUsers, với tên người dùng và mật khẩu được mã hóa.

Tôi đã đọc nội dung nào đó về các phiên trên gae, nhưng tôi cần trợ giúp về việc bắt đầu bảo mật ứng dụng của mình.

Làm cách nào để theo dõi phiên người dùng đã được xác thực của tôi? Đặt cookie?

Người mới bắt đầu.

+0

Bạn có lẽ nên thêm thẻ cho thời gian chạy bạn đang sử dụng (Python/Java) để kéo đám đông phù hợp. Và hãy xem các khuôn khổ có thể giúp bạn ra ngoài với phần phiên của sự vật –

+0

Có thư viện phiên cho App Engine - bạn không chắc chắn về điều gì? –

Trả lời

6

Bạn có thể sử dụng cookie để làm như vậy ... Nó thực sự không quá khó. Bạn có thể sử dụng cookie để theo dõi người dùng đã được xác thực và lưu khóa phiên trong kho dữ liệu gae.

Có một ví dụ (Nó chỉ hiển thị các ý tưởng cơ bản, tôi không đảm bảo mã có thể được sử dụng trực tiếp)

Những thành viên cơ bản Bảng:

# simply add an property to store the session key 
class User(db.Model):  
    username = db.StringProperty() 
    password = db.StringProperty() 
    session = db.StringProperty() 

Chức năng Login

# Do the following step: 
# 1. make sure user provide correct username and password 
# 2. generate a random session key 
# 3. store the session key to datastore 
# 4. set the session key and user name in cookie 
class LoginAPI(Webapp.RequestHandler): 
    def get(self): 
     username = self.getVar('username', username) 
     password = self.getVar('password', password) 

     user = User.all().filter("username = ", username).get() 
     password = encrypted_the_password(password) # encrypted your password with your own method! 

     if user.password == password: 
      # User login successfually 
      session = generate_random_session_key() # generate your session key here 
      user.session = session 
      user.put() 

      expires_time = decide_your_expires_time() # decide how long the login session is alive. 
      cookie_time_format = "%a, %d-%b-%Y %H:%M:%S GMT" 
      expires_datetime = datetime.datetime.fromtimestamp(expires_time) 

      # set cookie as session 
      self.response.headers.add_header("Set-Cookie", "user=%s; expires=%s; path=/" % (user.username,expires_datetime.strftime(cookie_time_format))) 
      self.response.headers.add_header("Set-Cookie", "session=%s; expires=%s; path=/" % (user.session, expires_datetime.strftime(cookie_time_format))) 
     else: 
      #User login failed 
      pass 

chức năng logout

# Remove the previous cookie info 
class LoginAPI(Webapp.RequestHandler): 
     def get(self): 
      # remove the cookie 
      self.response.headers.add_header("Set-Cookie", "user=%s; expires=%s; path=/" % ("",expires_datetime.strftime(cookie_time_format))) 
      self.response.headers.add_header("Set-Cookie", "session=%s; expires=%s; path=/" % ("", expires_datetime.strftime(cookie_time_format))) 

Khi bạn yêu cầu đăng nhập người dùng

# Get the session info from cookie. If the session info match the info stored in datastore 
# Then user authenticate successfully. 
class SomePage(Webapp.RequestHandler): 
    def get(self): 
     # get cookie info 
     username_from_cookie = self.request.cookies.get("user", "") 
     session_from_cookie = self.request.cookies.get("session", "") 

     if username_from_cookie and session_from_cookie: 
      user = User.all().filter("username = ", username_from_cookie).get() 
      if user.session == session_from_cookie: 
       # the user is login correctly 
       pass 
      else: 
       # the user is not login 
       pass 
     else: 
      # the user is not login 
      pass 
+1

Thực sự không cần phải tái tạo lại bánh xe - có các thư viện phiên làm việc này cho bạn. –

+1

Bạn có thể cung cấp thêm chi tiết về thư viện phiên cho GAE không? – lucemia

+1

quoto. Câu trả lời hay ... tuy nhiên tôi đã không đề cập đến tôi sẽ sử dụng JAVA. :) –