1+ package com .cloudsecurity .auth .config ;
2+
3+ import java .security .KeyPair ;
4+
5+ import org .springframework .beans .factory .annotation .Autowired ;
6+ import org .springframework .beans .factory .annotation .Value ;
7+ import org .springframework .context .annotation .Bean ;
8+ import org .springframework .context .annotation .Configuration ;
9+ import org .springframework .core .annotation .Order ;
10+ import org .springframework .core .io .Resource ;
11+ import org .springframework .security .core .userdetails .UserDetailsService ;
12+ import org .springframework .security .oauth2 .config .annotation .configurers .ClientDetailsServiceConfigurer ;
13+ import org .springframework .security .oauth2 .config .annotation .web .configuration .AuthorizationServerConfigurerAdapter ;
14+ import org .springframework .security .oauth2 .config .annotation .web .configuration .EnableAuthorizationServer ;
15+ import org .springframework .security .oauth2 .config .annotation .web .configurers .AuthorizationServerEndpointsConfigurer ;
16+ import org .springframework .security .oauth2 .provider .token .store .JwtAccessTokenConverter ;
17+ import org .springframework .security .oauth2 .provider .token .store .KeyStoreKeyFactory ;
18+
19+ @ Configuration
20+ @ EnableAuthorizationServer
21+ @ Order (6 )
22+ public class AuthServerConfigurer
23+ extends
24+ AuthorizationServerConfigurerAdapter {
25+
26+ @ Value ("${jwt.certificate.store.file}" )
27+ private Resource keystore ;
28+
29+ @ Value ("${jwt.certificate.store.password}" )
30+ private String keystorePassword ;
31+
32+ @ Value ("${jwt.certificate.key.alias}" )
33+ private String keyAlias ;
34+
35+ @ Value ("${jwt.certificate.key.password}" )
36+ private String keyPassword ;
37+
38+ @ Autowired
39+ private UserDetailsService userDetailsService ;
40+
41+ @ Override
42+ public void configure (
43+ ClientDetailsServiceConfigurer clients )
44+ throws Exception {
45+ clients
46+ .inMemory ()
47+ .withClient ("authserver" )
48+ .secret ("passwordforauthserver" )
49+ .redirectUris ("http://localhost:8080/" )
50+ .authorizedGrantTypes ("authorization_code" ,
51+ "refresh_token" )
52+ .scopes ("myscope" )
53+ .autoApprove (true )
54+ .accessTokenValiditySeconds (30 )
55+ .refreshTokenValiditySeconds (1800 );
56+ }
57+
58+ @ Override
59+ public void configure (
60+ AuthorizationServerEndpointsConfigurer endpoints )
61+ throws Exception {
62+ endpoints
63+ .accessTokenConverter (jwtAccessTokenConverter ())
64+ .userDetailsService (userDetailsService );
65+ }
66+
67+ @ Bean
68+ public JwtAccessTokenConverter jwtAccessTokenConverter () {
69+ KeyStoreKeyFactory keyStoreKeyFactory = new KeyStoreKeyFactory (
70+ keystore , keystorePassword .toCharArray ());
71+ KeyPair keyPair = keyStoreKeyFactory .getKeyPair (
72+ keyAlias , keyPassword .toCharArray ());
73+ JwtAccessTokenConverter converter = new JwtAccessTokenConverter ();
74+ converter .setKeyPair (keyPair );
75+ return converter ;
76+ }
77+ }
0 commit comments