Package org.apache.shiro.subject
Class Subject.Builder
- java.lang.Object
-
- org.apache.shiro.subject.Subject.Builder
-
- Enclosing interface:
- Subject
public static class Subject.Builder extends Object
Builder design pattern implementation for creatingSubject
instances in a simplified way without requiring knowledge of Shiro's construction techniques. NOTE: This is provided for framework development support only and should typically never be used by application developers.Subject
instances should generally be acquired by usingSecurityUtils.
getSubject()
Usage
The simplest usage of this builder is to construct an anonymous, session-lessSubject
instance:Subject subject = new Subject.
The default, no-argBuilder
().buildSubject()
;Subject.Builder()
constructor shown above will use the application's currently accessibleSecurityManager
viaSecurityUtils.
. You may also specify the exactgetSecurityManager()
SecurityManager
instance to be used by the additionalSubject.
constructor if desired. All other methods may be called before theBuilder(securityManager)
buildSubject()
method to provide context on how to construct theSubject
instance. For example, if you have a session id and want to acquire theSubject
that 'owns' that session (assuming the session exists and is not expired):Subject subject = new Subject.Builder().sessionId(sessionId).buildSubject();
Similarly, if you want aSubject
instance reflecting a certain identity:PrincipalCollection principals = new SimplePrincipalCollection("username", yourRealmName); Subject subject = new Subject.Builder().principals(principals).build();
Note* that the returnedSubject
instance is not automatically bound to the application (thread) for further use. That is,SecurityUtils
.getSubject()
will not automatically return the same instance as what is returned by the builder. It is up to the framework developer to bind the builtSubject
for continued use if desired.- Since:
- 1.0
-
-
Constructor Summary
Constructors Constructor Description Builder()
Constructs a newSubject.Builder
instance, using theSecurityManager
instance available to the calling code as determined by a call toSecurityUtils.getSecurityManager()
to build theSubject
instance.Builder(SecurityManager securityManager)
Constructs a newSubject.Builder
instance which will use the specifiedSecurityManager
when building theSubject
instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Subject.Builder
authenticated(boolean authenticated)
Ensures theSubject
being built will be consideredauthenticated
.Subject
buildSubject()
Creates and returns a newSubject
instance reflecting the cumulative state acquired by the other methods in this class.Subject.Builder
contextAttribute(String attributeKey, Object attributeValue)
Allows custom attributes to be added to the underlying contextMap
used to construct theSubject
instance.protected SubjectContext
getSubjectContext()
Returns the backing context used to build theSubject
instance, available to subclasses since thecontext
class attribute is marked asprivate
.Subject.Builder
host(String host)
Ensures theSubject
being built will reflect the specified host name or IP as its originating location.protected SubjectContext
newSubjectContextInstance()
Creates a newSubjectContext
instance to be used to populate with subject contextual data that will then be sent to theSecurityManager
to create a newSubject
instance.Subject.Builder
principals(PrincipalCollection principals)
Ensures theSubject
being built will reflect the specified principals (aka identity).Subject.Builder
session(Session session)
Ensures theSubject
being built will use the specifiedSession
instance.Subject.Builder
sessionCreationEnabled(boolean enabled)
Configures whether or not the created Subject instance can create a newSession
if one does not already exist.Subject.Builder
sessionId(Serializable sessionId)
-
-
-
Constructor Detail
-
Builder
public Builder()
Constructs a newSubject.Builder
instance, using theSecurityManager
instance available to the calling code as determined by a call toSecurityUtils.getSecurityManager()
to build theSubject
instance.
-
Builder
public Builder(SecurityManager securityManager)
Constructs a newSubject.Builder
instance which will use the specifiedSecurityManager
when building theSubject
instance.- Parameters:
securityManager
- theSecurityManager
to use when building theSubject
instance.
-
-
Method Detail
-
newSubjectContextInstance
protected SubjectContext newSubjectContextInstance()
Creates a newSubjectContext
instance to be used to populate with subject contextual data that will then be sent to theSecurityManager
to create a newSubject
instance.- Returns:
- a new
SubjectContext
instance
-
getSubjectContext
protected SubjectContext getSubjectContext()
Returns the backing context used to build theSubject
instance, available to subclasses since thecontext
class attribute is marked asprivate
.- Returns:
- the backing context used to build the
Subject
instance, available to subclasses.
-
sessionId
public Subject.Builder sessionId(Serializable sessionId)
Enables building aSubject
instance that owns theSession
with the specifiedsessionId
. Usually when specifying asessionId
, no otherBuilder
methods would be specified because everything else (principals, inet address, etc) can usually be reconstructed based on the referenced session alone. In other words, this is almost always sufficient:new Subject.Builder().sessionId(sessionId).buildSubject();
Although simple in concept, this method provides very powerful functionality previously absent in almost all Java environments: The ability to reference aSubject
and their server-side session across clients of different mediums such as web applications, Java applets, standalone C# clients over XML-RPC and/or SOAP, and many others. This is a huge benefit in heterogeneous enterprise applications. To maintain session integrity across client mediums, thesessionId
must be transmitted to all client mediums securely (e.g. over SSL) to prevent man-in-the-middle attacks. This is nothing new - all web applications are susceptible to the same problem when transmittingCookie
s or when using URL rewriting. As long as thesessionId
is transmitted securely, session integrity can be maintained.- Parameters:
sessionId
- the id of the session that backs the desired Subject being acquired.- Returns:
- this
Builder
instance for method chaining.
-
host
public Subject.Builder host(String host)
Ensures theSubject
being built will reflect the specified host name or IP as its originating location.- Parameters:
host
- the host name or IP address to use as theSubject
's originating location.- Returns:
- this
Builder
instance for method chaining.
-
session
public Subject.Builder session(Session session)
Ensures theSubject
being built will use the specifiedSession
instance. Note that it is more common to use thesessionId
builder method rather than having to construct aSession
instance for this method.- Parameters:
session
- the session to use as theSubject
'sSession
- Returns:
- this
Builder
instance for method chaining.
-
principals
public Subject.Builder principals(PrincipalCollection principals)
Ensures theSubject
being built will reflect the specified principals (aka identity). For example, if your application's unique identifier for users is aString
username, and you wanted to create aSubject
instance that reflected a user whose username is 'jsmith
', and you knew the Realm that could acquirejsmith
's principals based on the username was named "myRealm
", you might create the 'jsmith
Subject
instance this way:PrincipalCollection identity = new
Similarly, if your application's unique identifier for users is aSimplePrincipalCollection
("jsmith", "myRealm"); Subject jsmith = new Subject.Builder().principals(identity).buildSubject();long
value (such as might be used as a primary key in a relational database) and you were using aJDBC
Realm
named, (unimaginatively) "jdbcRealm", you might create the Subject instance this way:long userId = //get user ID from somewhere PrincipalCollection userIdentity = new
SimplePrincipalCollection
(userId, "jdbcRealm"); Subject user = new Subject.Builder().principals(identity).buildSubject();- Parameters:
principals
- the principals to use as theSubject
's identity.- Returns:
- this
Builder
instance for method chaining.
-
sessionCreationEnabled
public Subject.Builder sessionCreationEnabled(boolean enabled)
Configures whether or not the created Subject instance can create a newSession
if one does not already exist. If set tofalse
, any application calls tosubject.getSession()
orsubject.getSession(true))
will result in a SessionException. This setting istrue
by default, as most applications find value in sessions.- Parameters:
enabled
- whether or not the created Subject instance can create a newSession
if one does not already exist.- Returns:
- this
Builder
instance for method chaining. - Since:
- 1.2
-
authenticated
public Subject.Builder authenticated(boolean authenticated)
Ensures theSubject
being built will be consideredauthenticated
. Per theisAuthenticated()
JavaDoc, be careful when specifyingtrue
- you should know what you are doing and have a good reason for ignoring Shiro's default authentication state mechanisms.- Parameters:
authenticated
- whether or not the builtSubject
will be considered authenticated.- Returns:
- this
Builder
instance for method chaining. - See Also:
Subject.isAuthenticated()
-
contextAttribute
public Subject.Builder contextAttribute(String attributeKey, Object attributeValue)
Allows custom attributes to be added to the underlying contextMap
used to construct theSubject
instance. Anull
key throws anIllegalArgumentException
. Anull
value effectively removes any previously stored attribute under the given key from the context map. *NOTE*: This method is only useful when configuring Shiro with a customSubjectFactory
implementation. This method allows end-users to append additional data to the context map which theSubjectFactory
implementation can use when building custom Subject instances. As such, this method is only useful when a customSubjectFactory
implementation has been configured.- Parameters:
attributeKey
- the key under which the corresponding value will be stored in the contextMap
.attributeValue
- the value to store in the context map under the specifiedattributeKey
.- Returns:
- this
Builder
instance for method chaining. - Throws:
IllegalArgumentException
- if theattributeKey
isnull
.- See Also:
SubjectFactory.createSubject(SubjectContext)
-
buildSubject
public Subject buildSubject()
Creates and returns a newSubject
instance reflecting the cumulative state acquired by the other methods in this class. ThisBuilder
instance will still retain the underlying state after this method is called - it will not clear it; repeated calls to this method will return multipleSubject
instances, all reflecting the exact same state. If a new (different)Subject
is to be constructed, a newBuilder
instance must be created. Note that the returnedSubject
instance is not automatically bound to the application (thread) for further use. That is,SecurityUtils
.getSubject()
will not automatically return the same instance as what is returned by the builder. It is up to the framework developer to bind the returnedSubject
for continued use if desired.- Returns:
- a new
Subject
instance reflecting the cumulative state acquired by the other methods in this class.
-
-