Package org.apache.shiro.subject

Examples of org.apache.shiro.subject.Subject


  public static boolean login(String username, String password) {
    boolean isLogin = false;
    try {
      AuthenticationToken token = new UsernamePasswordToken(username, password);
      Subject currentUser = SecurityUtils.getSubject();
      currentUser.login(token);
      isLogin = true;
    } catch (AuthenticationException e) {
      log.error("login fail!,case as ", e);
    }
    return isLogin;
View Full Code Here


    }
    return isLogin;
  }

  public static void logout() {
    Subject currentUser = SecurityUtils.getSubject();
    currentUser.logout();
  }
View Full Code Here

    Subject currentUser = SecurityUtils.getSubject();
    currentUser.logout();
  }

  public static Object getSessionAttribute(String key) {
    Subject currentUser = SecurityUtils.getSubject();
    Session session = currentUser.getSession(false);
    return (session != null ? session.getAttribute(key) : null);
  }
View Full Code Here

    Session session = currentUser.getSession(false);
    return (session != null ? session.getAttribute(key) : null);
  }

  public static void setSessionAttribute(String name, Object value) {
    Subject currentUser = SecurityUtils.getSubject();
    if (value != null) {
      Session session = currentUser.getSession();
      session.setAttribute(name, value);
    } else {
      Session session = currentUser.getSession(false);
      session.removeAttribute(name);
    }
  }
View Full Code Here

                } finally {
                    IOHelper.close(objectInputStream);
                    IOHelper.close(byteArrayInputStream);
                }

                Subject currentUser = SecurityUtils.getSubject();

                // Authenticate user if not authenticated
                try {
                    authenticateUser(currentUser, securityToken);
               
                    // Test whether user's role is authorized to perform functions in the permissions list 
                    authorizeUser(currentUser, exchange);
                } finally {
                    if (alwaysReauthenticate) {
                        currentUser.logout();
                    }
                }
            }
        };
    }
View Full Code Here

   *
   * @return {@link SessionVariable}
   */
  public static SessionVariable getSessionVariable() {
   
    Subject subject = SecurityUtils.getSubject();
   
    if (subject != null && subject.getPrincipals() != null) {
      return subject.getPrincipals().oneByType(SessionVariable.class);
    }
   
    return null;
  }
View Full Code Here

    @Override
    protected boolean showTagBody(String commaDelimitedPermissions) {
        boolean hasAnyPermission = false;

        Subject subject = getSubject();

        if (subject != null) {
            // Iterate through permissions and check to see if the user has one of the permission
            String[] permissions = StringUtils.split(commaDelimitedPermissions);
            for (String permission : permissions) {
                if (subject.isPermitted(permission)) {
                    hasAnyPermission = true;
                    break;
                }
            }
        }
View Full Code Here

import static org.junit.Assert.assertSame;

public class ShiroSessionScopeTest {
    @Test
    public void testScope() throws Exception {
        Subject subject = createMock(Subject.class);
        try {
            final Key<SomeClass> key = Key.get(SomeClass.class);
            Provider<SomeClass> mockProvider = createMock(Provider.class);
            Session session = createMock(Session.class);

            SomeClass retuned = new SomeClass();

            expect(subject.getPrincipal()).andReturn("testUser").anyTimes();

            expect(subject.getSession()).andReturn(session);
            expect(session.getAttribute(key)).andReturn(null);
            expect(mockProvider.get()).andReturn(retuned);

            expect(subject.getSession()).andReturn(session);
            expect(session.getAttribute(key)).andReturn(retuned);


            replay(subject, mockProvider, session);
View Full Code Here

        SecurityUtils.setSecurityManager(securityManager);

        // Now that a simple Shiro environment is set up, let's see what you can do:

        // get the currently executing user:
        Subject currentUser = SecurityUtils.getSubject();

        // Do some stuff with a Session (no need for a web or EJB container!!!)
        Session session = currentUser.getSession();
        session.setAttribute("someKey", "aValue");
        String value = (String) session.getAttribute("someKey");
        if (value.equals("aValue")) {
            log.info("Retrieved the correct value! [" + value + "]");
        }

        // let's login the current user so we can check against roles and permissions:
        if (!currentUser.isAuthenticated()) {
            UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa");
            token.setRememberMe(true);
            try {
                currentUser.login(token);
            } catch (UnknownAccountException uae) {
                log.info("There is no user with username of " + token.getPrincipal());
            } catch (IncorrectCredentialsException ice) {
                log.info("Password for account " + token.getPrincipal() + " was incorrect!");
            } catch (LockedAccountException lae) {
                log.info("The account for username " + token.getPrincipal() + " is locked.  " +
                        "Please contact your administrator to unlock it.");
            }
            // ... catch more exceptions here (maybe custom ones specific to your application?
            catch (AuthenticationException ae) {
                //unexpected condition?  error?
            }
        }

        //say who they are:
        //print their identifying principal (in this case, a username):
        log.info("User [" + currentUser.getPrincipal() + "] logged in successfully.");

        //test a role:
        if (currentUser.hasRole("schwartz")) {
            log.info("May the Schwartz be with you!");
        } else {
            log.info("Hello, mere mortal.");
        }

        //test a typed permission (not instance-level)
        if (currentUser.isPermitted("lightsaber:weild")) {
            log.info("You may use a lightsaber ring.  Use it wisely.");
        } else {
            log.info("Sorry, lightsaber rings are for schwartz masters only.");
        }

        //a (very powerful) Instance Level permission:
        if (currentUser.isPermitted("winnebago:drive:eagle5")) {
            log.info("You are permitted to 'drive' the winnebago with license plate (id) 'eagle5'.  " +
                    "Here are the keys - have fun!");
        } else {
            log.info("Sorry, you aren't allowed to drive the 'eagle5' winnebago!");
        }

        //all done - log out!
        currentUser.logout();

        System.exit(0);
    }
View Full Code Here

                }
            }
            throw ae; //propagate
        }

        Subject loggedIn = createSubject(token, info, subject);

        onSuccessfulLogin(token, info, loggedIn);

        return loggedIn;
    }
View Full Code Here

TOP

Related Classes of org.apache.shiro.subject.Subject

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.