This repository has been archived by the owner on Nov 10, 2017. It is now read-only.
forked from mareknovotny/jboss-seam
-
Notifications
You must be signed in to change notification settings - Fork 50
/
Security.xml
executable file
·5222 lines (4515 loc) · 198 KB
/
Security.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.3//EN" "http://www.oasis-open.org/docbook/xml/4.3/docbookx.dtd">
<chapter id="security">
<title>Security</title>
<sect1>
<title>Overview</title>
<para>
The Seam Security API provides a multitude of security-related features for your Seam-based application, covering
such areas as:
</para>
<itemizedlist>
<listitem>
<para>
Authentication - an extensible, JAAS-based authentication layer that allows users to authenticate
against any security provider.
</para>
</listitem>
<listitem>
<para>
Identity Management - an API for managing a Seam application's users and roles at runtime.
</para>
</listitem>
<listitem>
<para>
Authorization - an extremely comprehensive authorization framework, supporting user roles, persistent and
rule-based permissions, and a pluggable permission resolver for easily implementing customised security logic.
</para>
</listitem>
<listitem>
<para>
Permission Management - a set of built-in Seam components to allow easy management of an application's
security policy.
</para>
</listitem>
<listitem>
<para>
CAPTCHA support - to assist in the prevention of automated software/scripts abusing your Seam-based site.
</para>
</listitem>
<listitem>
<para>
And much more
</para>
</listitem>
</itemizedlist>
<para>
This chapter will cover each of these features in detail.
</para>
</sect1>
<sect1>
<title>Disabling Security</title>
<para>
In some situations it may be necessary to disable Seam Security, for instances during unit tests or because you
are using a different approach to security, such as native JAAS. Simply call the static method
<literal>Identity.setSecurityEnabled(false)</literal> to disable the security infrastructure. Of course, it's not
very convenient to have to call a static method when you want to configure the application, so as an alternative
you can control this setting in components.xml:
</para>
<itemizedlist>
<listitem>
<para>Entity Security</para>
</listitem>
<listitem>
<para>Hibernate Security Interceptor</para>
</listitem>
<listitem>
<para>Seam Security Interceptor</para>
</listitem>
<listitem>
<para>Page restrictions</para>
</listitem>
<listitem>
<para>Servlet API security integration</para>
</listitem>
</itemizedlist>
<para>
Assuming you are planning to take advantage of what Seam Security has to offer, the rest of this chapter documents
the plethora of options you have for giving your user an identity in the eyes of the security model
(authentication) and locking down the application by establishing constraints (authorization). Let's begin with
the task of authentication since that's the foundation of any security model.
</para>
</sect1>
<sect1>
<title>Authentication</title>
<para>
The authentication features provided by Seam Security are built upon JAAS (Java Authentication and Authorization Service),
and as such provide a robust and highly configurable API for handling user authentication. However, for less complex
authentication requirements Seam offers a much more simplified method of authentication that hides the complexity of JAAS.
</para>
<sect2>
<title>Configuring an Authenticator component</title>
<note>
<para>
If you use Seam's Identity Management features (discussed later in this chapter) then it is not necessary to create
an authenticator component (and you can skip this section).
</para>
</note>
<para>
The simplified authentication method provided by Seam uses a built-in JAAS login module, <literal>SeamLoginModule</literal>, which
delegates authentication to one of your own Seam components. This login module is already configured inside Seam as
part of a default application policy and as such does not require any additional configuration files. It allows you to
write an authentication method using the entity classes that are provided by your own application, or alternatively to
authenticate with some other third party provider. Configuring this simplified form of authentication requires the
<literal>identity</literal> component to be configured in <literal>components.xml</literal>:
</para>
<programlisting role="XML"><![CDATA[<components xmlns="http://jboss.org/schema/seam/components"
xmlns:core="http://jboss.org/schema/seam/core"
xmlns:security="http://jboss.org/schema/seam/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://jboss.org/schema/seam/components http://jboss.org/schema/seam/components-2.3.xsd
http://jboss.org/schema/seam/security http://jboss.org/schema/seam/security-2.3.xsd">
<security:identity authenticate-method="#{authenticator.authenticate}"/>
</components>]]></programlisting>
<para>
The EL expression <literal>#{authenticator.authenticate}</literal> is a method binding that indicates
the <literal>authenticate</literal> method of the <literal>authenticator</literal> component will be used
to authenticate the user.
</para>
</sect2>
<sect2>
<title>Writing an authentication method</title>
<para>
The <literal>authenticate-method</literal> property specified for <literal>identity</literal> in
<literal>components.xml</literal> specifies which method will be used by <literal>SeamLoginModule</literal>
to authenticate users. This method takes no parameters, and is expected to return a boolean, which indicates
whether authentication is successful or not. The user's username and password can be obtained from
<literal>Credentials.getUsername()</literal> and <literal>Credentials.getPassword()</literal>,
respectively (you can get a reference to the <literal>credentials</literal> component via
<literal>Identity.instance().getCredentials()</literal>). Any roles that the user is a member of
should be assigned using <literal>Identity.addRole()</literal>. Here's a complete example of an
authentication method inside a POJO component:
</para>
<programlisting role="JAVA"><![CDATA[@Name("authenticator")
public class Authenticator {
@In EntityManager entityManager;
@In Credentials credentials;
@In Identity identity;
public boolean authenticate() {
try {
User user = (User) entityManager.createQuery(
"from User where username = :username and password = :password")
.setParameter("username", credentials.getUsername())
.setParameter("password", credentials.getPassword())
.getSingleResult();
if (user.getRoles() != null) {
for (UserRole mr : user.getRoles())
identity.addRole(mr.getName());
}
return true;
}
catch (NoResultException ex) {
return false;
}
}
}]]></programlisting>
<para>
In the above example, both <literal>User</literal> and <literal>UserRole</literal> are application-specific
entity beans. The <literal>roles</literal> parameter is populated with the roles that the user is a member
of, which should be added to the <literal>Set</literal> as literal string values, e.g. "admin", "user".
In this case, if the user record is not found and a <literal>NoResultException</literal> thrown, the
authentication method returns <literal>false</literal> to indicate the authentication failed.
</para>
<tip>
<para>
When writing an authenticator method, it is important that it is kept minimal and free from
any side-effects. This is because there is no guarantee as to how many times the authenticator
method will be called by the security API, and as such it may be invoked multiple times during
a single request. Because of this, any special code that should execute upon a successful or
failed authentication should be written by implementing an event observer. See the section on
Security Events further down in this chapter for more information about which events are
raised by Seam Security.
</para>
</tip>
<sect3>
<title>Identity.addRole()</title>
<para>
The <literal>Identity.addRole()</literal> method behaves differently depending on whether the current
session is authenticated or not. If the session is not authenticated, then <literal>addRole()</literal>
should <emphasis>only</emphasis> be called during the authentication process. When called here, the
role name is placed into a temporary list of pre-authenticated roles. Once authentication is successful,
the pre-authenticated roles then become "real" roles, and calling <literal>Identity.hasRole()</literal>
for those roles will then return true. The following sequence diagram represents the list of pre-authenticated
roles as a first class object to show more clearly how it fits in to the authentication process.
</para>
<mediaobject>
<imageobject role="fo">
<imagedata fileref="images/security-addrole.png" align="center" scalefit="1"/>
</imageobject>
<imageobject role="html">
<imagedata fileref="images/security-addrole.png" align="center"/>
</imageobject>
</mediaobject>
<para>
If the current session is already authenticated, then calling <literal>Identity.addRole()</literal> will
have the expected effect of immediately granting the specified role to the current user.
</para>
</sect3>
<sect3>
<title>Writing an event observer for security-related events</title>
<para>
Say for example, that upon a successful login that some user statistics must be
updated. This would be done by writing an event observer for the
<literal>org.jboss.seam.security.loginSuccessful</literal> event, like this:
</para>
<programlisting role="JAVA"><![CDATA[ @In UserStats userStats;
@Observer("org.jboss.seam.security.loginSuccessful")
public void updateUserStats()
{
userStats.setLastLoginDate(new Date());
userStats.incrementLoginCount();
}]]></programlisting>
<para>
This observer method can be placed anywhere, even in the Authenticator component itself.
You can find more information about security-related events later in this chapter.
</para>
</sect3>
</sect2>
<sect2>
<title>Writing a login form</title>
<para>
The <literal>credentials</literal> component provides both <literal>username</literal> and <literal>password</literal>
properties, catering for the most common authentication scenario. These properties can be bound directly to the
username and password fields on a login form. Once these properties are set, calling
<literal>identity.login()</literal> will authenticate the user using the provided credentials.
Here's an example of a simple login form:
</para>
<programlisting role="XHTML"><![CDATA[<div>
<h:outputLabel for="name" value="Username"/>
<h:inputText id="name" value="#{credentials.username}"/>
</div>
<div>
<h:outputLabel for="password" value="Password"/>
<h:inputSecret id="password" value="#{credentials.password}"/>
</div>
<div>
<h:commandButton value="Login" action="#{identity.login}"/>
</div>]]></programlisting>
<para>
Similarly, logging out the user is done by calling <literal>#{identity.logout}</literal>. Calling this
action will clear the security state of the currently authenticated user, and invalidate the user's session.
</para>
</sect2>
<sect2>
<title>Configuration Summary</title>
<para>
So to sum up, there are the three easy steps to configure authentication:
</para>
<itemizedlist>
<listitem>
<para>
Configure an authentication method in <literal>components.xml</literal>.
</para>
</listitem>
<listitem>
<para>
Write an authentication method.
</para>
</listitem>
<listitem>
<para>
Write a login form so that the user can authenticate.
</para>
</listitem>
</itemizedlist>
</sect2>
<sect2>
<title>Remember Me</title>
<para>
Seam Security supports the same kind of "Remember Me" functionality that is commonly encountered in many
online web-based applications. It is actually supported in two different "flavours", or modes - the first
mode allows the username to be stored in the user's browser as a cookie, and leaves the entering of the
password up to the browser (many modern browsers are capable of remembering passwords).
</para>
<para>
The second mode supports the storing of a unique token in a cookie, and allows a user to authenticate
automatically upon returning to the site, without having to provide a password.
</para>
<warning>
<para>
Automatic client authentication with a persistent cookie stored on the client machine is dangerous.
While convenient for users, any cross-site scripting security hole in your website would have dramatically more
serious effects than usual. Without the authentication cookie, the only cookie to steal for an attacker with XSS
is the cookie of the current session of a user. This means the attack only works when the user has an open session -
which should be a short timespan. However, it is much more attractive and dangerous if an attacker has the possibility
to steal a persistent Remember Me cookie that allows him to login without authentication, at any time. Note that this
all depends on how well you protect your website against XSS attacks - it's up to you to make sure that your website
is 100% XSS safe - a non-trivial achievement for any website that allows user input to be rendered on a page.
</para>
<para>
Browser vendors recognized this issue and introduced a "Remember Passwords" feature - today almost all browsers support
this. Here, the browser remembers the login username and password for a particular website and domain, and fills out the
login form automatically when you don't have an active session with the website. If you as a website designer then offer
a convenient login keyboard shortcut, this approach is almost as convenient as a "Remember Me" cookie and much safer.
Some browsers (e.g. Safari on OS X) even store the login form data in the encrypted global operation system keychain.
Or, in a networked environment, the keychain can be transported with the user (between laptop and desktop for example),
while browser cookies are usually not synchronized.
</para>
<para>
To summarize: While everyone is doing it, persistent "Remember Me" cookies with automatic authentication are a bad
practice and should not be used. Cookies that "remember" only the users login name, and fill out the login form with
that username as a convenience, are not an issue.
</para>
</warning>
<para>
To enable the remember me feature for the default (safe, username only) mode, no special configuration is required.
In your login form, simply bind the remember me checkbox to <literal>rememberMe.enabled</literal>, like in the following
example:
</para>
<programlisting role="XHTML"><![CDATA[ <div>
<h:outputLabel for="name" value="User name"/>
<h:inputText id="name" value="#{credentials.username}"/>
</div>
<div>
<h:outputLabel for="password" value="Password"/>
<h:inputSecret id="password" value="#{credentials.password}" redisplay="true"/>
</div>
<div class="loginRow">
<h:outputLabel for="rememberMe" value="Remember me"/>
<h:selectBooleanCheckbox id="rememberMe" value="#{rememberMe.enabled}"/>
</div>]]></programlisting>
<sect3>
<title>Token-based Remember-me Authentication</title>
<para>
To use the automatic, token-based mode of the remember me feature, you must first configure a token store. The
most common scenario is to store these authentication tokens within a database (which Seam supports), however it
is possible to implement your own token store by implementing the <literal>org.jboss.seam.security.TokenStore</literal>
interface. This section will assume you will be using the provided <literal>JpaTokenStore</literal> implementation
to store authentication tokens inside a database table.
</para>
<para>
The first step is to create a new Entity which will contain the tokens. The following example shows a possible
structure that you may use:
</para>
<programlisting role="JAVA"><![CDATA[@Entity
public class AuthenticationToken implements Serializable {
private Integer tokenId;
private String username;
private String value;
@Id @GeneratedValue
public Integer getTokenId() {
return tokenId;
}
public void setTokenId(Integer tokenId) {
this.tokenId = tokenId;
}
@TokenUsername
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@TokenValue
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}]]></programlisting>
<para>
As you can see from this listing, a couple of special annotations, <literal>@TokenUsername</literal> and
<literal>@TokenValue</literal> are used to configure the username and token properties of the entity. These
annotations are required for the entity that will contain the authentication tokens.
</para>
<para>
The next step is to configure <literal>JpaTokenStore</literal> to use this entity bean to store and retrieve
authentication tokens. This is done in <literal>components.xml</literal> by specifying the <literal>token-class</literal>
attribute:
</para>
<programlisting role="XML"><![CDATA[
<security:jpa-token-store token-class="org.jboss.seam.example.seamspace.AuthenticationToken" />]]>
</programlisting>
<para>
Once this is done, the last thing to do is to configure the <literal>RememberMe</literal> component in
<literal>components.xml</literal> also. Its <literal>mode</literal> should be set to <literal>autoLogin</literal>:
</para>
<programlisting role="XML"><![CDATA[
<security:remember-me mode="autoLogin"/>]]>
</programlisting>
<para>
That is all that is required - automatic authentication will now occur for users revisiting your site (as long as they
check the "remember me" checkbox).
</para>
<para>
To ensure that users are automatically authenticated when returning to the site, the following section
should be placed in components.xml:
</para>
<programlisting role="XML">
<![CDATA[<event type="org.jboss.seam.security.notLoggedIn">
<action execute="#{redirect.captureCurrentView}"/>
<action execute="#{identity.tryLogin()}"/>
</event>
<event type="org.jboss.seam.security.loginSuccessful">
<action execute="#{redirect.returnToCapturedView}"/>
</event>]]></programlisting>
</sect3>
</sect2>
<sect2>
<title>Handling Security Exceptions</title>
<para>
To prevent users from receiving the default error page in response to a security error, it's recommended that
<literal>pages.xml</literal> is configured to redirect security errors to a more "pretty" page. The two
main types of exceptions thrown by the security API are:
</para>
<itemizedlist>
<listitem>
<para>
<literal>NotLoggedInException</literal> - This exception is thrown if the user attempts to access a
restricted action or page when they are not logged in.
</para>
</listitem>
<listitem>
<para>
<literal>AuthorizationException</literal> - This exception is only thrown if the user is already logged in,
and they have attempted to access a restricted action or page for which they do not have the necessary
privileges.
</para>
</listitem>
</itemizedlist>
<para>
In the case of a <literal>NotLoggedInException</literal>, it is recommended that the user is redirected to
either a login or registration page so that they can log in. For an <literal>AuthorizationException</literal>,
it may be useful to redirect the user to an error page. Here's an example of a <literal>pages.xml</literal>
file that redirects both of these security exceptions:
</para>
<programlisting role="XML"><![CDATA[<pages>
...
<exception class="org.jboss.seam.security.NotLoggedInException">
<redirect view-id="/login.xhtml">
<message>You must be logged in to perform this action</message>
</redirect>
</exception>
<exception class="org.jboss.seam.security.AuthorizationException">
<end-conversation/>
<redirect view-id="/security_error.xhtml">
<message>You do not have the necessary security privileges to perform this action.</message>
</redirect>
</exception>
</pages>]]></programlisting>
<para>
Most web applications require even more sophisticated handling of login redirection, so
Seam includes some special functionality for handling this problem.
</para>
</sect2>
<sect2>
<title>Login Redirection</title>
<para>
You can ask Seam to redirect the user to a login screen when an unauthenticated user tries
to access a particular view (or wildcarded view id) as follows:
</para>
<programlisting role="XML"><![CDATA[<pages login-view-id="/login.xhtml">
<page view-id="/members/*" login-required="true"/>
...
</pages>]]></programlisting>
<tip>
<para>
This is less of a blunt instrument than the exception handler shown above, but should
probably be used in conjunction with it.
</para>
</tip>
<para>
After the user logs in, we want to automatically send them back where they came from, so
they can retry the action that required logging in. If you add the following event listeners
to <literal>components.xml</literal>, attempts to access a restricted view while not logged
in will be remembered, so that upon the user successfully logging in they will be redirected
to the originally requested view, with any page parameters that existed in the original
request.
</para>
<programlisting role="XML"><![CDATA[<event type="org.jboss.seam.security.notLoggedIn">
<action execute="#{redirect.captureCurrentView}"/>
</event>
<event type="org.jboss.seam.security.postAuthenticate">
<action execute="#{redirect.returnToCapturedView}"/>
</event>]]></programlisting>
<para>
Note that login redirection is implemented as a conversation-scoped mechanism, so don't end
the conversation in your <literal>authenticate()</literal> method.
</para>
</sect2>
<sect2>
<title>HTTP Authentication</title>
<para>
Although not recommended for use unless absolutely necessary, Seam provides means for authenticating
using either HTTP Basic or HTTP Digest (RFC 2617) methods. To use either form of authentication,
the <literal>authentication-filter</literal> component must be enabled in components.xml:
</para>
<programlisting role="XML"><![CDATA[
<web:authentication-filter url-pattern="*.seam" auth-type="basic"/>
]]></programlisting>
<para>
To enable the filter for basic authentication, set <literal>auth-type</literal> to <literal>basic</literal>,
or for digest authentication, set it to <literal>digest</literal>. If using digest authentication, the
<literal>key</literal> and <literal>realm</literal> must also be set:
</para>
<programlisting role="XML"><![CDATA[
<web:authentication-filter url-pattern="*.seam" auth-type="digest" key="AA3JK34aSDlkj" realm="My App"/>
]]></programlisting>
<para>
The <literal>key</literal> can be any String value. The <literal>realm</literal> is the name of the
authentication realm that is presented to the user when they authenticate.
</para>
<sect3>
<title>Writing a Digest Authenticator</title>
<para>
If using digest authentication, your authenticator class should extend the abstract class
<literal>org.jboss.seam.security.digest.DigestAuthenticator</literal>, and use the
<literal>validatePassword()</literal> method to validate the user's plain text password
against the digest request. Here is an example:
</para>
<programlisting role="JAVA"><![CDATA[
public boolean authenticate()
{
try
{
User user = (User) entityManager.createQuery(
"from User where username = :username")
.setParameter("username", identity.getUsername())
.getSingleResult();
return validatePassword(user.getPassword());
}
catch (NoResultException ex)
{
return false;
}
}
]]></programlisting>
</sect3>
</sect2>
<sect2>
<title>Advanced Authentication Features</title>
<para>
This section explores some of the advanced features provided by the security API for addressing more complex
security requirements.
</para>
<sect3>
<title>Using your container's JAAS configuration</title>
<para>
If you would rather not use the simplified JAAS configuration provided by the Seam Security API, you may
instead delegate to the default system JAAS configuration by providing a <literal>jaas-config-name</literal>
property in <literal>components.xml</literal>. For example, if you are using JBoss AS and wish to use
the <literal>other</literal> policy (which uses the <literal>UsersRolesLoginModule</literal> login module
provided by JBoss AS), then the entry in <literal>components.xml</literal> would look like this:
</para>
<programlisting role="XML"><![CDATA[<security:identity jaas-config-name="other"/>]]></programlisting>
<para>
Please keep in mind that doing this does not mean that your user will be authenticated in whichever
container your Seam application is deployed in. It merely instructs Seam Security to authenticate
itself using the configured JAAS security policy.
</para>
</sect3>
</sect2>
</sect1>
<sect1>
<title>Identity Management</title>
<para>
Identity Management provides a standard API for the management of a Seam application's users and roles,
regardless of which identity store (database, LDAP, etc) is used on the backend. At the center
of the Identity Management API is the <literal>identityManager</literal> component, which provides
all the methods for creating, modifying and deleting users, granting and revoking roles, changing passwords,
enabling and disabling user accounts, authenticating users and listing users and roles.
</para>
<para>
Before it may be used, the <literal>identityManager</literal> must first be configured with one or more
<literal>IdentityStore</literal>s. These components do the actual work of interacting with the backend
security provider, whether it be a database, LDAP server, or something else.
</para>
<mediaobject>
<imageobject role="fo">
<imagedata fileref="images/security-identitymanager.png" align="center"/>
</imageobject>
<imageobject role="html">
<imagedata fileref="images/security-identitymanager.png" align="center"/>
</imageobject>
</mediaobject>
<sect2>
<title>Configuring IdentityManager</title>
<para>
The <literal>identityManager</literal> component allows for separate identity stores to be configured
for authentication and authorization operations. This means that it is possible for users to
be authenticated against one identity store, for example an LDAP directory, yet have their roles
loaded from another identity store, such as a relational database.
</para>
<para>
Seam provides two <literal>IdentityStore</literal> implementations out of the box;
<literal>JpaIdentityStore</literal> uses a relational database to store user and role information,
and is the default identity store that is used if nothing is explicitly configured in the
<literal>identityManager</literal> component. The other implementation that is provided is
<literal>LdapIdentityStore</literal>, which uses an LDAP directory to store users and roles.
</para>
<para>
There are two configurable properties for the <literal>identityManager</literal> component -
<literal>identityStore</literal> and <literal>roleIdentityStore</literal>. The value for these
properties must be an EL expression referring to a Seam component implementing the
<literal>IdentityStore</literal> interface. As already mentioned,
if left unconfigured then <literal>JpaIdentityStore</literal> will be assumed by default. If
only the <literal>identityStore</literal> property is configured, then the same value will be used for
<literal>roleIdentityStore</literal> also. For example, the following entry in
<literal>components.xml</literal> will configure <literal>identityManager</literal> to use
an <literal>LdapIdentityStore</literal> for both user-related and role-related operations:
</para>
<programlisting role="XML"><![CDATA[
<security:identity-manager identity-store="#{ldapIdentityStore}"/>
]]></programlisting>
<para>
The following example configures <literal>identityManager</literal> to use an <literal>LdapIdentityStore</literal>
for user-related operations, and <literal>JpaIdentityStore</literal> for role-related operations:
</para>
<programlisting role="XML"><![CDATA[
<security:identity-manager
identity-store="#{ldapIdentityStore}"
role-identity-store="#{jpaIdentityStore}"/>
]]></programlisting>
<para>
The following sections explain both of these identity store implementations in greater detail.
</para>
</sect2>
<sect2>
<title>JpaIdentityStore</title>
<para>
This identity store allows for users and roles to be stored inside a relational database. It is designed
to be as unrestrictive as possible in regards to database schema design, allowing a great deal of
flexibility in the underlying table structure. This is achieved through the use of a set of special
annotations, allowing entity beans to be configured to store user and role records.
</para>
<sect3>
<title>Configuring JpaIdentityStore</title>
<para>
<literal>JpaIdentityStore</literal> requires that both the <literal>user-class</literal> and
<literal>role-class</literal> properties are configured. These properties should refer to the
entity classes that are to be used to store both user and role records, respectively. The following
example shows the configuration from <literal>components.xml</literal> in the SeamSpace example:
</para>
<programlisting role="XML"><![CDATA[
<security:jpa-identity-store
user-class="org.jboss.seam.example.seamspace.MemberAccount"
role-class="org.jboss.seam.example.seamspace.MemberRole"/>
]]></programlisting>
</sect3>
<sect3>
<title>Configuring the Entities</title>
<para>
As already mentioned, a set of special annotations are used to configure entity beans for storing
users and roles. The following table lists each of the annotations, and their descriptions.
</para>
<table>
<title>User Entity Annotations</title>
<tgroup cols="3">
<colspec colnum="1" colwidth="1*" />
<colspec colnum="2" colwidth="1*" />
<colspec colnum="3" colwidth="3*" />
<thead>
<row>
<entry align="center">
<para>Annotation</para>
</entry>
<entry align="center">
<para>Status</para>
</entry>
<entry align="center">
<para>Description</para>
</entry>
</row>
</thead>
<tbody>
<row>
<entry>
<para>
<literal>@UserPrincipal</literal>
</para>
</entry>
<entry>
<para>Required</para>
</entry>
<entry>
<para>
This annotation marks the field or method containing the user's username.
</para>
</entry>
</row>
<row>
<entry>
<para>
<literal>@UserPassword</literal>
</para>
</entry>
<entry>
<para>Required</para>
</entry>
<entry>
<para>
This annotation marks the field or method containing the user's password. It allows a <literal>hash</literal>
algorithm to be specified for password hashing. Possible values for <literal>hash</literal> are
<literal>md5</literal>, <literal>sha</literal> and <literal>none</literal>. E.g:
</para>
<programlisting role="JAVA"><![CDATA[@UserPassword (hash="md5")
public String getPasswordHash() {
return passwordHash;
}]]></programlisting>
<para>
If an application requires a hash algorithm that isn't supported natively by Seam, it
is possible to extend the <literal>PasswordHash</literal> component to implement other
hashing algorithms.
</para>
</entry>
</row>
<row>
<entry>
<para>
<literal>@UserFirstName</literal>
</para>
</entry>
<entry>
<para>Optional</para>
</entry>
<entry>
<para>
This annotation marks the field or method containing the user's first name.
</para>
</entry>
</row>
<row>
<entry>
<para>
<literal>@UserLastName</literal>
</para>
</entry>
<entry>
<para>Optional</para>
</entry>
<entry>
<para>
This annotation marks the field or method containing the user's last name.
</para>
</entry>
</row>
<row>
<entry>
<para>
<literal>@UserEnabled</literal>
</para>
</entry>
<entry>
<para>Optional</para>
</entry>
<entry>
<para>
This annotation marks the field or method containing the enabled status of the user. This should be a boolean
property, and if not present then all user accounts are assumed to be enabled.
</para>
</entry>
</row>
<row>
<entry>
<para>
<literal>@UserRoles</literal>
</para>
</entry>
<entry>
<para>Required</para>
</entry>
<entry>
<para>
This annotation marks the field or method containing the roles of the user. This property will be described in
more detail further down.
</para>
</entry>
</row>
</tbody>
</tgroup>
</table>
<table>
<title>Role Entity Annotations</title>
<tgroup cols="3">
<colspec colnum="1" colwidth="2*" />
<colspec colnum="2" colwidth="1*" />
<colspec colnum="3" colwidth="3*" />
<thead>
<row>
<entry align="center">
<para>Annotation</para>
</entry>
<entry align="center">
<para>Status</para>
</entry>
<entry align="center">
<para>Description</para>
</entry>
</row>
</thead>
<tbody>
<row>
<entry>
<para>
<literal>@RoleName</literal>
</para>
</entry>
<entry>
<para>Required</para>
</entry>
<entry>
<para>
This annotation marks the field or method containing the name of the role.
</para>
</entry>
</row>
<row>
<entry>
<para>
<literal>@RoleGroups</literal>
</para>
</entry>
<entry>
<para>Optional</para>
</entry>
<entry>
<para>
This annotation marks the field or method containing the group memberships of the role.
</para>
</entry>
</row>
<row>
<entry>
<para>
<literal>@RoleConditional</literal>
</para>
</entry>
<entry>
<para>Optional</para>
</entry>
<entry>
<para>
This annotation marks the field or method indicating whether the role is conditional or not.
Conditional roles are explained later in this chapter.
</para>
</entry>
</row>
</tbody>