Skip to content

Commit

Permalink
Updated Spring examples
Browse files Browse the repository at this point in the history
* Remoting was slightly misconfigured, that is fixed.
* samples/spring is now fully annotation based
* the previous samples/spring was moved to samples/spring-xml
* Both examples can now be run with `mvn jetty:run`
  • Loading branch information
bdemers committed Dec 13, 2016
1 parent 9cc88cb commit 46bcb3b
Show file tree
Hide file tree
Showing 42 changed files with 1,762 additions and 96 deletions.
1 change: 1 addition & 0 deletions samples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<module>web</module>
<module>spring-client</module>
<module>spring</module>
<module>spring-xml</module>
<module>spring-hibernate</module>
<module>spring-boot-web</module>
<module>guice</module>
Expand Down
2 changes: 1 addition & 1 deletion samples/spring-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
</sign>
<!-- BUILDING PROCESS -->
<pack200>
<enabled>true</enabled>
<enabled>false</enabled>
</pack200>
<verbose>false</verbose>

Expand Down
15 changes: 15 additions & 0 deletions samples/spring-xml/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Apache Shiro + Spring Web Example (XML)
=======================================

This example creates a web application (WAR packaged) to demonstrate configuring Apache Shiro via Spring. This example also includes a Spring Remoting example.

To seen an annotations based version of this example take a look [here](../spring/README.md).

Run the Example
---------------

```
mvn jetty:run
```

Browse to `http://localhost:8080/shiro-samples-spring`
161 changes: 161 additions & 0 deletions samples/spring-xml/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one
~ or more contributor license agreements. See the NOTICE file
~ distributed with this work for additional information
~ regarding copyright ownership. The ASF licenses this file
~ to you under the Apache License, Version 2.0 (the
~ "License"); you may not use this file except in compliance
~ with the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing,
~ software distributed under the License is distributed on an
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
~ KIND, either express or implied. See the License for the
~ specific language governing permissions and limitations
~ under the License.
-->
<!--suppress osmorcNonOsgiMavenDependency -->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<parent>
<groupId>org.apache.shiro.samples</groupId>
<artifactId>shiro-samples</artifactId>
<version>1.4.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<modelVersion>4.0.0</modelVersion>
<artifactId>samples-spring-xml</artifactId>
<name>Apache Shiro :: Samples :: Spring XML</name>
<packaging>war</packaging>
<description>
Spring-based web application sample demonstrating Shiro's capabilities. Uses samples-spring module
as the web start application.
</description>

<build>
<plugins>
<plugin>
<!-- Note that you need to run mvn jetty:run-exploded to test the webstart application -->
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty.version}</version>
<configuration>
<httpConnector>
<port>8080</port>
</httpConnector>
<webApp>
<contextPath>/shiro-samples-spring</contextPath>
<baseResource implementation="org.eclipse.jetty.util.resource.ResourceCollection">
<resourcesAsCSV>${project.build.directory}/extra/,src/main/webapp</resourcesAsCSV>
</baseResource>
</webApp>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>unpack</goal>
</goals>
</execution>
</executions>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.apache.shiro.samples</groupId>
<artifactId>samples-spring-client</artifactId>
<version>${project.version}</version>
<type>zip</type>
</artifactItem>
</artifactItems>
<outputDirectory>${project.build.directory}/extra/WEB-INF/resources</outputDirectory>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webResources>
<resource>
<!-- this is relative to the pom.xml directory -->
<directory>${project.build.directory}/extra</directory>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.apache.shiro.samples</groupId>
<artifactId>samples-spring-client</artifactId>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-ehcache</artifactId>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-web</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>${hsqldb.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.shiro.samples.spring;

import javax.sql.DataSource;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.jdbc.core.JdbcTemplate;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.apache.shiro.crypto.hash.Sha256Hash;

/**
* A data populator that creates a set of security tables and test data that can be used by the
* Shiro Spring sample application to demonstrate the use of the {@link org.apache.shiro.realm.jdbc.JdbcRealm}
* The tables created by this class follow the default table and column names that {@link org.apache.shiro.realm.jdbc.JdbcRealm} uses.
*
*/
public class BootstrapDataPopulator implements InitializingBean {

private static final String CREATE_TABLES = "create table users (\n" +
" username varchar(255) primary key,\n" +
" password varchar(255) not null\n" +
");\n" +
"\n" +
"create table roles (\n" +
" role_name varchar(255) primary key\n" +
");\n" +
"\n" +
"create table user_roles (\n" +
" username varchar(255) not null,\n" +
" role_name varchar(255) not null,\n" +
" constraint user_roles_uq unique ( username, role_name )\n" +
");\n" +
"\n" +
"create table roles_permissions (\n" +
" role_name varchar(255) not null,\n" +
" permission varchar(255) not null,\n" +
" primary key (role_name, permission)\n" +
");";

private static final Logger log = LoggerFactory.getLogger(BootstrapDataPopulator.class);

protected DataSource dataSource = null;

public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}

public void afterPropertiesSet() throws Exception {
//because we're using an in-memory hsqldb for the sample app, a new one will be created each time the
//app starts, so create the tables and insert the 2 sample users on bootstrap:

JdbcTemplate jdbcTemplate = new JdbcTemplate(this.dataSource);
jdbcTemplate.execute(CREATE_TABLES);

//password is 'user1' SHA hashed and base64 encoded:
//The first argument to the hash constructor is the actual value to be hased. The 2nd is the
//salt. In this simple demo scenario, the username and the password are the same, but to clarify the
//distinction, you would see this in practice:
//new Sha256Hash( <password>, <cryptographically strong randomly generated salt> (not the username!) )
String query = "insert into users values ('user1', '" + new Sha256Hash("user1", "user1").toBase64() + "' )";
jdbcTemplate.execute(query);
log.debug("Created user1.");

//password is 'user2' SHA hashed and base64 encoded:
query = "insert into users values ( 'user2', '" + new Sha256Hash("user2", "user2").toBase64() + "' )";
jdbcTemplate.execute(query);
log.debug("Created user2.");

query = "insert into roles values ( 'role1' )";
jdbcTemplate.execute(query);
log.debug("Created role1");

query = "insert into roles values ( 'role2' )";
jdbcTemplate.execute(query);
log.debug("Created role2");

query = "insert into roles_permissions values ( 'role1', 'permission1')";
jdbcTemplate.execute(query);
log.debug("Created permission 1 for role 1");

query = "insert into roles_permissions values ( 'role1', 'permission2')";
jdbcTemplate.execute(query);
log.debug("Created permission 2 for role 1");

query = "insert into roles_permissions values ( 'role2', 'permission1')";
jdbcTemplate.execute(query);
log.debug("Created permission 1 for role 2");

query = "insert into user_roles values ( 'user1', 'role1' )";
jdbcTemplate.execute(query);
query = "insert into user_roles values ( 'user1', 'role2' )";
jdbcTemplate.execute(query);
log.debug("Assigned user1 roles role1 and role2");

query = "insert into user_roles values ( 'user2', 'role2' )";
jdbcTemplate.execute(query);
log.debug("Assigned user2 role role2");
}

}
Loading

0 comments on commit 46bcb3b

Please sign in to comment.