1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 import com.google.inject.Guice;
21 import com.google.inject.Injector;
22 import org.apache.shiro.SecurityUtils;
23 import org.apache.shiro.authc.*;
24 import org.apache.shiro.mgt.SecurityManager;
25 import org.apache.shiro.session.Session;
26 import org.apache.shiro.subject.Subject;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31 * Simple Quickstart application showing how to use Shiro's API with Guice integration.
32 *
33 * @since 0.9 RC2
34 */
35 public class QuickstartGuice {
36
37 private static final transient Logger log = LoggerFactory.getLogger(QuickstartGuice.class);
38
39
40 public static void main(String[] args) {
41
42 // We will utilize standard Guice bootstrapping to create a Shiro SecurityManager.
43 Injector injector = Guice.createInjector(new QuickstartShiroModule());
44 SecurityManager securityManager = injector.getInstance(SecurityManager.class);
45
46 // for this simple example quickstart, make the SecurityManager
47 // accessible as a JVM singleton. Most applications wouldn't do this
48 // and instead rely on their container configuration or web.xml for
49 // webapps. That is outside the scope of this simple quickstart, so
50 // we'll just do the bare minimum so you can continue to get a feel
51 // for things.
52 SecurityUtils.setSecurityManager(securityManager);
53
54 // Now that a simple Shiro environment is set up, let's see what you can do:
55
56 // get the currently executing user:
57 Subject currentUser = SecurityUtils.getSubject();
58
59 // Do some stuff with a Session (no need for a web or EJB container!!!)
60 Session session = currentUser.getSession();
61 session.setAttribute("someKey", "aValue");
62 String value = (String) session.getAttribute("someKey");
63 if (value.equals("aValue")) {
64 log.info("Retrieved the correct value! [" + value + "]");
65 }
66
67 // let's login the current user so we can check against roles and permissions:
68 if (!currentUser.isAuthenticated()) {
69 UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa");
70 token.setRememberMe(true);
71 try {
72 currentUser.login(token);
73 } catch (UnknownAccountException uae) {
74 log.info("There is no user with username of " + token.getPrincipal());
75 } catch (IncorrectCredentialsException ice) {
76 log.info("Password for account " + token.getPrincipal() + " was incorrect!");
77 } catch (LockedAccountException lae) {
78 log.info("The account for username " + token.getPrincipal() + " is locked. " +
79 "Please contact your administrator to unlock it.");
80 }
81 // ... catch more exceptions here (maybe custom ones specific to your application?
82 catch (AuthenticationException ae) {
83 //unexpected condition? error?
84 }
85 }
86
87 //say who they are:
88 //print their identifying principal (in this case, a username):
89 log.info("User [" + currentUser.getPrincipal() + "] logged in successfully.");
90
91 //test a role:
92 if (currentUser.hasRole("schwartz")) {
93 log.info("May the Schwartz be with you!");
94 } else {
95 log.info("Hello, mere mortal.");
96 }
97
98 //test a typed permission (not instance-level)
99 if (currentUser.isPermitted("lightsaber:wield")) {
100 log.info("You may use a lightsaber ring. Use it wisely.");
101 } else {
102 log.info("Sorry, lightsaber rings are for schwartz masters only.");
103 }
104
105 //a (very powerful) Instance Level permission:
106 if (currentUser.isPermitted("winnebago:drive:eagle5")) {
107 log.info("You are permitted to 'drive' the winnebago with license plate (id) 'eagle5'. " +
108 "Here are the keys - have fun!");
109 } else {
110 log.info("Sorry, you aren't allowed to drive the 'eagle5' winnebago!");
111 }
112
113 //all done - log out!
114 currentUser.logout();
115
116 System.exit(0);
117 }
118 }