Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring mvc 需要在没有密码的情况下手动创建oAuth2令牌_Spring Mvc_Spring Security_Oauth 2.0_Spring Boot_Spring Security Oauth2 - Fatal编程技术网

Spring mvc 需要在没有密码的情况下手动创建oAuth2令牌

Spring mvc 需要在没有密码的情况下手动创建oAuth2令牌,spring-mvc,spring-security,oauth-2.0,spring-boot,spring-security-oauth2,Spring Mvc,Spring Security,Oauth 2.0,Spring Boot,Spring Security Oauth2,我已经用spring security实现了oAuth2,它对我来说运行良好。但是现在我想在没有密码的情况下从后端手动创建用户令牌。因为我只有用户的用户名 有人能帮我吗。得到答案了 HashMap<String, String> authorizationParameters = new HashMap<String, String>(); authorizationParameters.put("scope", "read"); authoriz

我已经用spring security实现了oAuth2,它对我来说运行良好。但是现在我想在没有密码的情况下从后端手动创建用户令牌。因为我只有用户的用户名

有人能帮我吗。

得到答案了

    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);
HashMap authorizationParameters=newhashmap();
authorizationParameters.put(“范围”、“读取”);
authorizationParameters.put(“用户名”、“用户”);
authorizationParameters.put(“客户id”、“客户id”);
authorizationParameters.put(“授权”、“密码”);
Set authorities=new HashSet();
添加(新的SimpleGrantedAuthority(“角色用户”);
Set responseType=new HashSet();
responseType.add(“密码”);
Set scopes=new HashSet();
范围。添加(“读取”);
范围。添加(“写入”);
OAuth2Request authorizationRequest=新的OAuth2Request(
授权参数,“客户端Id”,
权限,true,作用域,null,“,
responseType,null);
User userPrincipal=新用户(“用户”,“”,true,true,true,authorities);
UsernamePasswordAuthenticationToken authenticationToken=新UsernamePasswordAuthenticationToken(
userPrincipal,null,authorities);
OAuth2Authentication authenticationRequest=新的OAuth2Authentication(
授权请求、authenticationToken);
authenticationRequest.setAuthenticated(true);
OAuth2AccessToken accessToken=tokenService
.createAccessToken(authenticationRequest);
accessToken是您想要的令牌

谢谢

在注册过程中分配访问令牌,Spring引导。从应用程序代码中的任意位置调用getAccessToken(用户)

public OAuth2AccessToken getAccessToken(用户){
HashMap authorizationParameters=新HashMap();
authorizationParameters.put(“范围”、“读取”);
authorizationParameters.put(“用户名”,user.getEmail());
authorizationParameters.put(“客户端id”,clientId);
authorizationParameters.put(“授权”、“密码”);
Set authorities=new HashSet();
user.getRoles().forEach((角色)->{
Role rol=roleRepository.findByName(Role.getName());
添加(新的SimpleGrantedAuthority(rol.getName());
});
Set responseType=new HashSet();
responseType.add(“密码”);
Set scopes=new HashSet();
范围。添加(“读取”);
范围。添加(“写入”);
OAuth2Request authorizationRequest=新的OAuth2Request(authorizationParameters、clientId、Authority、true、,
作用域,null,“,responseType,null);
org.springframework.security.core.userdetails.User userPrincipal=新建org.springframework.security.core.userdetails.User(
user.getEmail(),user.getPassword(),authorities);
UsernamePasswordAuthenticationToken authenticationToken=新的UsernamePasswordAuthenticationToken(userPrincipal,
无效(官方);
OAuth2Authentication authenticationRequest=新的OAuth2Authentication(authorizationRequest,
身份验证令牌);
authenticationRequest.setAuthenticated(true);
OAuth2AccessToken accessToken=tokenServices().createAccessToken(authenticationRequest);
返回accessToken;
}
@豆子
TokenEnhancerCain EnhancerCain(){
TokenEnhancerCain EnhancerCain=新的TokenEnhancerCain();
EnhancerCain.setTokenEnhancers(Arrays.asList(customTokenEnhancer,accessTokenConverter());
回归增强因子;
}
@豆子
公共JwtAccessTokenConverter accessTokenConverter(){
JwtAccessTokenConverter=新的JwtAccessTokenConverter();
转换器。设置点火键(信号键);
回流转换器;
}
@豆子
公共令牌库令牌库(){
返回新的JwtTokenStore(accessTokenConverter());
}
@豆子
@初级的
公共DefaultTokenServices令牌服务(){
DefaultTokenServices DefaultTokenServices=新的DefaultTokenServices();
setTokenStore(tokenStore());
defaultTokenServices.setSupportRefreshToken(true);
setTokenEnhancer(enhancerCain());
返回服务;
}

以上大多数答案都是正确的,但第五行应改为

authorizationParameters.put("grant_type", "password")

到目前为止你做了什么?您是否先自己尝试过?普通用户是使用用户/密码登录的,并且oAuth2令牌已成功创建。但我需要使用后端创建其他用户令牌,而不需要密码。您能澄清一下什么是令牌服务吗?令牌服务是春季的预告呼叫
authorizationParameters.put("grant_type", "password")