Play Framework 2.6でユーザ認証

こんにちは。Play Framework を使ってユーザ認証部分をやってみたいと思います。

なお、Play Framework のバージョンは 2.6 となっています。

Play Framework

2.6.6

始めましょう

まず最初に、sbt を使って新しいアプリケーションを作ります。

sbt new playframework/play-java-seed.g8
[info] Set current project to aaa (in build file:/Users/oguni/Sites/play/aaa/)

This template generates a Play Java project

name [play-java-seed]: play-java-simple-authentication
organization [com.example]:
scala_version [2.12.3]:
play_version [maven(com.typesafe.play, play_2.12, stable)]: 2.6.6
sbt_version [maven(org.scala-sbt, sbt, stable)]: 1.0.2

Template applied in ./play-java-simple-authentication

Playを起動

sbt run

起動しているのを確認します。

f:id:ekzm:20171010152322p:plainf:id:ekzm:20171010152345p:plainf:id:ekzm:20171010152345p:plain

データベース

https://www.playframework.com/documentation/2.6.x/Developing-with-the-H2-Database

が王道のようだ、ここにあるように設定。

diff --git a/build.sbt b/build.sbt
index b1170db..aa4bc92 100644
--- a/build.sbt
+++ b/build.sbt
@@ -8,3 +8,5 @@ lazy val root = (project in file(".")).enablePlugins(PlayJava)
 scalaVersion := "2.12.2"
 
 libraryDependencies += guice
+
+libraryDependencies += "com.h2database" % "h2" % "1.4.192"
diff --git a/conf/application.conf b/conf/application.conf
index 85c184d..0831f8b 100644
--- a/conf/application.conf
+++ b/conf/application.conf
@@ -1,2 +1,4 @@
 # This is the main configuration file for the application.
 # https://www.playframework.com/documentation/latest/ConfigFile
+
+db.default.url="jdbc:h2:mem:play;MODE=MYSQL;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=FALSE"

な感じで、H2データベースの設定をすます。

h2-browserを使ってみる

sbt h2-browser

トラブルシューティング

play と sbt のバージョンを指定すると動くようになりました。