Blame view

src/main/java/etunicorn/OAuth2ServerConfiguration.java 3.66 KB
2a582f6f   Geoffrey PREUD'HOMME   Recopie de l'exem...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
  package etunicorn;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.beans.factory.annotation.Qualifier;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.context.annotation.Primary;
  import org.springframework.security.authentication.AuthenticationManager;
  import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
  import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
  import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
  import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
  import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
  import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
  import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
  import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
  import org.springframework.security.oauth2.provider.token.TokenStore;
  import org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore;
  
  /**
   * Created by geoffrey on 29/01/17.
   */
  @Configuration
  public class OAuth2ServerConfiguration {
      private static final String RESOURCE_ID = "etunicornservice";
  
      @Configuration
      @EnableResourceServer
      protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
  
          @Override
          public void configure(ResourceServerSecurityConfigurer resources) {
              resources.resourceId(RESOURCE_ID);
          }
  
          @Override
          public void configure(HttpSecurity http) throws Exception {
              http.authorizeRequests()
                      .antMatchers("/personne").authenticated();
          }
      }
  
      @Configuration
      @EnableAuthorizationServer
      protected static class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {
  
          private TokenStore tokenStore = new InMemoryTokenStore();
  
          @Autowired
          @Qualifier("authenticationManagerBean")
          private AuthenticationManager authenticationManager;
  
          @Autowired
          private CustomUserDetailsService userDetailsService;
  
          @Override
          public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
              clients
                      .inMemory()
                          .withClient("clientapp")
                              .authorizedGrantTypes("password", "refresh_token")
                              .authorities("USER")
                              .scopes("read", "write")
                              .resourceIds(RESOURCE_ID)
                              .secret("123456");
          }
  
          @Override
          public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
              endpoints
                      .tokenStore(this.tokenStore)
                      .authenticationManager(this.authenticationManager)
                      .userDetailsService(userDetailsService);
          }
  
          @Bean
          @Primary
          public DefaultTokenServices tokenServices() {
                  DefaultTokenServices tokenServices = new DefaultTokenServices();
                  tokenServices.setSupportRefreshToken(true);
                  tokenServices.setTokenStore(this.tokenStore);
                  return tokenServices;
          }
      }
  
  }