MCPcopy Create free account
hub / github.com/apache/shiro / main

Method main

samples/quickstart/src/main/java/Quickstart.java:41–123  ·  view source on GitHub ↗
(String[] args)

Source from the content-addressed store, hash-verified

39
40
41 public static void main(String[] args) {
42
43 // The easiest way to create a Shiro SecurityManager with configured
44 // realms, users, roles and permissions is to use the simple INI config.
45 // We'll do that by using a factory that can ingest a .ini file and
46 // return a SecurityManager instance:
47
48 // Use the shiro.ini file at the root of the classpath
49 // (file: and url: prefixes load from files and urls respectively):
50 SecurityManager securityManager = new BasicIniEnvironment("classpath:shiro.ini").getSecurityManager();
51
52 // for this simple example quickstart, make the SecurityManager
53 // accessible as a JVM singleton. Most applications wouldn't do this
54 // and instead rely on their container configuration or web.xml for
55 // webapps. That is outside the scope of this simple quickstart, so
56 // we'll just do the bare minimum, so you can continue to get a feel
57 // for things.
58 SecurityUtils.setSecurityManager(securityManager);
59
60 // Now that a simple Shiro environment is set up, let's see what you can do:
61
62 // get the currently executing user:
63 Subject currentUser = SecurityUtils.getSubject();
64
65 // Do some stuff with a Session (no need for a web or EJB container!!!)
66 Session session = currentUser.getSession();
67 session.setAttribute("someKey", "aValue");
68 String value = (String) session.getAttribute("someKey");
69 if (value.equals("aValue")) {
70 log.info("Retrieved the correct value! [" + value + "]");
71 }
72
73 // let's login the current user so we can check against roles and permissions:
74 if (!currentUser.isAuthenticated()) {
75 UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa");
76 token.setRememberMe(true);
77 try {
78 currentUser.login(token);
79 } catch (UnknownAccountException uae) {
80 log.info("There is no user with username of " + token.getPrincipal());
81 } catch (IncorrectCredentialsException ice) {
82 log.info("Password for account " + token.getPrincipal() + " was incorrect!");
83 } catch (LockedAccountException lae) {
84 log.info("The account for username " + token.getPrincipal() + " is locked. " +
85 "Please contact your administrator to unlock it.");
86 }
87 // ... catch more exceptions here (maybe custom ones specific to your application?
88 catch (AuthenticationException ae) {
89 //unexpected condition? error?
90 }
91 }
92
93 //say who they are:
94 //print their identifying principal (in this case, a username):
95 log.info("User [" + currentUser.getPrincipal() + "] logged in successfully.");
96
97 //test a role:
98 if (currentUser.hasRole("schwartz")) {

Callers

nothing calls this directly

Calls 15

setSecurityManagerMethod · 0.95
getSubjectMethod · 0.95
getSessionMethod · 0.95
setAttributeMethod · 0.95
getAttributeMethod · 0.95
isAuthenticatedMethod · 0.95
setRememberMeMethod · 0.95
loginMethod · 0.95
getPrincipalMethod · 0.95
getPrincipalMethod · 0.95
hasRoleMethod · 0.95
isPermittedMethod · 0.95

Tested by

no test coverage detected