blob: de9c940baa5847ca992be3f0fe65917ac5f439eb [file] [log] [blame]
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>Quickstart.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">Apache Shiro :: Samples :: Quick Start</a> &gt; <a href="index.source.html" class="el_package">default</a> &gt; <span class="el_source">Quickstart.java</span></div><h1>Quickstart.java</h1><pre class="source lang-java linenums">/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* &quot;License&quot;); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* &quot;AS IS&quot; BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Simple Quickstart application showing how to use Shiro's API.
*
* @since 0.9 RC2
*/
<span class="nc" id="L36">public class Quickstart {</span>
<span class="nc" id="L38"> private static final transient Logger log = LoggerFactory.getLogger(Quickstart.class);</span>
public static void main(String[] args) {
// The easiest way to create a Shiro SecurityManager with configured
// realms, users, roles and permissions is to use the simple INI config.
// We'll do that by using a factory that can ingest a .ini file and
// return a SecurityManager instance:
// Use the shiro.ini file at the root of the classpath
// (file: and url: prefixes load from files and urls respectively):
<span class="nc" id="L50"> Factory&lt;SecurityManager&gt; factory = new IniSecurityManagerFactory(&quot;classpath:shiro.ini&quot;);</span>
<span class="nc" id="L51"> SecurityManager securityManager = factory.getInstance();</span>
// for this simple example quickstart, make the SecurityManager
// accessible as a JVM singleton. Most applications wouldn't do this
// and instead rely on their container configuration or web.xml for
// webapps. That is outside the scope of this simple quickstart, so
// we'll just do the bare minimum so you can continue to get a feel
// for things.
<span class="nc" id="L59"> SecurityUtils.setSecurityManager(securityManager);</span>
// Now that a simple Shiro environment is set up, let's see what you can do:
// get the currently executing user:
<span class="nc" id="L64"> Subject currentUser = SecurityUtils.getSubject();</span>
// Do some stuff with a Session (no need for a web or EJB container!!!)
<span class="nc" id="L67"> Session session = currentUser.getSession();</span>
<span class="nc" id="L68"> session.setAttribute(&quot;someKey&quot;, &quot;aValue&quot;);</span>
<span class="nc" id="L69"> String value = (String) session.getAttribute(&quot;someKey&quot;);</span>
<span class="nc bnc" id="L70" title="All 2 branches missed."> if (value.equals(&quot;aValue&quot;)) {</span>
<span class="nc" id="L71"> log.info(&quot;Retrieved the correct value! [&quot; + value + &quot;]&quot;);</span>
}
// let's login the current user so we can check against roles and permissions:
<span class="nc bnc" id="L75" title="All 2 branches missed."> if (!currentUser.isAuthenticated()) {</span>
<span class="nc" id="L76"> UsernamePasswordToken token = new UsernamePasswordToken(&quot;lonestarr&quot;, &quot;vespa&quot;);</span>
<span class="nc" id="L77"> token.setRememberMe(true);</span>
try {
<span class="nc" id="L79"> currentUser.login(token);</span>
<span class="nc" id="L80"> } catch (UnknownAccountException uae) {</span>
<span class="nc" id="L81"> log.info(&quot;There is no user with username of &quot; + token.getPrincipal());</span>
<span class="nc" id="L82"> } catch (IncorrectCredentialsException ice) {</span>
<span class="nc" id="L83"> log.info(&quot;Password for account &quot; + token.getPrincipal() + &quot; was incorrect!&quot;);</span>
<span class="nc" id="L84"> } catch (LockedAccountException lae) {</span>
<span class="nc" id="L85"> log.info(&quot;The account for username &quot; + token.getPrincipal() + &quot; is locked. &quot; +</span>
&quot;Please contact your administrator to unlock it.&quot;);
}
// ... catch more exceptions here (maybe custom ones specific to your application?
<span class="nc" id="L89"> catch (AuthenticationException ae) {</span>
//unexpected condition? error?
<span class="nc" id="L91"> }</span>
}
//say who they are:
//print their identifying principal (in this case, a username):
<span class="nc" id="L96"> log.info(&quot;User [&quot; + currentUser.getPrincipal() + &quot;] logged in successfully.&quot;);</span>
//test a role:
<span class="nc bnc" id="L99" title="All 2 branches missed."> if (currentUser.hasRole(&quot;schwartz&quot;)) {</span>
<span class="nc" id="L100"> log.info(&quot;May the Schwartz be with you!&quot;);</span>
} else {
<span class="nc" id="L102"> log.info(&quot;Hello, mere mortal.&quot;);</span>
}
//test a typed permission (not instance-level)
<span class="nc bnc" id="L106" title="All 2 branches missed."> if (currentUser.isPermitted(&quot;lightsaber:weild&quot;)) {</span>
<span class="nc" id="L107"> log.info(&quot;You may use a lightsaber ring. Use it wisely.&quot;);</span>
} else {
<span class="nc" id="L109"> log.info(&quot;Sorry, lightsaber rings are for schwartz masters only.&quot;);</span>
}
//a (very powerful) Instance Level permission:
<span class="nc bnc" id="L113" title="All 2 branches missed."> if (currentUser.isPermitted(&quot;winnebago:drive:eagle5&quot;)) {</span>
<span class="nc" id="L114"> log.info(&quot;You are permitted to 'drive' the winnebago with license plate (id) 'eagle5'. &quot; +</span>
&quot;Here are the keys - have fun!&quot;);
} else {
<span class="nc" id="L117"> log.info(&quot;Sorry, you aren't allowed to drive the 'eagle5' winnebago!&quot;);</span>
}
//all done - log out!
<span class="nc" id="L121"> currentUser.logout();</span>
<span class="nc" id="L123"> System.exit(0);</span>
<span class="nc" id="L124"> }</span>
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.eclemma.org/jacoco">JaCoCo</a> 0.7.7.201606060606</span></div></body></html>