我已经使用Spring安全性实现了oAuth2,它对我来说很好。但现在我想手动从后端手动创建用户令牌而无需密码。因为我只有用户名。
任何人都可以帮助我。
答案 0 :(得分:16)
得到答案!!!
HashMap<String, String> authorizationParameters = new HashMap<String, String>();
authorizationParameters.put("scope", "read");
authorizationParameters.put("username", "user");
authorizationParameters.put("client_id", "client_id");
authorizationParameters.put("grant", "password");
Set<GrantedAuthority> authorities = new HashSet<GrantedAuthority>();
authorities.add(new SimpleGrantedAuthority("ROLE_USER"));
Set<String> responseType = new HashSet<String>();
responseType.add("password");
Set<String> scopes = new HashSet<String>();
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是您想要的令牌。
谢谢
答案 1 :(得分:1)
上面的大多数答案都是正确的,但第五行应更改为
authorizationParameters.put("grant_type", "password")
答案 2 :(得分:0)
在注册过程中分配访问令牌,春季启动。从应用程序代码中的任何位置调用getAccessToken(user)。
public OAuth2AccessToken getAccessToken(User user) {
HashMap<String, String> authorizationParameters = new HashMap<String, String>();
authorizationParameters.put("scope", "read");
authorizationParameters.put("username", user.getEmail());
authorizationParameters.put("client_id", clientId);
authorizationParameters.put("grant", "password");
Set<GrantedAuthority> authorities = new HashSet<GrantedAuthority>();
user.getRoles().forEach((role) -> {
Role rol = roleRepository.findByName(role.getName());
authorities.add(new SimpleGrantedAuthority(rol.getName()));
});
Set<String> responseType = new HashSet<String>();
responseType.add("password");
Set<String> scopes = new HashSet<String>();
scopes.add("read");
scopes.add("write");
OAuth2Request authorizationRequest = new OAuth2Request(authorizationParameters, clientId, authorities, true,
scopes, null, "", responseType, null);
org.springframework.security.core.userdetails.User userPrincipal = new org.springframework.security.core.userdetails.User(
user.getEmail(), user.getPassword(), authorities);
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(userPrincipal,
null, authorities);
OAuth2Authentication authenticationRequest = new OAuth2Authentication(authorizationRequest,
authenticationToken);
authenticationRequest.setAuthenticated(true);
OAuth2AccessToken accessToken = tokenServices().createAccessToken(authenticationRequest);
return accessToken;
}
@Bean
TokenEnhancerChain enhancerChain() {
TokenEnhancerChain enhancerChain = new TokenEnhancerChain();
enhancerChain.setTokenEnhancers(Arrays.asList(customTokenEnhancer, accessTokenConverter()));
return enhancerChain;
}
@Bean
public JwtAccessTokenConverter accessTokenConverter() {
JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
converter.setSigningKey(signingKey);
return converter;
}
@Bean
public TokenStore tokenStore() {
return new JwtTokenStore(accessTokenConverter());
}
@Bean
@Primary
public DefaultTokenServices tokenServices() {
DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
defaultTokenServices.setTokenStore(tokenStore());
defaultTokenServices.setSupportRefreshToken(true);
defaultTokenServices.setTokenEnhancer(enhancerChain());
return defaultTokenServices;
}