栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Spring OAuth2-在令牌存储中手动创建访问令牌

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring OAuth2-在令牌存储中手动创建访问令牌

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

        HashMap<String, String> authorizationParameters = new HashMap<String, String>();        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<GrantedAuthority> authorities = new HashSet<GrantedAuthority>();        authorities.add(new SimpleGrantedAuthority("ROLE_UNTRUSTED_CLIENT"));        authorizationRequest.setAuthorities(authorities);        HashSet<String> resourceIds = new HashSet<String>();        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);


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/453126.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号