Project

General

Profile

Sample-spring-basic » History » Version 26

Henning Blohm, 17.10.2015 20:14

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 26 Henning Blohm
{{include(Java Version Requirements)}}
6 25 Henning Blohm
7 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:
8
9 11 Henning Blohm
<pre><code class="ruby">
10 10 Henning Blohm
mkdir install
11
cd install 
12
git clone -b master http://git.z2-environment.net/z2-base.core
13
git clone -b master http://git.z2-environment.net/z2-samples.spring-basic
14
15 11 Henning Blohm
# on Linux / Mac OS:
16 10 Henning Blohm
cd z2-base.core/run/bin
17
./gui.sh
18
19
# on Windows:
20 12 Henning Blohm
cd z2-base.core\run\bin
21 10 Henning Blohm
gui.bat
22
</code></pre>
23
24 24 Henning Blohm
*Notes:*
25
26
* If you run into read timeouts due to weak internet connection, simply keep repeating to start z2.
27
* If you are located behind an internet proxy, please check [[How_to_proxy_settings]] on how to configure your installation.
28
29
30 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]]).
31 2 Henning Blohm
32 16 Henning Blohm
There are three modules contained in this sample. For the moment only consider the following two:
33 2 Henning Blohm
34
h2. com.zfabrik.samples.spring-basic.services
35
36 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.
37 2 Henning Blohm
38 17 Henning Blohm
The application context enables discovery of Spring beans that are marked by annotations such as @Service, @Component, @Repository:
39
40
<pre><code class="xml">
41
<beans ...>
42
	<!-- Turn on annotation based config -->
43
	<context:annotation-config/>
44
	<!-- Turn on auto discovery -->
45
	<context:component-scan base-package="com.zfabrik.samples.impl"/>
46
</beans>
47
</code></pre>
48
49
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:
50
51
<pre><code class="java">
52
@Service("computations")
53
public class ComputationServiceImpl implements IComputationService {
54
55
...
56
57
}
58
</code></pre>
59
60
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:
61
62
<pre><code class="ruby">
63
com.zfabrik.component.type=org.springframework.bean
64
65
#
66
# the context that defines the bean (more than one
67
# bean can be exposed like this)
68
#
69
bean.context=com.zfabrik.samples.spring-basic.services/applicationContext
70
71
#
72
# the bean name
73
#
74
bean.name=computations
75
</code></pre>
76
77
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.
78
79 4 Henning Blohm
h2. com.zfabrik.samples.spring-basic.frontend
80 1 Henning Blohm
81 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.
82 1 Henning Blohm
83 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:
84
85
<pre><code class="ruby">
86
com.zfabrik.component.type=com.zfabrik.java
87
88
java.privateReferences=\
89 22 Henning Blohm
	com.zfabrik.springframework.web,\
90
	org.springframework.tx,\
91 1 Henning Blohm
	org.springframework.orm,\
92
	com.zfabrik.samples.spring-basic.services
93 17 Henning Blohm
94
java.privateIncludes=\
95 22 Henning Blohm
	org.springframework.security:spring-security-core,\
96
	org.springframework:spring-aspects
97 17 Henning Blohm
98
java.compile.order=java,spring_aspectj
99
</code></pre>
100
101
102
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):
103
104
<pre><code class="xml">
105
<beans ... >
106
	<!-- turn on @Configurable support -->
107
	<context:spring-configured/>
108
	<!-- Turn on annotation based config -->
109
	<context:annotation-config/>
110
 	<!-- application config: Bind the bean at samples.spring.simplemodules.services/computations --> 
111
	<bean id="computations" class="com.zfabrik.springframework.ComponentFactoryBean">
112
		<property name="componentName" value="com.zfabrik.samples.spring-basic.services/computations"/>
113
		<property name="className" value="com.zfabrik.samples.services.IComputationService"/>
114
	</bean>
