blob: 1f3c7197f2175d818b19acfbce7e3badd85e62ef [file] [log] [blame]
<h1><a name="SecurityManager-UnderstandingtheSecurityManagerinApacheShiro"></a>Understanding the SecurityManager in Apache Shiro</h1>
<p>The <a class="external-link" href="static/current/apidocs/org/apache/shiro/mgt/SecurityManager.html">SecurityManager</a> lies at the heart of Shiro's architecture. While the <a href="subject.html" title="Subject">Subject</a> represents security functionality and state for a <em>single</em> application user, the <tt>SecurityManager</tt> performs security operations and manages state for <em>all</em> application users.</p>
<p>Because Shiro's API encourages a <tt>Subject</tt>-centric programming approach, most application developers will rarely, if ever, interact with the <tt>SecurityManager</tt> directly (framework developers however might sometimes find it useful). Even so, it is still important to know how the <tt>SecurityManager</tt> functions, especially when configuring one for an application.</p>
<h2><a name="SecurityManager-Design"></a>Design</h2>
<p>As stated previously, the application's <tt>SecurityManager</tt> performs security operations and manages state for <em>all</em> application users. In Shiro's default <tt>SecurityManager</tt> implementations, this includes:</p>
<ul>
<li>Authentication</li>
<li>Authorization</li>
<li>Session Management</li>
<li>Cache Management</li>
<li><a href="realm.html" title="Realm">Realm</a> coordination</li>
<li>Event propagation</li>
<li>"Remember Me" Services</li>
<li>Subject creation</li>
<li>Logout<br clear="none">and more.</li></ul>
<p>But this is a lot of functionality to try to manage in a single component. And, making these things flexible and customizable would be very difficult if everything were lumped into a single implementation class. </p>
<p>To simplify configuration and enable flexible configuration/pluggability, Shiro's implementations are all highly modular in design - so modular in fact, that the SecurityManager implementation (and its class-hierarchy) does not do much at all. Instead, the <tt>SecurityManager</tt> implementations mostly act as a lightweight 'container' component, delegating almost all behavior to nested/wrapped components.</p>
<h3><a name="SecurityManager-Modularity"></a>Modularity</h3>
<p>To simplify the <tt>SecurityManager</tt> implementation complexity and allow for pluggable behavior, the Shiro <tt>SecurityManager</tt> implementations delegate almost all logic to a nested set of modular components that actually perform the necessary functionality. While the components actually execute the logic, the <tt>SecurityManager</tt> implementation knows how and when to coordinate the components for the correct behavior.</p>
<p>The nested components that the <tt>SecurityManager</tt> coordinates and delegates to are:</p>
<ul>
<li>Authenticator (<tt>org.apache.shiro.authc.Authenticator</tt>)</li>
<li>Authorizer (<tt>org.apache.shiro.authz.Authorizer</tt>)</li>
<li>SessionManager (<tt>org.apache.shiro.session.mgt.SessionManager</tt>)</li>
<li><a href="cachemanager.html" title="CacheManager">CacheManager</a> (<tt>org.apache.shiro.cache.CacheManager</tt>)</li>
<li>RememberMeManager (<tt>org.apache.shiro.mgt.RememberMeManager</tt>)</li>
<li>SubjectFactory(<tt>org.apache.shiro.mgt.SubjectFactory</tt>)</li></ul>
<p>The <tt>SecurityManager</tt> implementations and are also JavaBeans compatible, which allows you (or a configuration mechanism) to easily customize the pluggable components via standard JavaBeans accessor/mutator methods (get*/set*). This means the Shiro's architectural modularity can translate into very easy configuration for custom behavior.</p>
<div class="panelMacro"><table class="tipMacro"><colgroup span="1"><col span="1" width="24"><col span="1"></colgroup><tr><td colspan="1" rowspan="1" valign="top"><img align="middle" src="https://cwiki.apache.org/confluence/images/icons/emoticons/check.gif" width="16" height="16" alt="" border="0"></td><td colspan="1" rowspan="1"><b>Easy Configuration</b><br clear="none">Because of JavaBeans compatibility, it is very easy to configure the <tt>SecurityManager</tt> with custom components via any mechanism that supports JavaBeans-style configuration, such as <a href="spring.html" title="Spring">Spring</a>, Guice, JBoss, etc.</td></tr></table></div>
<h3><a name="SecurityManager-ProgrammaticConfiguration"></a>Programmatic Configuration</h3>
<p>The absolute simplest way to create a SecurityManager and make it available to the application is to create a <tt>org.apache.shiro.mgt.DefaultSecurityManager</tt> and wire it up in code:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
Realm realm = <span class="code-comment">//instantiate or acquire a Realm instance. We'll discuss Realms later.
</span><span class="code-object">SecurityManager</span> securityManager = <span class="code-keyword">new</span> DefaultSecurityManager(realm);
<span class="code-comment">//Make the <span class="code-object">SecurityManager</span> instance available to the entire application:
</span>SecurityUtils.setSecurityManager(securityManager);
</pre>
</div></div>
<p>Surprisingly, after only 3 lines of code, you now have a fully functional Shiro environment suitable for most applications. How easy was that!?</p>
<p>You could additionally call any of the <tt>SecurityManager</tt> instance's setter methods with custom implementations of the nested components listed above to fully customize its behavior.</p>
<p>But, as simple as programmatic customization is, these 3 lines of code do not represent the ideal configuration for most real world applications. There are a few reasons why programmatic configuration may not be suitable for your application:</p>
<ol><li>It requires you to know about and instantiate a direct implementation. It would be nicer if you didn't have to know about concrete implementations and where to find them.</li><li>The <tt>SecurityUtils.setSecurityManager</tt> method call makes the instantiated <tt>SecurityManager</tt> instance a VM static singleton, which, while fine for many applications, would cause problems if more than one Shiro-enabled application was running on the same JVM. It could be better if the instance was an application singleton, but not a static memory reference.</li><li>It requires you to recompile your application every time you want to make a Shiro configuration change.</li></ol>
<p>Most applications instead benefit from text-based configuration that could be modified independently of source code and even make things easier to understand for those not intimately familiar with Shiro's APIs. </p>
<h3><a name="SecurityManager-TextConfiguration"></a>Text Configuration</h3>
<p>Shiro provides a simple INI-based <a href="configuration.html" title="Configuration">configuration</a> that can be used out of the box, but any other JavaBeans-compatible mechanism can be used as well. For example, Shiro has excellent <a href="spring.html" title="Spring">Spring support</a> too. Other similar frameworks (Guice, JBoss, etc) could also be used.</p>
<h2><a name="SecurityManager-Lendahandwithdocumentation"></a>Lend a hand with documentation </h2>
<p>While we hope this documentation helps you with the work you're doing with Apache Shiro, the community is improving and expanding the documentation all the time. If you'd like to help the Shiro project, please consider corrected, expanding, or adding documentation where you see a need. Every little bit of help you provide expands the community and in turn improves Shiro. </p>
<p>The easiest way to contribute your documentation is to send it to the <a class="external-link" href="http://shiro-user.582556.n2.nabble.com/" rel="nofollow">User Forum</a> or the <a href="mailing-lists.html" title="Mailing Lists">User Mailing List</a>.</p>