2014-11-24 71 views
3

我正在使用scala和play框架。我想在我的應用中使用Play安全授權。如何使用scala實現安全性授權和播放?

以前我用Java實現它的項目,發揮類似以下內容:

public class Secured extends Security.Authenticator { 
    private static String EMAIL = "Email"; 
    private static String U_COOKIE = "ucookie"; 
    public String getUsername(Context ctx) { 
     String decodedText = null; 
     String CHARSET = "ISO-8859-1"; 
     Cookies cookies = play.mvc.Controller.request().cookies(); 
     try { 
      Cookie emailCookie = cookies.get(EMAIL); 
     Cookie uCookie = cookies.get(U_COOKIE); 
     if (uCookie !=null && uCookie.value() != null) { 
    String userId = uCookie.value(); 
     } 
      if (emailCookie != null && emailCookie.value() != null) { 
       String email = emailCookie.value(); 
       try { 
        decodedText = new String(Base64.decodeBase64(email.getBytes(CHARSET))); 
       } catch (UnsupportedEncodingException e) { 
        e.printStackTrace(); 
       } 
      } 
     } catch (Exception e) { 
      Logger.error(e.getMessage()); 
     } 
     return decodedText; 
    } 

    public Result onUnauthorized(Context ctx) { 
     String done = play.mvc.Controller.request().path(); 
     return redirect(routes.RegController.signIn(done)); 
    } 
} 

和我上面授權使用我的所有方法的使用

@Security.Authenticated(Secured.class) 

我的任何方法之前在我的應用程序。

當我調用任何方法@before該方法調用安全類和驗證用戶。

現在我想用scala來實現同樣的事情。以下是我的問題....

1)是否有可能使用@來繼承和調用安全類的方法?

2)什麼是正確的方法來調用播放的安全認證?

P.S.我想使用cookie來實現安全認證/授權。

任何幫助或解決方法將是巨大的青睞..

回答

9

如果構建用於生產的應用程序: 不要做的很多框架在那裏它

用途之一:

他們也是一個很好的起點,尋找最佳實踐。

如果你想這樣做主要是爲了學習,有沒有真正的scecurity顧慮去:

https://www.playframework.com/documentation/2.3.x/ScalaActionsComposition

有查找標題AUTH它提供了一些信息,如何做到這一點。

要使認證踢之前的任何方法,你可以使用一個過濾攔截請求:

https://www.playframework.com/documentation/2.3.x/ScalaInterceptors

+0

其中所提到的框架是你最喜歡的,爲什麼?你會推薦什麼來實現一個簡單的身份驗證? – slashburn 2015-03-10 11:34:18

+0

目前似乎與最新的Play版本存在一些兼容性問題。 – slashburn 2015-03-10 12:53:43

+1

目前沒有偏好。 Deadbolt似乎是最好的選擇,因爲它甚至可能成爲遊戲的一部分。 – 2015-03-10 15:09:53