Apache Shiro Quickstart Part 2

Last time, I moved the sample as it is, but I don't think that the user information is written directly in the configuration file. 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

Please refer to here to get user information from the database. I use Apache Derby because it's not as good as using MySQL. https://nhachicha.wordpress.com/2012/06/30/mysql-apache-derby-as-jdbcrealm-for-apache-shiro/

Start the Apache Derby service.

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

Create a user information database.

$ 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');

Move to the web directory.

$ cd shiro-root-1.3.2/samples/web

Modify shiro.ini as follows.

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

Add the dependency to pom.xml as follows.

<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>

Run Jetty.

$ mvn jetty:run

Please refer to the following URL with a web browser. http://localhost:9080/

Recommended Posts

Apache Shiro Quickstart Part 2
Apache Hadoop and Java 9 (Part 1)
Apache Shiro tutorial translation summary