· 9 years ago · Nov 25, 2016, 06:14 AM
1@Configuration
2@EnableAuthorizationServer
3
4public class OAuth2AuthorizationConfig extends AuthorizationServerConfigurerAdapter {
5
6
7@Autowired
8private AuthenticationManager authenticationManager;
9
10@Autowired
11private CustomUserDetailService customUserDetailService;
12
13@Autowired
14private MongoTokenStore mongoTokenStore;
15
16@Override
17public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
18clients.inMemory()
19 .withClient("acme")
20 .secret("kksd")
21 .authorizedGrantTypes("authorization_code", "refresh_token",
22 "password").scopes("read", "write")
23 .resourceIds("ReadAndWriteResource")
24 .autoApprove(true)
25 .accessTokenValiditySeconds(120)
26 .refreshTokenValiditySeconds(1200);
27}
28
29@Override
30public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
31endpoints.authenticationManager(authenticationManager)
32 .tokenStore(mongoTokenStore)
33 .userDetailsService(customUserDetailService);
34}
35
36@Override
37public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
38oauthServer.tokenKeyAccess("permitAll()").checkTokenAccess("isAuthenticated()");
39}
40
41@Bean
42@Primary
43public DefaultTokenServices tokenServices() {
44DefaultTokenServices tokenServices = new DefaultTokenServices();
45tokenServices.setSupportRefreshToken(true);
46tokenServices.setTokenStore(mongoTokenStore);
47return tokenServices;
48}
49}
50
51@GetMapping("/user")
52public Principal user(Principal user) {
53 log.info(String.valueOf(user));
54 return user;
55}
56
57security:
58 user:
59 password: none
60 oauth2:
61 client:
62 accessTokenUri: http://localhost:9999/uaa/oauth/token
63 userAuthorizationUri: http://localhost:9999/uaa/oauth/authorize
64 clientId: acme
65 clientSecret: kksd
66
67 resource:
68 user-info-uri: http://localhost:9999:9999/uaa/user
69
70o.s.s.o.provider.endpoint.TokenEndpoint : Handling error: MappingInstantiationException, Failed to instantiate org.springframework.security.authentication.UsernamePasswordAuthenticationToken using constructor NO_CONSTRUCTOR with arguments
71
722016-11-25 12:54:28.830 DEBUG 15521 --- [nio-9999-exec-5] o.s.s.w.a.i.FilterSecurityInterceptor : Authorization successful
732016-11-25 12:54:28.830 DEBUG 15521 --- [nio-9999-exec-5] o.s.s.w.a.i.FilterSecurityInterceptor : RunAsManager did not change Authentication object
742016-11-25 12:54:28.830 DEBUG 15521 --- [nio-9999-exec-5] o.s.security.web.FilterChainProxy : /oauth/token reached end of additional filter chain; proceeding with original chain
752016-11-25 12:54:28.835 DEBUG 15521 --- [nio-9999-exec-5] .s.o.p.e.FrameworkEndpointHandlerMapping : Looking up handler method for path /oauth/token
762016-11-25 12:54:28.835 DEBUG 15521 --- [nio-9999-exec-5] .s.o.p.e.FrameworkEndpointHandlerMapping : Returning handler method [public org.springframework.http.ResponseEntity<org.springframework.security.oauth2.common.OAuth2AccessToken> org.springframework.security.oauth2.provider.endpoint.TokenEndpoint.postAccessToken(java.security.Principal,java.util.Map<java.lang.String, java.lang.String>) throws org.springframework.web.HttpRequestMethodNotSupportedException]
772016-11-25 12:54:28.837 DEBUG 15521 --- [nio-9999-exec-5] o.s.s.o.provider.endpoint.TokenEndpoint : Clearing scope of incoming token request
782016-11-25 12:54:28.842 DEBUG 15521 --- [nio-9999-exec-5] .s.s.o.p.c.AuthorizationCodeTokenGranter : Getting access token for: kksdi2388wmkwe
792016-11-25 12:54:28.893 INFO 15521 --- [nio-9999-exec-5] o.s.s.o.provider.endpoint.TokenEndpoint : Handling error: MappingInstantiationException, Failed to instantiate org.springframework.security.authentication.UsernamePasswordAuthenticationToken using constructor NO_CONSTRUCTOR with arguments
802016-11-25 12:54:28.942 DEBUG 15521 --- [nio-9999-exec-5] o.s.s.w.header.writers.HstsHeaderWriter : Not injecting HSTS header since it did not match the requestMatcher org.springframework.security.web.header.writers.HstsHeaderWriter$SecureRequestMatcher@8bcbc50
812016-11-25 12:54:28.944 DEBUG 15521 --- [nio-9999-exec-5] o.s.s.w.a.ExceptionTranslationFilter : Chain processed normally
822016-11-25 12:54:28.944 DEBUG 15521 --- [nio-9999-exec-5] s.s.w.c.SecurityContextPersistenceFilter : SecurityContextHolder now cleared, as request processing completed
83
84http://localhost:8080/login?code=PrPkG6&state=DBTWGU