I have implemented oAuth2 with spring security and it is working fine for me. But Now I want to create user token from back-end manually without password. Because I have only us
Got Answer!!!
HashMap authorizationParameters = new HashMap();
authorizationParameters.put("scope", "read");
authorizationParameters.put("username", "user");
authorizationParameters.put("client_id", "client_id");
authorizationParameters.put("grant", "password");
Set authorities = new HashSet();
authorities.add(new SimpleGrantedAuthority("ROLE_USER"));
Set responseType = new HashSet();
responseType.add("password");
Set scopes = new HashSet();
scopes.add("read");
scopes.add("write");
OAuth2Request authorizationRequest = new OAuth2Request(
authorizationParameters, "Client_Id",
authorities, true,scopes, null, "",
responseType, null);
User userPrincipal = new User("user", "", true, true, true, true, authorities);
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(
userPrincipal, null, authorities);
OAuth2Authentication authenticationRequest = new OAuth2Authentication(
authorizationRequest, authenticationToken);
authenticationRequest.setAuthenticated(true);
OAuth2AccessToken accessToken = tokenService
.createAccessToken(authenticationRequest);
accessToken is token which you want.
Thanks