我用jwt和oauth2(在内存中)实现了spring boot。

在这里,令牌位于内存而不是数据库中。

但是当我使用tokenStore.findTokensByClientId(clientId)然后在以下情况下它返回空白数组



@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {


    static final String CLIEN_ID = "vishvas-client";
    static final String CLIENT_SECRET = "$2a$10$kfH4W.jyBuqvX5TLu.OfbOEUtScm4V9FEUDvGI8AWPaqObUOQ7HJ2"; // vishvas-secret
    static final String GRANT_TYPE_PASSWORD = "password";
    static final String AUTHORIZATION_CODE = "authorization_code";
    static final String REFRESH_TOKEN = "refresh_token";
    static final String IMPLICIT = "implicit";
    static final String SCOPE_READ = "read";
    static final String SCOPE_WRITE = "write";
    static final String TRUST = "trust";
    static final int ACCESS_TOKEN_VALIDITY_SECONDS = 12 * 60 * 60; // 12 hour
    static final int FREFRESH_TOKEN_VALIDITY_SECONDS = 24 * 60 * 60; // 24 hour

    @Autowired
    private AuthenticationManager authenticationManager;

    @Bean
    public JwtAccessTokenConverter accessTokenConverter() {
        JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
        converter.setSigningKey("as466gf");
        return converter;
    }

    @Bean
    public TokenStore tokenStore() {
        //  return new InMemoryTokenStore();   // Success : working but same access token generated every time. i want different access tokens
        return new JwtTokenStore(accessTokenConverter()); // Error :  tokenStore.findTokensByClientId(clientId) returns blank

    }

    @Override
    public void configure(ClientDetailsServiceConfigurer configurer) throws Exception {

        configurer.inMemory().withClient(CLIEN_ID).secret(CLIENT_SECRET)
                .authorizedGrantTypes(GRANT_TYPE_PASSWORD, AUTHORIZATION_CODE, REFRESH_TOKEN, IMPLICIT)
                .scopes(SCOPE_READ, SCOPE_WRITE, TRUST).accessTokenValiditySeconds(ACCESS_TOKEN_VALIDITY_SECONDS)
                .refreshTokenValiditySeconds(FREFRESH_TOKEN_VALIDITY_SECONDS);
    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.tokenStore(tokenStore()).authenticationManager(authenticationManager)
                .accessTokenConverter(accessTokenConverter());
    }
}


@RestController
@RequestMapping("/api/tokens")
public class TokensEndpointController {

    @Autowired
    private TokenStore tokenStore;

    @CrossOrigin
    @SuppressWarnings({ "unchecked", "rawtypes", "deprecation" })
    @GetMapping
    public ResponseEntity<?> findAllActiveSessions(@RequestParam String clientId,
            HttpServletRequest httpServletRequest) {

        try {

            String username = httpServletRequest.getUserPrincipal().getName();

            Collection<OAuth2AccessToken> tokens = tokenStore.findTokensByClientId(clientId);
            List<String> tokenValues = tokens.stream().map(OAuth2AccessToken::getValue).collect(Collectors.toList());

            System.out.println("tokenValues : " + tokenValues); // Blank array

            return ResponseEntity.ok(new ResponseWrapperDTO(HttpServletResponse.SC_OK,
                    httpServletRequest.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE).toString(),
                    "Tokens got successfully.", tokenValues));

        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity(
                    new ResponseErrorDTO(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                            MethodUtils.getApiPathFromHttpServletRequest(httpServletRequest), e.getMessage()),
                    HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

}


示例说明:


tokenStore()方法中,如果使用return new InMemoryTokenStore();,则可以使用api(http://localhost:8080/api/tokens?clientId=vishvas-client)成功获取所有令牌,但是每次生成相同的访问令牌
tokenStore()方法中,如果我使用return new JwtTokenStore(accessTokenConverter());,则api(http://localhost:8080/api/tokens?clientId=vishvas-client)返回空数组。 (第二点问题,无法获得令牌)

最佳答案

在第一个配置“ InMemoryTokenStore”中:


是标准的oauth2令牌生成方法,这意味着将为每个用户生成一个令牌并将其存储(因此,当您向授权服务器询问客户端的令牌时,它将获取而不生成令牌),直到令牌到期为止将生成另一个令牌




  这就是为什么您每次都获得相同的令牌的原因,但是当该令牌到期时,授权服务器将生成另一个令牌



在第二个配置“ JwtTokenStore”中:


是标准的oauth2 jwt令牌生成方法,意味着每次您请求令牌时,授权服务器都会为您生成一个令牌,并且不会将其存储在内存中
此时,如果您使用的是JWT令牌,则服务器必须是无状态的,这意味着无需令牌存储或用户信息存储,因为JWT令牌是独立的,服务器无需存储令牌或用户信息




  这就是为什么您每次都会获得不同的令牌,但列表为空的原因


您可以在第162行的link中检查JwtTokenStore类,您会发现默认情况下会给出一个空列表

因此,对于标准配置,您应该使用JwtTokenStore

关于java - 在Spring Boot JWT Plus Oauth2中,TokenStore findTokensByClientId(clientId)返回空数组(我想要 Activity token ),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55791919/

10-13 04:25