Example usage for org.springframework.security.oauth2.provider TokenRequest createOAuth2Request

List of usage examples for org.springframework.security.oauth2.provider TokenRequest createOAuth2Request

Introduction

In this page you can find the example usage for org.springframework.security.oauth2.provider TokenRequest createOAuth2Request.

Prototype

public OAuth2Request createOAuth2Request(ClientDetails client) 

Source Link

Usage

From source file:it.smartcommunitylab.aac.apimanager.APIProviderManager.java

/**
 * //from   ww w .  j  a v a  2  s. c  om
 * @return
 * @throws Exception
 */
@Transactional(isolation = Isolation.SERIALIZABLE)
public String createToken() throws Exception {
    Map<String, String> requestParameters = new HashMap<>();
    String apiManagerName = getAPIManagerName();

    Long userId = userManager.getUser().getId();
    if (apiManagerName == null) {
        return null;
    }
    requestParameters.put("username", apiManagerName);
    requestParameters.put("password", "");

    // USER
    org.springframework.security.core.userdetails.User user = new org.springframework.security.core.userdetails.User(
            userId.toString(), "", new ArrayList<GrantedAuthority>());

    ClientDetails clientDetails = getAPIMgmtClient();
    TokenRequest tokenRequest = new TokenRequest(requestParameters, clientDetails.getClientId(), scopes(),
            "password");
    OAuth2Request oAuth2Request = tokenRequest.createOAuth2Request(clientDetails);
    Collection<? extends GrantedAuthority> list = authorities();
    OAuth2Authentication oAuth2Authentication = new OAuth2Authentication(oAuth2Request,
            new UsernamePasswordAuthenticationToken(user, "", list));
    OAuth2AccessToken accessToken = tokenService.createAccessToken(oAuth2Authentication);
    return accessToken.getValue();
}

From source file:it.smartcommunitylab.aac.apimanager.APIProviderManager.java

/**
 * //from  www .j ava 2 s  .c  o  m
 * @return
 * @throws Exception
 */
@Transactional(isolation = Isolation.SERIALIZABLE)
public String createToken(String username, String password) throws Exception {
    Map<String, String> requestParameters = new HashMap<>();

    User userObj = userRepository.findByUsername(username);

    if (userObj != null) {
        Long userId = userObj.getId();

        requestParameters.put("username", username);
        requestParameters.put("password", password);

        // USER
        org.springframework.security.core.userdetails.User user = new org.springframework.security.core.userdetails.User(
                userId.toString(), "", new ArrayList<GrantedAuthority>());

        ClientDetails clientDetails = getAPIMgmtClient();
        TokenRequest tokenRequest = new TokenRequest(requestParameters, clientDetails.getClientId(), scopes(),
                "password");
        OAuth2Request oAuth2Request = tokenRequest.createOAuth2Request(clientDetails);
        Collection<? extends GrantedAuthority> list = authorities(userObj);
        OAuth2Authentication oAuth2Authentication = new OAuth2Authentication(oAuth2Request,
                new UsernamePasswordAuthenticationToken(user, "", list));
        OAuth2AccessToken accessToken = tokenService.createAccessToken(oAuth2Authentication);
        return accessToken.getValue();
    }
    return null;
}