当前位置: 首页 > 面试题库 >

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

有宏邈
2023-03-14
问题内容

我遇到一种情况,我想自己创建一个访问令牌(因此不能通过通常的过程)。我想出了这样的东西:

@Inject
private DefaultTokenServices defaultTokenServices;

...

OAuth2Authentication auth = xxx;
OAuth2AccessToken  token = defaultTokenServices.createAccessToken(auth);

唯一的问题是我不确定如何创建OAuth2Authentication(在我的代码中带有xxx的部分)。我有用户和客户信息,我知道我想授予该令牌的单位。


问题答案:

在这里,根据使用的流程,您的用例可能会略有不同。这适用于密码授予流程。有一些自定义类,如令牌存储,令牌增强器等。但这实际上只是为满足我们自己的需要而修改的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);


 类似资料:
  • 基本上,我有两种方式将文件上传到firebase存储: null 我有一个onFinalize触发器,每次在firebase存储中存储新对象时都会调用该触发器。 在此触发器中,我希望为添加的特定对象创建一个firestore文档。 为了获取文件信息,我使用probe-image-size。 问题是我需要该文件的下载链接,而据我所知,如果没有storageAccessToken,我无法下载该文件。

  • 用户必须在浏览器和应用程序之间手动复制粘贴“授权码”,这在Android中不是一种实用的获取访问令牌的方法。 为了找到一种新的方法,我使用Android API中的来获取访问令牌,同时使用Java API中的和来获取文件列表,如下所示, 不幸的是,当调用中的时,应用程序总是由于NetworkOnMainThreadException而崩溃。 所以我的问题是,如何通过使用访问令牌而不是在中应用授权代

  • 在处理基于浏览器的应用程序时,关于安全存储JWT令牌的主题已经提出了很多问题。大家一致认为,应该使用仅限http的安全cookie。然而,当涉及短期访问令牌和长期刷新令牌时,存储JWT令牌似乎存在许多变化。 我发现了以下变化: 1.仅将JWT访问令牌和刷新令牌存储在http安全cookie中 赞成的意见: 无法从Javascript访问访问令牌和刷新令牌 欺骗: 引入CSRF漏洞,因此也必须添加C

  • 我试图在vue js中构建镜像上传器,我希望它可以恢复上传,我从浏览器直接将请求发送到云存储服务器(例如,不使用php作为发送上传请求的中间件) 但是文档说我必须有访问令牌才能获取会话url,我稍后将使用它来上传图像,如何使用JavaScript获取此访问令牌,谷歌云没有javascript库,但在我不能用于浏览器使用的节点js中 邮递https://www.googleapis.com/uplo

  • 我正在尝试构建一个Spring Boot REST API,它将实现社交登录(Spotify)。成功登录Spotify后,我想将Spotify access_令牌存储在我生成的JWT令牌中,以便能够访问我的后端。我需要一个Spotify访问令牌,以便能够对Spotify进行API调用(几乎所有对我的应用程序的请求都需要调用Spotify API)。在JWT中存储外部服务的访问令牌是一种好做法吗?或