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:Global.java

License:Open Source License

@Override
public void onStop(Application application) {

    Logger.debug("DOING ON STOP");

    HibernateSessionFactory.closeSession();
    HibernateSessionFactory.unloadAll();
    DatabaseManager dbManager = DatabaseManager.getInstance();
    dbManager.initialize("default");
    dbManager.closeIfConnectionOpen();/*ww w  .  jav  a  2  s.c  o  m*/
    dbManager.unloadAll();

    RealmSecurityManager mgr = (RealmSecurityManager) SecurityUtils.getSecurityManager();

    Collection<Realm> realmCollection = mgr.getRealms();
    realmCollection.clear();

    /*
    Iterator<Realm> i = realmCollection.iterator();
            
    //There should be only one realm?
    while(i.hasNext()) {
            
    try {
        SampleRealm r = (SampleRealm) i.next();
            
        r.invalidateUser(SecurityUtils.getSubject().getPrincipals());
    } catch (Exception ee) {
            
    }
            
    }*/

    mgr.destroy();

    super.onStop(application);
}

From source file:annis.service.internal.AdminServiceImpl.java

License:Apache License

@GET
@Path("users")
@Produces("application/xml")
public List<User> listUsers() {
    Subject requestingUser = SecurityUtils.getSubject();
    requestingUser.checkPermission("admin:read:user");

    if (SecurityUtils.getSecurityManager() instanceof ANNISSecurityManager) {
        ANNISUserConfigurationManager confManager = getConfManager();
        if (confManager != null) {
            return confManager.listAllUsers();
        }//from w w w  .  ja  va2 s .  c o m
    }
    return new LinkedList<>();
}

From source file:annis.service.internal.AdminServiceImpl.java

License:Apache License

@DELETE
@Path("users/{userName}")
public Response deleteUser(@PathParam("userName") String userName) {
    Subject requestingUser = SecurityUtils.getSubject();
    requestingUser.checkPermission("admin:write:user");

    if (SecurityUtils.getSecurityManager() instanceof ANNISSecurityManager) {
        ANNISUserConfigurationManager confManager = getConfManager();
        if (confManager != null) {
            if (confManager.deleteUser(userName)) {
                // also delete any possible user configs
                adminDao.deleteUserConfig(userName);
                // if no error until here everything went well
                return Response.ok().build();
            }/*from   w w w  .j  a v  a2s .c  o  m*/
        }
    }
    return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("Could not delete user").build();
}

From source file:annis.service.internal.AdminServiceImpl.java

License:Apache License

@GET
@Path("groups")
@Produces("application/xml")
public List<Group> listGroups() {
    Subject requestingUser = SecurityUtils.getSubject();
    requestingUser.checkPermission("admin:read:group");

    if (SecurityUtils.getSecurityManager() instanceof ANNISSecurityManager) {
        ANNISUserConfigurationManager confManager = getConfManager();
        if (confManager != null) {
            return new LinkedList<>(confManager.getGroups().values());
        }/* w  w w  . j  a  v  a 2  s .co  m*/
    }
    return new LinkedList<>();
}

From source file:annis.service.internal.AdminServiceImpl.java

License:Apache License

@PUT
@Path("groups/{groupName}")
@Consumes("application/xml")
public Response updateOrCreateGroup(Group group, @PathParam("groupName") String groupName) {

    Subject requestingUser = SecurityUtils.getSubject();
    requestingUser.checkPermission("admin:write:group");

    if (!groupName.equals(group.getName())) {
        return Response.status(Response.Status.BAD_REQUEST)
                .entity("Group name in object is not the same as in path").build();
    }/* www  .j a  va  2s. c  om*/

    if (SecurityUtils.getSecurityManager() instanceof ANNISSecurityManager) {
        ANNISUserConfigurationManager confManager = getConfManager();
        if (confManager != null) {
            if (confManager.writeGroup(group)) {
                return Response.ok().build();
            }
        }
    }
    return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("Could not update/create group")
            .build();
}

From source file:annis.service.internal.AdminServiceImpl.java

License:Apache License

@DELETE
@Path("groups/{groupName}")
public Response deleteGroup(@PathParam("groupName") String groupName) {

    Subject requestingUser = SecurityUtils.getSubject();
    requestingUser.checkPermission("admin:write:group");

    if (SecurityUtils.getSecurityManager() instanceof ANNISSecurityManager) {
        ANNISUserConfigurationManager confManager = getConfManager();
        if (confManager != null) {

            if (confManager.deleteGroup(groupName)) {
                return Response.ok().build();
            }/*from   w ww .  jav a 2 s .  c  o  m*/

        }
    }
    return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("Could not delete group").build();
}

From source file:annis.service.internal.AdminServiceImpl.java

License:Apache License

private ANNISUserConfigurationManager getConfManager() {
    if (SecurityUtils.getSecurityManager() instanceof ANNISSecurityManager) {
        ANNISUserConfigurationManager confManager = ((ANNISSecurityManager) SecurityUtils.getSecurityManager())
                .getConfManager();/*from  w  w w  .  java2s .  c om*/
        return confManager;
    }
    return null;
}

