gradle jettyでJDBCRealm

2014-10-15T00:00:00+00:00 gradle

参考: http://wiki.eclipse.org/Jetty/Tutorial/Realms#JDBCLoginService

タイトル通り。ちょっとgradle jettyRunでJDBCRealmを使いたい場合のメモ

build.gradle

apply plugin: "java"
apply plugin: "jetty"
apply plugin: "eclipse"

configurations {
    jettyRuntime
}

repositories {
    mavenCentral()
}

dependencies {
    jettyRuntime "mysql:mysql-connector-java:+"
}

jettyRun {
    jettyEnvXml = file("jetty-env.xml")
    doFirst {
        configurations.jettyRuntime.each { file ->
            gradle.class.classLoader.addURL(file.toURI().toURL())
        }
    }
}

っていう感じで。JDBCRealmを使う為の設定はjetty-env.xmlを参照するのでここではパス。ただし、JDBCRealmを使う際にあたってのクラスパスはgradle側に直接打ち込まないと上手く解決出来なかったので(ry

jetty-env.xml

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">
<Configure class="org.mortbay.jetty.webapp.WebAppContext">
    <Get name="securityHandler">
        <Set name="userRealm">
            <New class="org.mortbay.jetty.security.JDBCUserRealm">
                <Set name="name">default</Set>
                <Set name="config">realm.properties</Set>
            </New>
        </Set>
    </Get>
</Configure>

JAX-RSネタで書いてるのをHashUserRealmからJDBCUserRealmに変えるだけ。残りの必要な情報はrealm.propertiesに記載する

realm.properties

jdbcdriver = com.mysql.jdbc.Driver
url = jdbc:mysql://localhost/sample
username = [MySQLのusername]
password = [MySQLのpassword]
usertable = users 
usertablekey = id
usertableuserfield = username
usertablepasswordfield = pwd
roletable = roles
roletablekey = id
roletablerolefield = role_name
userroletable = user_roles
userroletableuserkey = user_id
userroletablerolekey = role_id
cachetime = 0

んまぁ上記参考サイト側を見れば分かるけど、usersテーブルとrolesテーブルとuser_rolesテーブルを作って普通にデータを打ち込めばいい

ちなみにパスワードに関する所は http://grepcode.com/file_/repo1.maven.org/maven2/org.eclipse.jetty.aggregate/jetty-all/9.2.2.v20140723/org/eclipse/jetty/util/security/Credential.java/?v=source を見れば分かるように一部の接頭辞を持つ文字列に組み合わせればMD5などでぶちこんでもいいっぽい

ってな感じでやればgradle jettyRunを使ってJDBCRealmが利用できる

JAX-RSをやってみる (19) - OAuth1Providerを実装する - JAX-RSをやってみる (18) - OAuth1Provider -