· 9 years ago · Nov 25, 2016, 03:28 AM
1{
2 "timestamp": 1480042650103,
3 "status": 401,
4 "error": "Unauthorized",
5 "message": "Bad credentials",
6 "path": "/oauth/token"
7}
8
9@Configuration
10@EnableAuthorizationServer
11@EnableResourceServer
12public class AuthorizationServerConfiguration {
13
14 @Configuration
15 @EnableResourceServer
16 protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
17
18 @Autowired
19 private TokenStore tokenStore;
20
21
22 @Override
23 public void configure(ResourceServerSecurityConfigurer resources) {
24 resources.tokenStore(tokenStore);
25 }
26
27 @Override
28 public void configure(HttpSecurity http) throws Exception {
29 http
30 .anonymous().disable()
31 .authorizeRequests().anyRequest().authenticated();
32 }
33 }
34
35 @Configuration
36 @EnableAuthorizationServer
37 protected static class OAuth2ServerConfiguration extends AuthorizationServerConfigurerAdapter {
38
39 @Autowired
40 private AuthenticationManager authenticationManager;
41
42 @Autowired
43 private DataSource dataSource;
44
45 private PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
46
47 @Autowired
48 private CustomUserDetailService customUserDetailService;
49
50 @Bean
51 public JdbcTokenStore tokenStore() {
52 return new JdbcTokenStore(dataSource);
53 }
54
55 @Bean
56 protected AuthorizationCodeServices authorizationCodeServices() {
57 return new JdbcAuthorizationCodeServices(dataSource);
58 }
59
60 @Override
61 public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
62 security.passwordEncoder(passwordEncoder);
63 }
64
65 @Override
66 public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
67 endpoints
68 .authorizationCodeServices(authorizationCodeServices())
69 .authenticationManager(authenticationManager)
70 .tokenStore(tokenStore())
71 .approvalStoreDisabled()
72 .userDetailsService(customUserDetailService);
73 }
74
75 @Override
76 public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
77 clients
78 .jdbc(dataSource);
79 }
80
81 }
82}
83
84@Service
85public class CustomUserDetailService
86 implements UserDetailsService {
87
88 @Override
89 public UserDetailsService loadUserByUsername(String username) throws UsernameNotFoundException {
90 // I tested this logic and works fine so i avoid this lines
91 return userDetailsService;
92 }
93}
94
95security.oauth2.client.client-id=my-trusted-client
96security.oauth2.client.client-secret=secret
97security.oauth2.client.authorized-grant-types=password,refresh_token,authorization_code,implicit
98security.oauth2.client.scope=read,write,trust
99security.oauth2.client.resource-ids=oauth2-resource
100security.oauth2.client.access-token-validity-seconds=120
101security.oauth2.client.refresh-token-validity-seconds=600