115
</beans>
116
</code></pre>
117 7 Henning Blohm
118 9 Henning Blohm
h2. Finally
119
120 19 Henning Blohm
Open a browser and navigate to http://localhost:8080/spring-basic to verify you get this:
121 9 Henning Blohm
122 1 Henning Blohm
!frontend.png!
123 16 Henning Blohm
124
h1. An extended Spring with Z2 modularity and some Spring Security sample
125
126
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:
127
128
* How to use Spring Security to secure the access to a Web application
129
* How to use Spring Security to secure methods of a bean
130
* How to use Spring Security with Spring AspectJ weaving
131
132 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.
133
134
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@.
135
136
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:
137
138
<pre><code class="ruby">
139
com.zfabrik.component.type=com.zfabrik.java
140
141
java.privateReferences=\
142 23 Henning Blohm
	com.zfabrik.samples.spring-basic.services,\
143
	org.springframework.tx,\
144 1 Henning Blohm
	org.springframework.orm,\
145 23 Henning Blohm
	com.zfabrik.springframework.web
146 18 Henning Blohm
147
java.privateIncludes=\
148 23 Henning Blohm
	org.springframework:spring-aspects,\
149
	org.springframework.security:spring-security-core,\
150
	org.springframework.security:spring-security-web,\
151
	org.springframework.security:spring-security-config,\
152
	org.springframework.security:spring-security-aspects
153 18 Henning Blohm
154
#
155
# this enables the processing of @Secured annotations
156
#
157
aspectj.privateAspectPathByClass=\
158
	org.springframework.security.access.intercept.aspectj.aspect.AnnotationSecurityAspect
159
	
160
java.compile.order=java,spring_aspectj
161
</code></pre>
162
163
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. 
164
165
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. 
166
167
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.
168
169
Fortunately this was the hardest part of our configuration tour. 
170
171
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:
172
173
174
<pre><code class="xml">
175
...
176
177
	<!-- web security config: require basic authentication, hard-coded users "user" and "admin" -->
178
	<security:http auto-config='true' disable-url-rewriting="true">
179
		<security:intercept-url pattern="/**" access="ROLE_ADMIN,ROLE_USER" />
180
		<security:http-basic />
181
	</security:http>
182
183
	<security:authentication-manager>
184
		<security:authentication-provider>
185
			<security:user-service>
186
				<security:user name="admin" password="admin" authorities="ROLE_ADMIN,ROLE_USER" />
187
				<security:user name="user" password="user" authorities="ROLE_USER" />
188
			</security:user-service>
189
		</security:authentication-provider>
190
	</security:authentication-manager>
191
192
	<!-- method security config (in this case, only Secured annotations enabled)-->
193
	<security:global-method-security
194
		secured-annotations="enabled"
195
		mode="aspectj"
196
	/>
197
198
...
199
</code></pre>
200
201
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 
202
203
# the Web app uses Basic Authentication (being simpler than a form-based login for the sample), 
204
# that all requests require either @ROLE_USER@ or @ROLE_ADMIN@, and 
205
# that it makes use of _Method Security_, expecting the support of @Secured annotations with AspectJ weaving.
206
207
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:
208
209
<pre><code class="java">
210
@Component
211
public class SomeFacadeImpl {
212
	private final static Logger LOG = Logger.getLogger(SomeFacadeImpl.class.getName());
213
	
214
	public SomeFacadeImpl() {
215
		LOG.info("Init of "+this);
216
	}
217
218
	@Autowired
219
	private IComputationService computations;
220
221
	@Secured("ROLE_USER")
222
	public String doSomethingWithAString(String in) {
223
		return computations.doSomethingWithAString(in);
224
	}
225
	
226
	@Secured("ROLE_ADMIN")
227
	public String doSomethingThatRequiresAdmin() {
228
		return "Yes, you can!";
229
	}	
230
}
231
</code></pre>
232
233
It does use the re-use service *computations* from the other module and gets it injected as above.
234
235 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:
236 18 Henning Blohm
237
!secured.png!
238
239 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.