Example usage for org.apache.shiro SecurityUtils getSecurityManager

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

Introduction

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

Prototype

public static SecurityManager getSecurityManager() throws UnavailableSecurityManagerException 

Source Link

Document

Returns the SecurityManager accessible to the calling code.

Usage

From source file:com.github.dactiv.fear.commons.service.auth.Subjects.java

License:Apache License

/**
 * ?/*from w  w w  . ja  va  2  s .  c o  m*/
 *
 * @return ?
 */
public static List<Map<String, Object>> getPrincipalNavs() {

    DefaultWebSecurityManager webSecurityManager = (DefaultWebSecurityManager) SecurityUtils
            .getSecurityManager();
    Collection<Realm> realms = webSecurityManager.getRealms();
    Subject subject = SecurityUtils.getSubject();

    List<Map<String, Object>> navs = new ArrayList<>();

    for (Realm realm : realms) {
        // ?? AuthorizationRealm  getAuthorizationInfo 
        if (realm instanceof AuthorizationRealm) {

            AuthorizationRealm authorizationRealm = (AuthorizationRealm) realm;
            AuthorizationInfo authorizationInfo = authorizationRealm
                    .getAuthorizationInfo(subject.getPrincipals());

            if (authorizationInfo == null) {
                continue;
            }

            NavAuthorizationInfo navAuthorizationInfo = (NavAuthorizationInfo) authorizationInfo;
            //  map ?????
            for (Map<String, Object> map : navAuthorizationInfo.getNavs()) {

                boolean flag = Boolean.TRUE;

                for (Map<String, Object> curr : navs) {
                    if (curr.get("id").equals(map.get("id"))) {
                        flag = Boolean.FALSE;
                        break;
                    }
                }

                if (flag) {
                    navs.add(map);
                }

            }
        }
    }

    return navs;
}

From source file:com.github.dactiv.fear.user.web.AccountController.java

License:Apache License

/**
 * ?//from   w  w  w .java2 s.  c o  m
 *
 * @param entity              Map
 * @param redirectAttributes spring mvc ??
 *
 * @return ? json
 *
 * @throws IOException
 */
@RequestMapping("update-profile")
public String updateProfile(@RequestParam Map<String, Object> entity, RedirectAttributes redirectAttributes)
        throws IOException {
    // ?? shiro subject ?
    Subject subject = SecurityUtils.getSubject();
    Map<String, Object> user = Casts.cast(subject.getPrincipal());
    // ???
    user.putAll(entity);
    // ??
    Apis.invoke("accountService", "saveUser", user, null);

    DefaultSecurityManager securityManager = (DefaultSecurityManager) SecurityUtils.getSecurityManager();
    // ?????
    AbstractRememberMeManager rmm = (AbstractRememberMeManager) securityManager.getRememberMeManager();
    rmm.rememberIdentity(subject, null,
            new SimpleAuthenticationInfo(subject.getPrincipals(), user.get("password")));

    // ? subjectDao, ???
    securityManager.getSubjectDAO().save(subject);

    redirectAttributes.addFlashAttribute("message", "??.");

    return "redirect:/account/user-profile";
}

From source file:com.github.sdorra.shiro.ShiroRule.java

License:Open Source License

/**
 * Method description//w w w  .  j  a v a  2 s .  co  m
 *
 */
private void tearDownShiro() {
    try {
        SecurityManager securityManager = SecurityUtils.getSecurityManager();

        LifecycleUtils.destroy(securityManager);
        ThreadContext.unbindSecurityManager();
        ThreadContext.unbindSubject();
        ThreadContext.remove();
    } catch (UnavailableSecurityManagerException e) {

        // we don't care about this when cleaning up the test environment
        // (for example, maybe the subclass is a unit test and it didn't
        // need a SecurityManager instance because it was using only mock Subject instances)
    }

    SecurityUtils.setSecurityManager(null);
}

From source file:com.sonicle.webtop.core.app.RunContext.java

License:Open Source License

private static boolean hasRole(PrincipalCollection principals, String role) {
    return hasRole(SecurityUtils.getSecurityManager(), principals, role);
}

From source file:com.sonicle.webtop.core.app.RunContext.java

License:Open Source License

private static boolean hasAllRoles(PrincipalCollection principals, Collection<String> roles) {
    return hasAllRoles(SecurityUtils.getSecurityManager(), principals, roles);
}

From source file:com.sonicle.webtop.core.app.RunContext.java

License:Open Source License

private static boolean isPermitted(boolean strict, PrincipalCollection principals, String serviceId, String key,
        String action, String instance) {
    if (principals.isEmpty())
        return false;
    SecurityManager manager = SecurityUtils.getSecurityManager();
    if (!strict && isWebTopAdmin(principals))
        return true;
    //if (manager.isPermitted(principals, WebTopManager.WTADMIN_PSTRING)) return true;
    return manager.isPermitted(principals, ServicePermission
            .permissionString(ServicePermission.namespacedName(serviceId, key), action, instance));
}

From source file:com.sonicle.webtop.core.app.shiro.ShiroUtils.java

License:Open Source License

public static RealmSecurityManager getRealmSecurityManager() {
    return (RealmSecurityManager) SecurityUtils.getSecurityManager();
}

From source file:com.thesett.util.security.shiro.ShiroUtils.java

License:Apache License

protected static SecurityManager getSecurityManager() {
    return SecurityUtils.getSecurityManager();
}

From source file:com.yea.shiro.web.interceptor.ShiroInterceptor.java

License:Apache License

@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
        ModelAndView modelAndView) throws Exception {
    // TODO Auto-generated method stub
    Subject subject = SecurityUtils.getSubject();
    if ((subject.isAuthenticated() || subject.isRemembered()) && modelAndView != null
            && modelAndView.getModel() != null) {
        modelAndView.getModel().put("loginuser", subject.getPrincipal());
        if (subject.getSession().getAttribute(ShiroConstants.SYSTEM_MENU) == null) {
            _InnerMenu menu = new _InnerMenu();
            menu.menu(((WebSecurityManager) SecurityUtils.getSecurityManager()).getEndpoint(), subject);
        }/*from  w w w  .  ja  va 2  s.  com*/
        modelAndView.getModel().put("systemMenu",
                subject.getSession().getAttribute(ShiroConstants.SYSTEM_MENU));
    }
}

From source file:de.iai.ilcd.services.AuthenticateResource.java

License:Open Source License

@GET
@Path("logout")
@Produces("text/plain")
public String logout() {
    logger.info("authenticate/logout");
    Subject currentUser = SecurityUtils.getSubject();
    if (currentUser.isAuthenticated()) {
        SecurityUtils.getSecurityManager().logout(currentUser);
        Response.status(Status.OK);
        return "successfully logged out";
    }/*  w w w.  j  av a2s. c  om*/
    // no user currently logged in
    return "currently not authenticated";
}