LoginSignup
0
0

More than 5 years have passed since last update.

Apache Shiro Quickstart その2

Last updated at Posted at 2017-08-16

前回はサンプルをそのまま動かしましたが、さすがに設定ファイルにユーザー情報を直書きはないと思います。
http://qiita.com/namikitakeo/items/fc593526fd354a381fbd

[users]
# format: username = password, role1, role2, ..., roleN
root = secret,admin
guest = guest,guest
presidentskroob = 12345,president
darkhelmet = ludicrousspeed,darklord,schwartz
lonestarr = vespa,goodguy,schwartz

[roles]
# format: roleName = permission1, permission2, ..., permissionN
admin = *
schwartz = lightsaber:*
goodguy = winnebago:drive:eagle5

ここらへんを参考にユーザー情報をデータベースから取得するようにします。MySQLを使うほどでもないのでApache Derbyを使います。
https://nhachicha.wordpress.com/2012/06/30/mysql-apache-derby-as-jdbcrealm-for-apache-shiro/

Apache Derbyサービスを起動します。

$ sudo su -
# cd /usr/share/derby
# ./NetworkServerControl start

ユーザー情報データベースを作成します。

$ ij
ij> connect 'jdbc:derby://localhost:1527/test;create=true';
ij> CREATE TABLE USERS (ID varchar(255) PRIMARY KEY NOT NULL, PASS varchar(255) NOT NULL);
ij> CREATE TABLE ROLES (ID varchar(255) PRIMARY KEY NOT NULL, ROLE varchar(255) NOT NULL);
ij> INSERT INTO USERS (ID,PASS) VALUES ('root','password');
ij> INSERT INTO ROLES (ID,ROLE) VALUES ('root','admin');

webディレクトリに移動。

$ cd shiro-root-1.3.2/samples/web

以下のようにshiro.iniを修正します。

jdbcRealm=org.apache.shiro.realm.jdbc.JdbcRealm
jdbcRealm.authenticationQuery = select pass from users where id = ?
jdbcRealm.userRolesQuery = select role from roles where id = ?

ds = com.jolbox.bonecp.BoneCPDataSource
ds.driverClass=org.apache.derby.jdbc.ClientDriver
ds.jdbcUrl=jdbc:derby://localhost:1527/test
jdbcRealm.dataSource=$ds

# We need to set the cipherKey, if you want the rememberMe cookie to work after restarting or on multiple nodes.
# YOU MUST SET THIS TO A UNIQUE STRING
#securityManager.rememberMeManager.cipherKey = kPH+bIxk5D2deZiIxcaaaA==

#[users]
# format: username = password, role1, role2, ..., roleN
#root = secret,admin
#guest = guest,guest
#presidentskroob = 12345,president
#darkhelmet = ludicrousspeed,darklord,schwartz
#lonestarr = vespa,goodguy,schwartz

#[roles]
# format: roleName = permission1, permission2, ..., permissionN
#admin = *
#schwartz = lightsaber:*
#goodguy = winnebago:drive:eagle5

以下のようにpom.xmlに依存関係を追加します。

<dependency>
    <groupId>com.jolbox</groupId>
    <artifactId>bonecp</artifactId>
    <version>0.7.1.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.apache.derby</groupId>
    <artifactId>derbyclient</artifactId>
    <version>10.4.2.0</version>
</dependency>

Jettyを実行。

$ mvn jetty:run

WEBブラウザで以下のURLを参照してください。
http://localhost:9080/

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