Project

General

Profile

Sample-spring-basic » History » Version 23

Henning Blohm, 27.05.2014 08:23

1 8 Henning Blohm
h1. A basic Spring with Z2 modularity sample
2 2 Henning Blohm
3
The sample contained in the repository "z2-samples.spring-basic":http://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-spring-basic is a clean room example on how to use the Spring integration features described in [[how_to_spring]].
4
5 10 Henning Blohm
There is no further pre-requisite to running this sample, and you may proceed as described in [[How to run a sample]]. Here's the really fast version:
6
7 11 Henning Blohm
<pre><code class="ruby">
8 10 Henning Blohm
mkdir install
9
cd install 
10
git clone -b master http://git.z2-environment.net/z2-base.core
11
git clone -b master http://git.z2-environment.net/z2-samples.spring-basic
12
13 11 Henning Blohm
# on Linux / Mac OS:
14 10 Henning Blohm
cd z2-base.core/run/bin
15
./gui.sh
16
17
# on Windows:
18 12 Henning Blohm
cd z2-base.core\run\bin
19 10 Henning Blohm
gui.bat
20
</code></pre>
21
22 15 Henning Blohm
If you want to inspect the code using Eclipse, please create a workspace in install (i.e. *install/workspace*) and import the Git repositories and projects (see also [[Step_3_-_First_steps_with_Z2_on_Git|First steps]]).
23 2 Henning Blohm
24 16 Henning Blohm
There are three modules contained in this sample. For the moment only consider the following two:
25 2 Henning Blohm
26
h2. com.zfabrik.samples.spring-basic.services
27
28 6 Henning Blohm
This module has a classpath defined "applicationContext":http://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-spring-basic/revisions/master/entry/com.zfabrik.samples.spring-basic.services/java/src.impl/META-INF/applicationContext.xml and exposes an annotation defined bean "ComputationServiceImpl":http://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-spring-basic/revisions/master/entry/com.zfabrik.samples.spring-basic.services/java/src.impl/com/zfabrik/samples/impl/services/ComputationServiceImpl.java bean from it as a service to be consumed from another module: The *computations* bean.
29 2 Henning Blohm
30 17 Henning Blohm
The application context enables discovery of Spring beans that are marked by annotations such as @Service, @Component, @Repository:
31
32
<pre><code class="xml">
33
<beans ...>
34
	<!-- Turn on annotation based config -->
35
	<context:annotation-config/>
36
	<!-- Turn on auto discovery -->
37
	<context:component-scan base-package="com.zfabrik.samples.impl"/>
38
</beans>
39
</code></pre>
40
41
The *computations" bean implementation implements the interface "IComputationService":https://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-spring-basic/revisions/master/entry/com.zfabrik.samples.spring-basic.services/java/src.api/com/zfabrik/samples/services/IComputationService.java and is itself not visible to consumers. It's structure is roughly like this:
42
43
<pre><code class="java">
44
@Service("computations")
45
public class ComputationServiceImpl implements IComputationService {
46
47
...
48
49
}
50
</code></pre>
51
52
The *computations* bean is exposed via the Z2 component "com.zfabrik.samples.spring-basic.services/computations":https://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-spring-basic/revisions/master/entry/com.zfabrik.samples.spring-basic.services/computations.properties:
53
54
<pre><code class="ruby">
55
com.zfabrik.component.type=org.springframework.bean
56
57
#
58
# the context that defines the bean (more than one
59
# bean can be exposed like this)
60
#
61
bean.context=com.zfabrik.samples.spring-basic.services/applicationContext
62
63
#
64
# the bean name
65
#
66
bean.name=computations
67
</code></pre>
68
69
Note that the Z2 Bean component names its application context. At runtime this means that an attempt to retrieve the bean will make sure the application context is loaded - and not any earlier.
70
71 4 Henning Blohm
h2. com.zfabrik.samples.spring-basic.frontend
72 1 Henning Blohm
73 17 Henning Blohm
This module has a Web application with an application context defined in "WEB-INF/applicationContext":https://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-spring-basic/revisions/master/entry/com.zfabrik.samples.spring-basic.frontend/web/WebContent/WEB-INF/applicationContext.xml. It uses Spring AspectJ based annotation driven configuration to inject dependencies into instances of "ControllerServlet":http://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-spring-basic/revisions/master/entry/com.zfabrik.samples.spring-basic.frontend/java/src.impl/com/zfabrik/samples/impl/frontend/ControllerServlet.java.
74 1 Henning Blohm
75 17 Henning Blohm
Note the Java component descriptor that, apart from referencing the service module, holds the minimum declaration to make sure of Spring with AspectJ supported, compile-time-woven annotation based configuration:
76
77
<pre><code class="ruby">
78
com.zfabrik.component.type=com.zfabrik.java
79
80
java.privateReferences=\
81 22 Henning Blohm
	com.zfabrik.springframework.web,\
