org.springframework.security.core.authority.GrantedAuthorityImpl Java Examples

The following examples show how to use org.springframework.security.core.authority.GrantedAuthorityImpl. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: secure-data-service   Author: inbloom   File: SLIAuthenticationEntryPoint.java    License: Apache License 2.0 4 votes vote down vote up
private SLIPrincipal completeSpringAuthentication(String token) {

        // Get authentication information
        JsonObject json = restClient.sessionCheck(token);

        LOG.debug(json.toString());

        // If the user is authenticated, create an SLI principal, and authenticate
        JsonElement authElement = json.get(Constants.ATTR_AUTHENTICATED);
        if ((authElement != null) && (authElement.getAsBoolean())) {

            // Setup principal
            SLIPrincipal principal = new SLIPrincipal();
            principal.setId(token);

            // Extract user name from authentication payload
            String username = "";
            JsonElement nameElement = json.get(Constants.ATTR_AUTH_FULL_NAME);
            if (nameElement != null) {
                username = nameElement.getAsString();
                if (username != null && username.contains("@")) {
                    username = username.substring(0, username.indexOf("@"));
                    if (username.contains(".")) {
                        String first = username.substring(0, username.indexOf('.'));
                        String second = username.substring(username.indexOf('.') + 1);
                        username = first.substring(0, 1).toUpperCase()
                                + (first.length() > 1 ? first.substring(1) : "")
                                + (second.substring(0, 1).toUpperCase() + (second.length() > 1 ? second.substring(1)
                                        : ""));
                    }
                }
            } else {
                LOG.error(LOG_MESSAGE_AUTH_EXCEPTION_INVALID_NAME);
            }

            // Set principal name
            principal.setName(username);

            // Extract user roles from authentication payload
            LinkedList<GrantedAuthority> authList = new LinkedList<GrantedAuthority>();
            JsonArray grantedAuthorities = json.getAsJsonArray(Constants.ATTR_AUTH_ROLES);
            if (grantedAuthorities != null) {

                // Add authorities to user principal
                Iterator<JsonElement> authIterator = grantedAuthorities.iterator();
                while (authIterator.hasNext()) {
                    JsonElement nextElement = authIterator.next();
                    authList.add(new GrantedAuthorityImpl(nextElement.getAsString()));
                }
            } else {
                LOG.error(LOG_MESSAGE_AUTH_EXCEPTION_INVALID_ROLES);
            }
            if(json.get(Constants.ATTR_USER_TYPE).getAsString().equals(Constants.ROLE_TEACHER)) {
              authList.add(new GrantedAuthorityImpl(Constants.ROLE_EDUCATOR));
            }

            if(json.get(Constants.ATTR_ADMIN_USER).getAsBoolean()) {
             authList.add(new GrantedAuthorityImpl(Constants.ROLE_IT_ADMINISTRATOR));
            }

            SecurityContextHolder.getContext().setAuthentication(
                    new PreAuthenticatedAuthenticationToken(principal, token, authList));

            return principal;
        } else {
            LOG.error(LOG_MESSAGE_AUTH_EXCEPTION_INVALID_AUTHENTICATED);
        }

        return null;
    }