|
| 1 | +package org.baeldung.config; |
| 2 | + |
| 3 | +import org.springframework.beans.factory.annotation.Autowired; |
| 4 | +import org.springframework.beans.factory.annotation.Qualifier; |
| 5 | +import org.springframework.context.annotation.Bean; |
| 6 | +import org.springframework.security.authentication.AuthenticationManager; |
| 7 | +import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer; |
| 8 | +import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter; |
| 9 | +import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer; |
| 10 | +import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer; |
| 11 | +import org.springframework.security.oauth2.provider.token.TokenStore; |
| 12 | +import org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore; |
| 13 | + |
| 14 | +//@Configuration |
| 15 | +//@EnableAuthorizationServer |
| 16 | +public class OAuth2AuthorizationServerConfig1 extends AuthorizationServerConfigurerAdapter { |
| 17 | + |
| 18 | + @Autowired |
| 19 | + @Qualifier("authenticationManagerBean") |
| 20 | + private AuthenticationManager authenticationManager; |
| 21 | + |
| 22 | + // |
| 23 | + |
| 24 | + @Override |
| 25 | + public void configure(final AuthorizationServerSecurityConfigurer oauthServer) throws Exception { |
| 26 | + oauthServer.tokenKeyAccess("permitAll()").checkTokenAccess("isAuthenticated()"); |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public void configure(final ClientDetailsServiceConfigurer clients) throws Exception { // @formatter:off |
| 31 | + clients.inMemory() |
| 32 | + .withClient("fooClientIdPassword") |
| 33 | + .secret("secret") |
| 34 | + .authorizedGrantTypes("password", "authorization_code", "refresh_token") |
| 35 | + .scopes("foo", "read", "write") |
| 36 | + .accessTokenValiditySeconds(3600) // 1 hour |
| 37 | + .refreshTokenValiditySeconds(2592000) // 30 days |
| 38 | + ; |
| 39 | + } // @formatter:on |
| 40 | + |
| 41 | + @Override |
| 42 | + public void configure(final AuthorizationServerEndpointsConfigurer endpoints) throws Exception { |
| 43 | + endpoints.tokenStore(tokenStore()).authenticationManager(authenticationManager); |
| 44 | + } |
| 45 | + |
| 46 | + @Bean |
| 47 | + public TokenStore tokenStore() { |
| 48 | + return new InMemoryTokenStore(); |
| 49 | + } |
| 50 | + |
| 51 | +} |
0 commit comments