From source file:annis.service.internal.AdminServiceImpl.java

License:Apache License

private ANNISUserRealm getUserRealm() {
    if (SecurityUtils.getSecurityManager() instanceof ANNISSecurityManager) {
        ANNISUserRealm userRealm = ((ANNISSecurityManager) SecurityUtils.getSecurityManager())
                .getANNISUserRealm();/*from  www . j  a  v  a  2 s . c o  m*/
        return userRealm;
    }
    return null;
}

From source file:br.com.criativasoft.opendevice.restapi.auth.BearerAuthRealm.java

License:Open Source License

public AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

    BearerAuthToken authToken = (BearerAuthToken) token;

    String authTokenS = (String) authToken.getPrincipal();

    DefaultSecurityManager securityManager = (DefaultSecurityManager) SecurityUtils.getSecurityManager();
    Cache<Object, Object> cache = securityManager.getCacheManager().getCache(TOKEN_CACHE);

    DataManager context = manager.getDataManager();

    String apiKey = (String) cache.get(authTokenS);

    // The token is API_KEY
    if (apiKey == null && authToken.isApikey()) {
        apiKey = authTokenS;//from   w ww.j  a v a  2  s  .  c om
    }

    if (apiKey == null)
        log.warn("ApiKey not found for token : " + authTokenS);

    if (apiKey != null && context instanceof ApiDataManager) {

        AccountDao dao = ((ApiDataManager) context).getAccountDao();

        UserAccount userAccount = dao.getUserAccountByApiKey(apiKey);

        if (userAccount != null) {
            Account account = userAccount.getOwner();

            AccountType type = userAccount.getType();

            AccountPrincipal principal = new AccountPrincipal(userAccount.getUser().getId(),
                    userAccount.getId(), account.getUuid(), type);

            // todo: load permission tags into AuthenticationInfo
            return new SimpleAuthenticationInfo(principal, authToken.getCredentials(), "BearerTokenRealm");
        }
    }

    return null;
}

From source file:br.com.criativasoft.opendevice.restapi.auth.GoogleAuthRealm.java

License:Open Source License

public AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

    GoogleAuthToken authToken = (GoogleAuthToken) token;

    String authTokenS = (String) authToken.getPrincipal();

    DefaultSecurityManager securityManager = (DefaultSecurityManager) SecurityUtils.getSecurityManager();
    Cache<Object, Object> cache = securityManager.getCacheManager().getCache(TOKEN_CACHE);

    DataManager context = manager.getDataManager();
    AccountDao dao = ((ApiDataManager) context).getAccountDao();

    String userAccountID = (String) cache.get(authTokenS);

    if (userAccountID == null) {

        log.warn("ApiKey not found for token : " + authTokenS);

        try {/*  w  ww  . j av  a 2  s. co  m*/
            String url = "https://www.googleapis.com/oauth2/v3/tokeninfo?access_token=";
            CloseableHttpClient client = HttpClientBuilder.create().build();
            CloseableHttpResponse response = client.execute(new HttpGet(url + authTokenS));
            String bodyAsString = EntityUtils.toString(response.getEntity());

            if (response.getStatusLine().getStatusCode() == 200) {

                String appID = ODev.getConfig().getString(OpenDeviceConfig.ConfigKey.google_appid);

                if (appID == null) {
                    throw new AuthenticationException("Google AppID not configured !");
                }

                JsonNode json = new ObjectMapper().readTree(bodyAsString);

                String aud = json.get("aud").asText();

                // TODO: need validate, but this may ne used for another appletavions IDs (ALEXA, MIDDLEWARE)
                //                    if(!appID.equals(aud)){
                //                        throw new AuthenticationException("Invalid Google Token");
                //                    }

                UserDao userDao = ((ApiDataManager) context).getUserDao();
                User user = userDao.getUser(json.get("email").asText());

                // Store in cahe
                if (user != null) {
                    userAccountID = "" + user.getLasLoginAccount().getId();
                    cache.put(authTokenS, userAccountID);
                }

            } else {
                throw new AuthenticationException("Invalid Google Token");
            }

        } catch (IOException ex) {
            throw new AuthenticationException(ex.getMessage());
        }
    }

    if (userAccountID != null && context instanceof ApiDataManager) {

        UserAccount userAccount = dao.getUserAccountByID(Long.parseLong(userAccountID));

        if (userAccount != null) {
            Account account = userAccount.getOwner();

            AccountType type = userAccount.getType();

            AccountPrincipal principal = new AccountPrincipal(userAccount.getUser().getId(),
                    userAccount.getId(), account.getUuid(), type);

            // todo: load permission tags into AuthenticationInfo
            return new SimpleAuthenticationInfo(principal, authToken.getCredentials(), "BearerTokenRealm");
        }
    }

    return null;
}