Skip to content

Commit 46bcb3b

Browse files
committed
Updated Spring examples
* 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`
1 parent 9cc88cb commit 46bcb3b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1762
-96
lines changed

samples/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
<module>web</module>
3939
<module>spring-client</module>
4040
<module>spring</module>
41+
<module>spring-xml</module>
4142
<module>spring-hibernate</module>
4243
<module>spring-boot-web</module>
4344
<module>guice</module>

samples/spring-client/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@
119119
</sign>
120120
<!-- BUILDING PROCESS -->
121121
<pack200>
122-
<enabled>true</enabled>
122+
<enabled>false</enabled>
123123
</pack200>
124124
<verbose>false</verbose>
125125

samples/spring-xml/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Apache Shiro + Spring Web Example (XML)
2+
=======================================
3+
4+
This example creates a web application (WAR packaged) to demonstrate configuring Apache Shiro via Spring. This example also includes a Spring Remoting example.
5+
6+
To seen an annotations based version of this example take a look [here](../spring/README.md).
7+
8+
Run the Example
9+
---------------
10+
11+
```
12+
mvn jetty:run
13+
```
14+
15+
Browse to `http://localhost:8080/shiro-samples-spring`

samples/spring-xml/pom.xml

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Licensed to the Apache Software Foundation (ASF) under one
4+
~ or more contributor license agreements. See the NOTICE file
5+
~ distributed with this work for additional information
6+
~ regarding copyright ownership. The ASF licenses this file
7+
~ to you under the Apache License, Version 2.0 (the
8+
~ "License"); you may not use this file except in compliance
9+
~ with the License. You may obtain a copy of the License at
10+
~
11+
~ http://www.apache.org/licenses/LICENSE-2.0
12+
~
13+
~ Unless required by applicable law or agreed to in writing,
14+
~ software distributed under the License is distributed on an
15+
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
~ KIND, either express or implied. See the License for the
17+
~ specific language governing permissions and limitations
18+
~ under the License.
19+
-->
20+
<!--suppress osmorcNonOsgiMavenDependency -->
21+
<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">
22+
23+
<parent>
24+
<groupId>org.apache.shiro.samples</groupId>
25+
<artifactId>shiro-samples</artifactId>
26+
<version>1.4.0-SNAPSHOT</version>
27+
<relativePath>../pom.xml</relativePath>
28+
</parent>
29+
30+
<modelVersion>4.0.0</modelVersion>
31+
<artifactId>samples-spring-xml</artifactId>
32+
<name>Apache Shiro :: Samples :: Spring XML</name>
33+
<packaging>war</packaging>
34+
<description>
35+
Spring-based web application sample demonstrating Shiro's capabilities. Uses samples-spring module
36+
as the web start application.
37+
</description>
38+
39+
<build>
40+
<plugins>
41+
<plugin>
42+
<!-- Note that you need to run mvn jetty:run-exploded to test the webstart application -->
43+
<groupId>org.eclipse.jetty</groupId>
44+
<artifactId>jetty-maven-plugin</artifactId>
45+
<version>${jetty.version}</version>
46+
<configuration>
47+
<httpConnector>
48+
<port>8080</port>
49+
</httpConnector>
50+
<webApp>
51+
<contextPath>/shiro-samples-spring</contextPath>
52+
<baseResource implementation="org.eclipse.jetty.util.resource.ResourceCollection">
53+
<resourcesAsCSV>${project.build.directory}/extra/,src/main/webapp</resourcesAsCSV>
54+
</baseResource>
55+
</webApp>
56+
</configuration>
57+
</plugin>
58+
<plugin>
59+
<groupId>org.apache.maven.plugins</groupId>
60+
<artifactId>maven-dependency-plugin</artifactId>
61+
<executions>
62+
<execution>
63+
<phase>generate-resources</phase>
64+
<goals>
65+
<goal>unpack</goal>
66+
</goals>
67+
</execution>
68+
</executions>
69+
<configuration>
70+
<artifactItems>
71+
<artifactItem>
72+
<groupId>org.apache.shiro.samples</groupId>
73+
<artifactId>samples-spring-client</artifactId>
74+
<version>${project.version}</version>
75+
<type>zip</type>
76+
</artifactItem>
77+
</artifactItems>
78+
<outputDirectory>${project.build.directory}/extra/WEB-INF/resources</outputDirectory>
79+
</configuration>
80+
</plugin>
81+
<plugin>
82+
<groupId>org.apache.maven.plugins</groupId>
83+
<artifactId>maven-war-plugin</artifactId>
84+
<configuration>
85+
<webResources>
86+
<resource>
87+
<!-- this is relative to the pom.xml directory -->
88+
<directory>${project.build.directory}/extra</directory>
89+
</resource>
90+
</webResources>
91+
</configuration>
92+
</plugin>
93+
</plugins>
94+
</build>
95+
96+
<dependencies>
97+
<dependency>
98+
<groupId>org.apache.shiro.samples</groupId>
99+
<artifactId>samples-spring-client</artifactId>
100+
</dependency>
101+
<dependency>
102+
<groupId>org.apache.shiro</groupId>
103+
<artifactId>shiro-core</artifactId>
104+
</dependency>
105+
<dependency>
106+
<groupId>org.apache.shiro</groupId>
107+
<artifactId>shiro-ehcache</artifactId>
108+
</dependency>
109+
<dependency>
110+
<groupId>org.apache.shiro</groupId>
111+
<artifactId>shiro-spring</artifactId>
112+
</dependency>
113+
<dependency>
114+
<groupId>org.apache.shiro</groupId>
115+
<artifactId>shiro-web</artifactId>
116+
</dependency>
117+
<dependency>
118+
<groupId>javax.servlet</groupId>
119+
<artifactId>javax.servlet-api</artifactId>
120+
<scope>provided</scope>
121+
</dependency>
122+
<dependency>
123+
<groupId>org.slf4j</groupId>
124+
<artifactId>slf4j-log4j12</artifactId>
125+
<scope>runtime</scope>
126+
</dependency>
127+
<dependency>
128+
<groupId>org.slf4j</groupId>
129+
<artifactId>jcl-over-slf4j</artifactId>
130+
<scope>runtime</scope>
131+
</dependency>
132+
<dependency>
133+
<groupId>log4j</groupId>
134+
<artifactId>log4j</artifactId>
135+
<scope>runtime</scope>
136+
</dependency>
137+
<dependency>
138+
<groupId>org.springframework</groupId>
139+
<artifactId>spring-context</artifactId>
140+
</dependency>
141+
<dependency>
142+
<groupId>org.springframework</groupId>
143+
<artifactId>spring-jdbc</artifactId>
144+
</dependency>
145+
<dependency>
146+
<groupId>org.springframework</groupId>
147+
<artifactId>spring-webmvc</artifactId>
148+
</dependency>
149+
<dependency>
150+
<groupId>hsqldb</groupId>
151+
<artifactId>hsqldb</artifactId>
152+
<version>${hsqldb.version}</version>
153+
<scope>runtime</scope>
154+
</dependency>
155+
<dependency>
156+
<groupId>javax.servlet</groupId>
157+
<artifactId>jstl</artifactId>
158+
<scope>runtime</scope>
159+
</dependency>
160+
</dependencies>
161+
</project>
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.shiro.samples.spring;
20+
21+
import javax.sql.DataSource;
22+
23+
import org.springframework.beans.factory.InitializingBean;
24+
import org.springframework.jdbc.core.JdbcTemplate;
25+
26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
28+
29+
import org.apache.shiro.crypto.hash.Sha256Hash;
30+
31+
/**
32+
* A data populator that creates a set of security tables and test data that can be used by the
33+
* Shiro Spring sample application to demonstrate the use of the {@link org.apache.shiro.realm.jdbc.JdbcRealm}
34+
* The tables created by this class follow the default table and column names that {@link org.apache.shiro.realm.jdbc.JdbcRealm} uses.
35+
*
36+
*/
37+
public class BootstrapDataPopulator implements InitializingBean {
38+
39+
private static final String CREATE_TABLES = "create table users (\n" +
40+
" username varchar(255) primary key,\n" +
41+
" password varchar(255) not null\n" +
42+
");\n" +
43+
"\n" +
44+
"create table roles (\n" +
45+
" role_name varchar(255) primary key\n" +
46+
");\n" +
47+
"\n" +
48+
"create table user_roles (\n" +
49+
" username varchar(255) not null,\n" +
50+
" role_name varchar(255) not null,\n" +
51+
" constraint user_roles_uq unique ( username, role_name )\n" +
52+
");\n" +
53+
"\n" +
54+
"create table roles_permissions (\n" +
55+
" role_name varchar(255) not null,\n" +
56+
" permission varchar(255) not null,\n" +
57+
" primary key (role_name, permission)\n" +
58+
");";
59+
60+
private static final Logger log = LoggerFactory.getLogger(BootstrapDataPopulator.class);
61+
62+
protected DataSource dataSource = null;
63+
64+
public void setDataSource(DataSource dataSource) {
65+
this.dataSource = dataSource;
66+
}
67+
68+
public void afterPropertiesSet() throws Exception {
69+
//because we're using an in-memory hsqldb for the sample app, a new one will be created each time the
70+
//app starts, so create the tables and insert the 2 sample users on bootstrap:
71+
72+
JdbcTemplate jdbcTemplate = new JdbcTemplate(this.dataSource);
73+
jdbcTemplate.execute(CREATE_TABLES);
74+
75+
//password is 'user1' SHA hashed and base64 encoded:
76+
//The first argument to the hash constructor is the actual value to be hased. The 2nd is the
77+
//salt. In this simple demo scenario, the username and the password are the same, but to clarify the
78+
//distinction, you would see this in practice:
79+
//new Sha256Hash( <password>, <cryptographically strong randomly generated salt> (not the username!) )
80+
String query = "insert into users values ('user1', '" + new Sha256Hash("user1", "user1").toBase64() + "' )";
81+
jdbcTemplate.execute(query);
82+
log.debug("Created user1.");
83+
84+
//password is 'user2' SHA hashed and base64 encoded:
85+
query = "insert into users values ( 'user2', '" + new Sha256Hash("user2", "user2").toBase64() + "' )";
86+
jdbcTemplate.execute(query);
87+
log.debug("Created user2.");
88+
89+
query = "insert into roles values ( 'role1' )";
90+
jdbcTemplate.execute(query);
91+
log.debug("Created role1");
92+
93+
query = "insert into roles values ( 'role2' )";
94+
jdbcTemplate.execute(query);
95+
log.debug("Created role2");
96+
97+
query = "insert into roles_permissions values ( 'role1', 'permission1')";
98+
jdbcTemplate.execute(query);
99+
log.debug("Created permission 1 for role 1");
100+
101+
query = "insert into roles_permissions values ( 'role1', 'permission2')";
102+
jdbcTemplate.execute(query);
103+
log.debug("Created permission 2 for role 1");
104+
105+
query = "insert into roles_permissions values ( 'role2', 'permission1')";
106+
jdbcTemplate.execute(query);
107+
log.debug("Created permission 1 for role 2");
108+
109+
query = "insert into user_roles values ( 'user1', 'role1' )";
110+
jdbcTemplate.execute(query);
111+
query = "insert into user_roles values ( 'user1', 'role2' )";
112+
jdbcTemplate.execute(query);
113+
log.debug("Assigned user1 roles role1 and role2");
114+
115+
query = "insert into user_roles values ( 'user2', 'role2' )";
116+
jdbcTemplate.execute(query);
117+
log.debug("Assigned user2 role role2");
118+
}
119+
120+
}

0 commit comments

Comments
 (0)