82
	org.springframework.tx,\
83 1 Henning Blohm
	org.springframework.orm,\
84
	com.zfabrik.samples.spring-basic.services
85 17 Henning Blohm
86
java.privateIncludes=\
87 22 Henning Blohm
	org.springframework.security:spring-security-core,\
88
	org.springframework:spring-aspects
89 17 Henning Blohm
90
java.compile.order=java,spring_aspectj
91
</code></pre>
92
93
94
It's "application context":http://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-spring-basic/revisions/master/entry/com.zfabrik.samples.spring-basic.frontend/web/WebContent/WEB-INF/applicationContext.xml imports the *computations* service from the other module above and enables the use of Spring configuration (note: Unlike above it does not enable discovery of Spring beans):
95
96
<pre><code class="xml">
97
<beans ... >
98
	<!-- turn on @Configurable support -->
99
	<context:spring-configured/>
100
	<!-- Turn on annotation based config -->
101
	<context:annotation-config/>
102
 	<!-- application config: Bind the bean at samples.spring.simplemodules.services/computations --> 
103
	<bean id="computations" class="com.zfabrik.springframework.ComponentFactoryBean">
104
		<property name="componentName" value="com.zfabrik.samples.spring-basic.services/computations"/>
105
		<property name="className" value="com.zfabrik.samples.services.IComputationService"/>
106
	</bean>
107
</beans>
108
</code></pre>
109 7 Henning Blohm
110 9 Henning Blohm
h2. Finally
111
112 19 Henning Blohm
Open a browser and navigate to http://localhost:8080/spring-basic to verify you get this:
113 9 Henning Blohm
114 1 Henning Blohm
!frontend.png!
115 16 Henning Blohm
116
h1. An extended Spring with Z2 modularity and some Spring Security sample
117
118
The third module *com.zfabrik.samples.spring-basic.secured* contained in the sample repository implements a very similar basic frontend to the one above but illustrating in addition:
119
120
* How to use Spring Security to secure the access to a Web application
121
* How to use Spring Security to secure methods of a bean
122
* How to use Spring Security with Spring AspectJ weaving
123
124 18 Henning Blohm
More specifically, the contained Web application knows of two users *"user"* (password "user") and *"admin"* (password "admin") that have roles @ROLE_USER@ or @ROLE_USER@ and @ROLE_ADMIN@ respectively.
125
126
The controller servlet delegates operations to a _session facade_ implemented by the class "SomeFacadeImpl":https://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-spring-basic/revisions/master/entry/com.zfabrik.samples.spring-basic.secured/java/src.impl/com/zfabrik/samples/impl/facades/SomeFacadeImpl.java that offers two methods, one requiring @ROLE_USER@, one requiring @ROLE_ADMIN@.
127
128
But let's rather have a step-by-step overview: The Java component declaration "com.zfabrik.samples.spring-basic.secured/java":https://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-spring-basic/revisions/master/entry/com.zfabrik.samples.spring-basic.secured/java/z.properties, as compared to the one of the other frontend module has some noteworthy additions:
129
130
<pre><code class="ruby">
131
com.zfabrik.component.type=com.zfabrik.java
132
133
java.privateReferences=\
134 23 Henning Blohm
	com.zfabrik.samples.spring-basic.services,\
135
	org.springframework.tx,\
136 1 Henning Blohm
	org.springframework.orm,\
137 23 Henning Blohm
	com.zfabrik.springframework.web
138 18 Henning Blohm
139
java.privateIncludes=\
140 23 Henning Blohm
	org.springframework:spring-aspects,\
141
	org.springframework.security:spring-security-core,\
142
	org.springframework.security:spring-security-web,\
143
	org.springframework.security:spring-security-config,\
144
	org.springframework.security:spring-security-aspects
145 18 Henning Blohm
146
#
147
# this enables the processing of @Secured annotations
148
#
149
aspectj.privateAspectPathByClass=\
150
	org.springframework.security.access.intercept.aspectj.aspect.AnnotationSecurityAspect
