Project

General

Profile

Sample-spring-basic » History » Version 28

Henning Blohm, 18.09.2018 21:42

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