LoginSignup
0
0

More than 5 years have passed since last update.

Spring XD の認証機能を有効化した話

Last updated at Posted at 2015-12-06

Spring XD を扱うにあたって、実際に VPS などへデプロイすることを考えた場合、認証機能が必要になってくるかと思う。
もちろん Spring XD は認証機能も備えているので、今回はこれを有効にしてみます。

マニュアルらしきものはこちら。
https://github.com/spring-projects/spring-xd/blob/master/src/docs/asciidoc/Application-Configuration.asciidoc#enabling-authentication

環境

  • Ubuntu 14.04
  • Nginx 1.4.6
  • Java 1.8.0_66
  • Spring XD 1.3.0.RELEASE
  • Flo for Spring XD 1.0.0 GA (flo-spring-xd-admin-ui-client-1.3.0.RELEASE)
$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=14.04
DISTRIB_CODENAME=trusty
DISTRIB_DESCRIPTION="Ubuntu 14.04.3 LTS"
$ uname -a
Linux 133-130-119-232 3.16.0-51-generic #69~14.04.1-Ubuntu SMP Wed Oct 7 15:32:41 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
$ nginx -v
nginx version: nginx/1.4.6 (Ubuntu)
$ java -version
java version "1.8.0_66"
Java(TM) SE Runtime Environment (build 1.8.0_66-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.66-b17, mixed mode)
$ ./xd-shell
 _____                           __   _______
/  ___|          (-)             \ \ / /  _  \
\ `--. _ __  _ __ _ _ __   __ _   \ V /| | | |
 `--. \ '_ \| '__| | '_ \ / _` |  / ^ \| | | |
/\__/ / |_) | |  | | | | | (_| | / / \ \ |/ /
\____/| .__/|_|  |_|_| |_|\__, | \/   \/___/
      | |                  __/ |
      |_|                 |___/
eXtreme Data
1.3.0.RELEASE | Admin Server Target: http://localhost:9393
Welcome to the Spring XD shell. For assistance hit TAB or type "help".
xd:>

以前セットアップした環境を引き続き利用します。

Spring XD 設定の変更

Spring XD は、Spring Boot と概ね同じようなコンフィギュレーションになっているため、設定すべき箇所は見当をつけやすい。

config ディレクトリがあるので、その中の YAML ファイルを編集する。
security を設定する箇所を以下のとおり編集すれば、認証機能の有効化と、ログインユーザーの設定は完了。

/opt/lib/spring-xd/xd/config/servers.yml
security:
  basic:
    enabled: true # false to disable security settings (default)
    realm: SpringXD
  user: # valid only if security.basic.enabled=true
    name: adminUserName
    password: adminUserPassword
    role: ADMIN, VIEW, CREATE

Spring XD を編集するとログイン画面が現れるので、先ほど設定した内容でログインしてみる。
スクリーンショット 2015-12-05 13.54.22.png

Spring XD のフロントに Nginx を置いたらハマった話でも発生した 404。
また起きました。

今度は /authenticate というエンドポイントに対して、直接アクセスをしているようなので、以下のとおり Nginx の設定を変更します。

/etc/nginx/sites-enabled/default
        location /authenticate {
                if ($http_referer ~* /spring-xd/admin-ui/$) {
                        proxy_pass http://localhost:9393$request_uri;
                }
        }

ログインできました!
画面右上にユーザー名も表示されているので、これで良さそうです。
スクリーンショット 2015-12-05 13.55.49.png

Nginx 設定の見直し

さて、これで認証機能を含めた Spring XD の設定は完了しているのですが、ここまでで Spring XD には、いくつかのイレギュラーなリクエストがあることがわかりました。

そこで、Nginx の設定を見直して、今後 同様のリクエストが新たなエンドポイントに対して発生しても、同様に処理できるようにしてみます。

最終的に以下のようになりました。

/etc/nginx/sites-enabled/default
server {

        ...

        location / {
                # Spring XD
                set $spring_xd_json 1;

                if ($http_accept !~* application/json) {
                        set $spring_xd_json 0;
                }

                if ($http_referer !~* /spring-xd/admin-ui/$) {
                        set $spring_xd_json 0;
                }

                if ($spring_xd_json = 1) {
                        proxy_pass http://localhost:9393$request_uri;
                }

                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules
        }

        # Spring XD
        location /spring-xd/ {
                proxy_pass http://localhost:9393/;
        }
}

リクエストヘッダに Accept: application/json を持った、/spring-xd/admin-ui/ からのリクエストを、一括でそのままプロキシしてやるような設定です。
これで今後、新たにイレギュラーなリクエストを投げてくるエンドポイントが現れても、とくに対応せず処理されることでしょう。

ここまでで環境は概ねできたように思うので、あとは実際に使っていくのを試していこうと思う。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0