spring boot oauth2 token JWT converter

Viewed 35

How to use jwt in spring boot to produce access token? i use tokenstore to JDBC and also use jwtconverter bean but the token also the same format as uuid. I am stuck with this please help to resolve this issue thank in advanced. My Authorization class :

public class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter{
        private static final String SIGNING_KEY = "ABCD";
        @Autowired
        private PasswordEncoder passwordEncoder;
        @Autowired
        private DataSource dataSource;
        @Autowired
        private AuthenticationManager authenticationManager;
        @Autowired
        private TokenStore tokenStore;
        
        
        @Bean
        public JwtAccessTokenConverter jwtAccessTokenConverter() {
            final JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();
            jwtAccessTokenConverter.setSigningKey(SIGNING_KEY);
            return jwtAccessTokenConverter;
        }
        
        
        @Bean
        TokenStore jdbcTokenStore() {
            return new JdbcTokenStore(dataSource);
        }

        @Override
        public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
            security.tokenKeyAccess("permitAll()").checkTokenAccess("isAuthenticated()").passwordEncoder(passwordEncoder);

        }

        @Override
        public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
            clients.jdbc(dataSource).passwordEncoder(passwordEncoder);

        }

        @Override
        public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
            final TokenEnhancerChain tokenEnhancerChain = new TokenEnhancerChain();
            tokenEnhancerChain.setTokenEnhancers(Arrays.asList(tokenEnhancer()));
             endpoints.tokenStore(tokenStore)
                     .tokenEnhancer(tokenEnhancer())
                     .tokenEnhancer(tokenEnhancerChain)
                     .authenticationManager(authenticationManager)
                     .exceptionTranslator(new MyWebResponseExceptionTranslator());
        }
        
        @Bean
        public DefaultTokenServices tokenServices() {
            
            final DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
            defaultTokenServices.setTokenStore(tokenStore);
            defaultTokenServices.setSupportRefreshToken(true);
            defaultTokenServices.setAuthenticationManager(authenticationManager);
            defaultTokenServices.setTokenEnhancer(tokenEnhancer());
           
            return defaultTokenServices;
        }
         
        
        @Bean
        public TokenEnhancer tokenEnhancer() {
            return new CustomTokenEnhancer();
            
        }
}

My Security Config:

public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter{
     @Autowired
     private UserDetailsService userDetailsService;

        @Bean
        protected AuthenticationManager getAuthenticationManager() throws Exception {
            return super.authenticationManagerBean();
        }

        @Bean
        PasswordEncoder passwordEncoder() {
            return PasswordEncoderFactories.createDelegatingPasswordEncoder();
        }
        

        @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception {
            auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
        }

        @Override
        protected void configure(HttpSecurity http) throws Exception {

            http
            .authorizeRequests().antMatchers("/oauth/token").permitAll()
            .antMatchers("/oauth/token/logout").permitAll()
            .antMatchers("/tokens/**").permitAll()
            .anyRequest().authenticated()
            .and().formLogin().permitAll()
            .and().csrf().disable()
            .cors().configurationSource(corsConfigurationSource());
        }

        //This can be customized as required
        CorsConfigurationSource corsConfigurationSource() {
            CorsConfiguration configuration = new CorsConfiguration();
            List<String> allowOrigins = Arrays.asList("*");
            configuration.setAllowedOrigins(allowOrigins);
            configuration.setAllowedMethods(Collections.singletonList("*"));
            configuration.setAllowedHeaders(Collections.singletonList("*"));
            //in case authentication is enabled this flag MUST be set, otherwise CORS requests will fail
            configuration.setAllowCredentials(true);
            UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
            source.registerCorsConfiguration("/**", configuration);
            return source;
        }
}

The result:

{
    "access_token": "e5546d31-975b-4476-8d5d-bc48f4c1577d",
    "token_type": "bearer",
    "refresh_token": "7b785e22-c7c1-4e86-910a-09b53043769d",
    "expires_in": 2674,
    "scope": "READ WRITE",
    "role": [
        {
            "authority": "ROLE_ADMIN"
        }
    ]
}

My expected result:

{
        "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c",
        "token_type": "bearer",
        "refresh_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c",
        "expires_in": 2674,
        "scope": "READ WRITE",
        "role": [
            {
                "authority": "ROLE_ADMIN"
            }
        ]
    }
0 Answers
Related