Example usage for org.apache.shiro SecurityUtils getSubject

List of usage examples for org.apache.shiro SecurityUtils getSubject

Introduction

In this page you can find the example usage for org.apache.shiro SecurityUtils getSubject.

Prototype

public static Subject getSubject() 

Source Link

Document

Returns the currently accessible Subject available to the calling code depending on runtime environment.

Usage

From source file:cn.adfi.radius.controller.LoginController.java

@RequestMapping(value = "me", method = RequestMethod.GET)
public User getMe() {
    Subject subject = SecurityUtils.getSubject();
    Session session = subject.getSession();

    return (User) session.getAttribute("user");
}

From source file:cn.adfi.radius.controller.LoginController.java

@RequestMapping(value = "logout", method = RequestMethod.GET)
public void logout() {
    Subject subject = SecurityUtils.getSubject();
    subject.logout();
}

From source file:cn.adfi.radius.controller.LoginController.java

@RequestMapping(value = "authenticate", method = RequestMethod.POST)
public AngularShiroLoginResponse shiroLogin(@RequestBody TokenWarpper tokenWarpper) throws Exception {

    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    if (new Date().after(df.parse("2015-04-01"))) {
        throw new LicenseExpiredException("License Expired!");
    }//from   w w w.  ja  v  a2s.  c om

    Subject subject = SecurityUtils.getSubject();
    subject.login(new UsernamePasswordToken(tokenWarpper.getToken().getPrincipal(),
            tokenWarpper.getToken().getCredentials()));

    User user;
    if (subject.isAuthenticated()) {
        List<User> lst = userRepo.findByUsername(tokenWarpper.getToken().getPrincipal());
        Session session = subject.getSession();
        session.setAttribute("user", lst.get(0));
        user = lst.get(0);
    } else {
        throw new Exception("Username or Password error!");
    }

    AngularShiroAuthc authc = new AngularShiroAuthc();
    AngularShiroPrincipal principal = new AngularShiroPrincipal();
    principal.setLogin(user.getUsername());
    principal.setName(user.getFullname());
    principal.setEmail(user.getEmail());
    authc.setPrincipal(principal);

    AngularShiroCredentials credentials = new AngularShiroCredentials();
    credentials.setLogin(user.getUsername());
    credentials.setName(user.getFullname());
    credentials.setEmail(user.getEmail());
    authc.setCredentials(credentials);

    AngularShiroAuthz authz = new AngularShiroAuthz();
    authz.setRoles(user.getRolesStringSet());
    authz.setPermissions(user.getPermissionStringSet());

    AngularShiroInfo info = new AngularShiroInfo();
    info.setAuthc(authc);
    info.setAuthz(authz);
    AngularShiroLoginResponse resp = new AngularShiroLoginResponse();
    resp.setInfo(info);
    return resp;
}

From source file:cn.aozhi.songify.service.account.AccountService.java

License:Apache License

/**
 * ?Shiro?LoginName.
 */
private String getCurrentUserName() {
    ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
    return user.loginName;
}

From source file:cn.aozhi.songify.web.account.ProfileController.java

License:Apache License

/**
 * ?Shiro?Id.
 */
private Long getCurrentUserId() {
    ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
    return user.id;
}

From source file:cn.aozhi.songify.web.account.ProfileController.java

License:Apache License

/**
 * Shiro???.
 */
private void updateCurrentUserName(String userName) {
    ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
    user.name = userName;
}

From source file:cn.cdwx.jpa.web.account.ProfileController.java

License:Apache License

/**
 * ?Shiro?Id.
 */
private String getCurrentUserId() {
    ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
    return user.id;
}

From source file:cn.cjam.test.TestShiro.java

public static void main(String[] args) {
    log.info("My First Apache Shiro Application");

    Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
    SecurityManager securityManager = factory.getInstance();
    SecurityUtils.setSecurityManager(securityManager);

    // ??://from  w w  w  . ja  v  a2  s.c o  m
    Subject currentUser = SecurityUtils.getSubject();

    // ? Session 
    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 + "]");
    }

    // ???
    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 (AuthenticationException ae) {
            //??
        }
    }

    //?:
    //??? ( username):
    log.info("User [" + currentUser.getPrincipal() + "] logged in successfully.");

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

    //?? (?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.");
    }

    //(?)??:
    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!");
    }

    //? - t!
    currentUser.logout();

    System.exit(0);
}

From source file:cn.com.cowboy.project.web.filter.CustomFormAuthenticationFilter.java

License:Apache License

/**
 * <p>/*from w  w  w .j  a  v a2  s  .c  o  m*/
 * session
 * </p>
 * 
 * @param username
 */
private void setUserInstanceToSession(String username) {
    SecurityUtils.getSubject().getSession().setAttribute("cuser", userBus.findByName(username));
}

From source file:cn.com.infcn.ade.system.service.UserRealm.java

/**
 * ?,.//from  ww w.  ja v  a 2  s. c o m
 */
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken)
        throws AuthenticationException {
    try {
        checkHandler.checkExpireDate();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        throw new LicenseException(e.getMessage(), e);
    }
    UsernamePasswordCaptchaToken token = (UsernamePasswordCaptchaToken) authcToken;
    User user = userService.getUser(token.getUsername());

    if (user != null && doCaptchaValidate(token)) {
        byte[] salt = Encodes.decodeHex(user.getSalt());
        ShiroUser shiroUser = new ShiroUser(user.getId(), user.getLoginName(), user.getName());
        //session
        Session session = SecurityUtils.getSubject().getSession();
        session.setAttribute("user", user);
        return new SimpleAuthenticationInfo(shiroUser, user.getPassword(), ByteSource.Util.bytes(salt),
                getName());
    } else {
        return null;
    }
}