java 令牌访问_java – Spring OAuth2 – 在令牌存储中手动创建访问令牌

在这里,您的使用案例可能会根据您使用的流程略有不同.这适用于密码授予流程.有一些自定义类,如令牌存储,令牌增强等.但这只是为了我们自己的需要而修改的弹簧类的扩展版本.

HashMap authorizationParameters = new HashMap();

authorizationParameters.put("scope", "read");

authorizationParameters.put("username", "mobile_client");

authorizationParameters.put("client_id", "mobile-client");

authorizationParameters.put("grant", "password");

DefaultAuthorizationRequest authorizationRequest = new DefaultAuthorizationRequest(authorizationParameters);

authorizationRequest.setApproved(true);

Set authorities = new HashSet();

authorities.add(new SimpleGrantedAuthority("ROLE_UNTRUSTED_CLIENT"));

authorizationRequest.setAuthorities(authorities);

HashSet resourceIds = new HashSet();

resourceIds.add("mobile-public");

authorizationRequest.setResourceIds(resourceIds);

// Create principal and auth token

User userPrincipal = new User(user.getUserID(), "", true, true, true, true, authorities);

UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(userPrincipal, null, authorities) ;

OAuth2Authentication authenticationRequest = new OAuth2Authentication(authorizationRequest, authenticationToken);

authenticationRequest.setAuthenticated(true);

CustomTokenStore tokenStore = new CustomTokenStore();

// Token Enhancer

CustomTokenEnhancer tokenEnhancer = new CustomTokenEnhancer(user.getUserID());

CustomTokenServices tokenServices = new CustomTokenServices();

tokenServices.setTokenEnhancer(tokenEnhancer);

tokenServices.setSupportRefreshToken(true);

tokenServices.setTokenStore(tokenStore);

OAuth2AccessToken accessToken = tokenServices.createAccessTokenForUser(authenticationRequest, user);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值