151
	
152
java.compile.order=java,spring_aspectj
153
</code></pre>
154
155
One the one it references the Spring Security module and it includes some Spring Security elements (that unfortunately must be included). Notably the support for Web apps, the *config* support, and finally the *aspect*, which is the pre-requisite for AspectJ support with Spring Security. 
156
157
We prefer AspectJ compile-time weaving over Spring AOP as it is much more consistent (non-proxy-based AOP, see also [[how_to_spring]] and elsewhere) and, as the compilation part is automatic with Z2, no extra burden to configure. 
158
159
However, to let the Spring AspectJ compiler, supported by Z2 know about the handling of Spring Security annotations used in the implementation, the corresponding _Aspect Implementation_ must be indicated. Hence the additional property "aspectj.privateAspectPathByClass". See also "AspectJCompiler":http://www.z2-environment.net/javadoc/com.zfabrik.springframework!2Fjava/impl/com/zfabrik/impl/springframework/AspectJCompiler.html for more details.
160
161
Fortunately this was the hardest part of our configuration tour. 
162
163
The Web apps "application context":https://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-spring-basic/revisions/master/entry/com.zfabrik.samples.spring-basic.secured/web/WebContent/WEB-INF/applicationContext.xml has a lot of similarities to the one of the other frontend but adds some Spring Security related configuration:
164
165
166
<pre><code class="xml">
167
...
168
169
	<!-- web security config: require basic authentication, hard-coded users "user" and "admin" -->
170
	<security:http auto-config='true' disable-url-rewriting="true">
171
		<security:intercept-url pattern="/**" access="ROLE_ADMIN,ROLE_USER" />
172
		<security:http-basic />
173
	</security:http>
174
175
	<security:authentication-manager>
176
		<security:authentication-provider>
177
			<security:user-service>
178
				<security:user name="admin" password="admin" authorities="ROLE_ADMIN,ROLE_USER" />
179
				<security:user name="user" password="user" authorities="ROLE_USER" />
180
			</security:user-service>
181
		</security:authentication-provider>
182
	</security:authentication-manager>
183
184
	<!-- method security config (in this case, only Secured annotations enabled)-->
185
	<security:global-method-security
186
		secured-annotations="enabled"
187
		mode="aspectj"
188
	/>
189
190
...
191
</code></pre>
192
193
Having users and passwords declared in the application context is is a sacrifice due to the sample nature. Other than that these declarations say that 
194
195
# the Web app uses Basic Authentication (being simpler than a form-based login for the sample), 
196
# that all requests require either @ROLE_USER@ or @ROLE_ADMIN@, and 
197
# that it makes use of _Method Security_, expecting the support of @Secured annotations with AspectJ weaving.
198
199
The controller servlet ("ControllerServlet":https://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-spring-basic/revisions/master/entry/com.zfabrik.samples.spring-basic.secured/java/src.impl/com/zfabrik/samples/impl/frontend/ControllerServlet.java) delegates all calls to the facade implementation "SomeFacadeImpl":https://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-spring-basic/revisions/master/entry/com.zfabrik.samples.spring-basic.secured/java/src.impl/com/zfabrik/samples/impl/facades/SomeFacadeImpl.java that has secured methods:
200
201
<pre><code class="java">
202
@Component
203
public class SomeFacadeImpl {
204
	private final static Logger LOG = Logger.getLogger(SomeFacadeImpl.class.getName());
205
	
206
	public SomeFacadeImpl() {
207
		LOG.info("Init of "+this);
208
	}
209
210
	@Autowired
211
	private IComputationService computations;
212
213
	@Secured("ROLE_USER")
214
	public String doSomethingWithAString(String in) {
215
		return computations.doSomethingWithAString(in);
216
	}
217
	
218
	@Secured("ROLE_ADMIN")
219
	public String doSomethingThatRequiresAdmin() {
220
		return "Yes, you can!";
221
	}	
222
}
223
</code></pre>
224
225
It does use the re-use service *computations* from the other module and gets it injected as above.
226
227 20 Henning Blohm
Now, if you open a Web browser at http://localhost:8080/spring-basic-secured you will first be asked to log in (best try "user" with password "user" first) and next offered the following options:
228 18 Henning Blohm
229
!secured.png!
230
231 21 Henning Blohm
Pressing the button beneath "Try as admin" should give you an "Access denied" response, if you did not log on with the "admin" user, while it should give some affirmative response in case you did not log with admin permissions.