From 70595c41ce404ca68aaa6d1b531c6a858492553b Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sun, 23 Dec 2012 23:55:05 +0100 Subject: o Adding Apache Shiro for security. --- pom.xml | 5 +++ src/main/java/io/trygvis/esper/testing/Config.java | 10 ++++- .../esper/testing/web/JerseyApplication.java | 3 +- .../esper/testing/web/MissingShiroJdbcRealm.java | 9 ++++ .../io/trygvis/esper/testing/web/WebConfig.java | 17 ++++++++ src/main/resources/ddl-core.sql | 11 +---- src/main/resources/ddl-file.sql | 14 ++++++ src/main/resources/logback.xml | 2 + src/main/webapp/WEB-INF/shiro.ini | 18 ++++++++ src/main/webapp/WEB-INF/web.xml | 15 +++++++ src/main/webapp/index.jspx | 32 ++++++++++++-- src/main/webapp/login.jspx | 51 ++++++++++++++++++++++ .../io/trygvis/esper/testing/web/WebRunner.java | 2 +- 13 files changed, 170 insertions(+), 19 deletions(-) create mode 100644 src/main/java/io/trygvis/esper/testing/web/MissingShiroJdbcRealm.java create mode 100644 src/main/java/io/trygvis/esper/testing/web/WebConfig.java create mode 100644 src/main/resources/ddl-file.sql create mode 100644 src/main/webapp/WEB-INF/shiro.ini create mode 100644 src/main/webapp/login.jspx diff --git a/pom.xml b/pom.xml index 8547498..e578079 100644 --- a/pom.xml +++ b/pom.xml @@ -124,6 +124,11 @@ postgresql 9.1-901-1.jdbc4 + + org.apache.shiro + shiro-web + 1.2.1 + junit junit diff --git a/src/main/java/io/trygvis/esper/testing/Config.java b/src/main/java/io/trygvis/esper/testing/Config.java index 13ef1cd..5beb9bb 100644 --- a/src/main/java/io/trygvis/esper/testing/Config.java +++ b/src/main/java/io/trygvis/esper/testing/Config.java @@ -48,6 +48,8 @@ public class Config { public final String databaseUsername; public final String databasePassword; + private BoneCPDataSource dataSource; + public Config(GitoriousConfig gitorious, long nexusUpdateInterval, long jenkinsUpdateInterval, String databaseUrl, String databaseUsername, String databasePassword) { this.gitorious = gitorious; @@ -89,12 +91,16 @@ public class Config { } public BoneCPDataSource createBoneCp() throws SQLException { - return new BoneCPDataSource(new BoneCPConfig(){{ + if (dataSource != null) { + return dataSource; + } + + return dataSource = new BoneCPDataSource(new BoneCPConfig() {{ setJdbcUrl(databaseUrl); setUsername(databaseUsername); setPassword(databasePassword); setDefaultAutoCommit(false); - setCloseConnectionWatch(true); + setCloseConnectionWatch(false); setMaxConnectionsPerPartition(10); }}); } diff --git a/src/main/java/io/trygvis/esper/testing/web/JerseyApplication.java b/src/main/java/io/trygvis/esper/testing/web/JerseyApplication.java index 8d071a9..6937804 100644 --- a/src/main/java/io/trygvis/esper/testing/web/JerseyApplication.java +++ b/src/main/java/io/trygvis/esper/testing/web/JerseyApplication.java @@ -10,8 +10,7 @@ public class JerseyApplication extends Application { private final DatabaseAccess da; public JerseyApplication() throws Exception { - Config config = Config.loadFromDisk(); - this.da = new DatabaseAccess(config.createBoneCp()); + this.da = new DatabaseAccess(WebConfig.config.createBoneCp()); } @Override diff --git a/src/main/java/io/trygvis/esper/testing/web/MissingShiroJdbcRealm.java b/src/main/java/io/trygvis/esper/testing/web/MissingShiroJdbcRealm.java new file mode 100644 index 0000000..0aacf7f --- /dev/null +++ b/src/main/java/io/trygvis/esper/testing/web/MissingShiroJdbcRealm.java @@ -0,0 +1,9 @@ +package io.trygvis.esper.testing.web; + +import org.apache.shiro.realm.jdbc.*; + +public class MissingShiroJdbcRealm extends JdbcRealm { + public MissingShiroJdbcRealm() throws Exception { + setDataSource(WebConfig.config.createBoneCp()); + } +} diff --git a/src/main/java/io/trygvis/esper/testing/web/WebConfig.java b/src/main/java/io/trygvis/esper/testing/web/WebConfig.java new file mode 100644 index 0000000..02fb4ff --- /dev/null +++ b/src/main/java/io/trygvis/esper/testing/web/WebConfig.java @@ -0,0 +1,17 @@ +package io.trygvis.esper.testing.web; + +import io.trygvis.esper.testing.*; + +import java.io.*; + +public class WebConfig { + public static final Config config; + + static { + try { + config = Config.loadFromDisk(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } +} diff --git a/src/main/resources/ddl-core.sql b/src/main/resources/ddl-core.sql index 376ca9f..b145438 100644 --- a/src/main/resources/ddl-core.sql +++ b/src/main/resources/ddl-core.sql @@ -8,16 +8,6 @@ DROP TABLE IF EXISTS person_badge; DROP TABLE IF EXISTS person_jenkins_user; DROP TABLE IF EXISTS person; DROP TABLE IF EXISTS table_poller_status; -DROP TABLE IF EXISTS file; - -CREATE TABLE file ( - uuid CHAR(36) NOT NULL, - created_date TIMESTAMP NOT NULL, - url VARCHAR(1000) NOT NULL, - content_type VARCHAR(100) NOT NULL, - data BYTEA, - CONSTRAINT pk_file PRIMARY KEY (uuid) -); CREATE TABLE table_poller_status ( poller_name VARCHAR(100) NOT NULL, @@ -32,6 +22,7 @@ CREATE TABLE person ( uuid CHAR(36) NOT NULL, created_date TIMESTAMP NOT NULL, name VARCHAR(100), + mail VARCHAR(100), CONSTRAINT pk_person PRIMARY KEY (uuid) ); diff --git a/src/main/resources/ddl-file.sql b/src/main/resources/ddl-file.sql new file mode 100644 index 0000000..99e8bf6 --- /dev/null +++ b/src/main/resources/ddl-file.sql @@ -0,0 +1,14 @@ +BEGIN; + +DROP TABLE IF EXISTS file; + +CREATE TABLE file ( + uuid CHAR(36) NOT NULL, + created_date TIMESTAMP NOT NULL, + url VARCHAR(1000) NOT NULL, + content_type VARCHAR(100) NOT NULL, + data BYTEA, + CONSTRAINT pk_file PRIMARY KEY (uuid) +); + +COMMIT; diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml index 1a63a5f..1f6becf 100644 --- a/src/main/resources/logback.xml +++ b/src/main/resources/logback.xml @@ -14,6 +14,8 @@ + + diff --git a/src/main/webapp/WEB-INF/shiro.ini b/src/main/webapp/WEB-INF/shiro.ini new file mode 100644 index 0000000..21015af --- /dev/null +++ b/src/main/webapp/WEB-INF/shiro.ini @@ -0,0 +1,18 @@ +[main] + +authc.loginUrl=/login.jspx +roles.unauthorizedUrl = /access-denied.jsp + +sha256Matcher = org.apache.shiro.authc.credential.Sha256CredentialsMatcher + +myCredentialsMatcher = org.apache.shiro.authc.credential.AllowAllCredentialsMatcher + +myRealm = io.trygvis.esper.testing.web.MissingShiroJdbcRealm +myRealm.authenticationQuery = select 'wat' from person where mail = ? +myRealm.credentialsMatcher = $myCredentialsMatcher + +[urls] + +/external/** = anon + +/login.jspx = authc diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml index de37fae..0a48260 100644 --- a/src/main/webapp/WEB-INF/web.xml +++ b/src/main/webapp/WEB-INF/web.xml @@ -2,6 +2,20 @@ + + org.apache.shiro.web.env.EnvironmentLoaderListener + + + + ShiroFilter + org.apache.shiro.web.servlet.ShiroFilter + + + + ShiroFilter + /* + + Jersey
- + + +

+ + + + + + + + + + + + + + + + + +
guestYes
authenticatedYes
notAuthenticatedYes
principalYes
+

+ diff --git a/src/main/webapp/login.jspx b/src/main/webapp/login.jspx new file mode 100644 index 0000000..84e24fc --- /dev/null +++ b/src/main/webapp/login.jspx @@ -0,0 +1,51 @@ + + + + + + + + +
+ +
+ + +
+
+ +
+ +
+
+
+ +
+ +
+
+
+
+ + +
+
+
+ + FUCK--> + + <--YEAH! + +
+
+ + + + + diff --git a/src/test/java/io/trygvis/esper/testing/web/WebRunner.java b/src/test/java/io/trygvis/esper/testing/web/WebRunner.java index 36c2dfb..2e73b9f 100644 --- a/src/test/java/io/trygvis/esper/testing/web/WebRunner.java +++ b/src/test/java/io/trygvis/esper/testing/web/WebRunner.java @@ -1,6 +1,6 @@ package io.trygvis.esper.testing.web; -import io.trygvis.appsh.booter.jetty.JettyWebServer; +import io.trygvis.appsh.booter.jetty.*; import org.slf4j.bridge.*; import java.io.*; -- cgit v1.2.3