| <?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>QuickstartGuice.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 Guice</a> > <a href="index.source.html" class="el_package">default</a> > <span class="el_source">QuickstartGuice.java</span></div><h1>QuickstartGuice.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 |
| * "License"); 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 |
| * "AS IS" 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 com.google.inject.Guice; |
| import com.google.inject.Injector; |
| import org.apache.shiro.SecurityUtils; |
| import org.apache.shiro.authc.*; |
| import org.apache.shiro.mgt.SecurityManager; |
| import org.apache.shiro.session.Session; |
| import org.apache.shiro.subject.Subject; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| /** |
| * Simple Quickstart application showing how to use Shiro's API with Guice integration. |
| * |
| * @since 0.9 RC2 |
| */ |
| <span class="nc" id="L35">public class QuickstartGuice {</span> |
| |
| <span class="nc" id="L37"> private static final transient Logger log = LoggerFactory.getLogger(QuickstartGuice.class);</span> |
| |
| |
| public static void main(String[] args) { |
| |
| // We will utilize standard Guice bootstrapping to create a Shiro SecurityManager. |
| <span class="nc" id="L43"> Injector injector = Guice.createInjector(new QuickstartShiroModule());</span> |
| <span class="nc" id="L44"> SecurityManager securityManager = injector.getInstance(SecurityManager.class);</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="L52"> 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="L57"> Subject currentUser = SecurityUtils.getSubject();</span> |
| |
| // Do some stuff with a Session (no need for a web or EJB container!!!) |
| <span class="nc" id="L60"> Session session = currentUser.getSession();</span> |
| <span class="nc" id="L61"> session.setAttribute("someKey", "aValue");</span> |
| <span class="nc" id="L62"> String value = (String) session.getAttribute("someKey");</span> |
| <span class="nc bnc" id="L63" title="All 2 branches missed."> if (value.equals("aValue")) {</span> |
| <span class="nc" id="L64"> log.info("Retrieved the correct value! [" + value + "]");</span> |
| } |
| |
| // let's login the current user so we can check against roles and permissions: |
| <span class="nc bnc" id="L68" title="All 2 branches missed."> if (!currentUser.isAuthenticated()) {</span> |
| <span class="nc" id="L69"> UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa");</span> |
| <span class="nc" id="L70"> token.setRememberMe(true);</span> |
| try { |
| <span class="nc" id="L72"> currentUser.login(token);</span> |
| <span class="nc" id="L73"> } catch (UnknownAccountException uae) {</span> |
| <span class="nc" id="L74"> log.info("There is no user with username of " + token.getPrincipal());</span> |
| <span class="nc" id="L75"> } catch (IncorrectCredentialsException ice) {</span> |
| <span class="nc" id="L76"> log.info("Password for account " + token.getPrincipal() + " was incorrect!");</span> |
| <span class="nc" id="L77"> } catch (LockedAccountException lae) {</span> |
| <span class="nc" id="L78"> log.info("The account for username " + token.getPrincipal() + " is locked. " +</span> |
| "Please contact your administrator to unlock it."); |
| } |
| // ... catch more exceptions here (maybe custom ones specific to your application? |
| <span class="nc" id="L82"> catch (AuthenticationException ae) {</span> |
| //unexpected condition? error? |
| <span class="nc" id="L84"> }</span> |
| } |
| |
| //say who they are: |
| //print their identifying principal (in this case, a username): |
| <span class="nc" id="L89"> log.info("User [" + currentUser.getPrincipal() + "] logged in successfully.");</span> |
| |
| //test a role: |
| <span class="nc bnc" id="L92" title="All 2 branches missed."> if (currentUser.hasRole("schwartz")) {</span> |
| <span class="nc" id="L93"> log.info("May the Schwartz be with you!");</span> |
| } else { |
| <span class="nc" id="L95"> log.info("Hello, mere mortal.");</span> |
| } |
| |
| //test a typed permission (not instance-level) |
| <span class="nc bnc" id="L99" title="All 2 branches missed."> if (currentUser.isPermitted("lightsaber:weild")) {</span> |
| <span class="nc" id="L100"> log.info("You may use a lightsaber ring. Use it wisely.");</span> |
| } else { |
| <span class="nc" id="L102"> log.info("Sorry, lightsaber rings are for schwartz masters only.");</span> |
| } |
| |
| //a (very powerful) Instance Level permission: |
| <span class="nc bnc" id="L106" title="All 2 branches missed."> if (currentUser.isPermitted("winnebago:drive:eagle5")) {</span> |
| <span class="nc" id="L107"> log.info("You are permitted to 'drive' the winnebago with license plate (id) 'eagle5'. " +</span> |
| "Here are the keys - have fun!"); |
| } else { |
| <span class="nc" id="L110"> log.info("Sorry, you aren't allowed to drive the 'eagle5' winnebago!");</span> |
| } |
| |
| //all done - log out! |
| <span class="nc" id="L114"> currentUser.logout();</span> |
| |
| <span class="nc" id="L116"> System.exit(0);</span> |
| <span class="nc" id="L117"> }</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> |