|
17 | 17 | package org.minbox.framework.api.boot.autoconfigure.oauth; |
18 | 18 |
|
19 | 19 | import org.minbox.framework.api.boot.autoconfigure.security.ApiBootSecurityProperties; |
| 20 | +import org.minbox.framework.oauth.entrypoint.AccessTokenInvalidAuthenticationEntryPoint; |
| 21 | +import org.minbox.framework.oauth.response.AccessTokenInvalidResponse; |
| 22 | +import org.minbox.framework.oauth.response.DefaultAccessTokenInvalidResponse; |
20 | 23 | import org.minbox.framework.security.SecurityUser; |
21 | 24 | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
| 25 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
22 | 26 | import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 27 | +import org.springframework.context.annotation.Bean; |
23 | 28 | import org.springframework.context.annotation.Configuration; |
24 | 29 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
25 | 30 | import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer; |
26 | 31 | import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter; |
27 | 32 | import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer; |
| 33 | +import org.springframework.security.web.AuthenticationEntryPoint; |
28 | 34 |
|
29 | 35 | /** |
30 | 36 | * Resource server configuration |
@@ -61,8 +67,32 @@ public void configure(HttpSecurity http) throws Exception { |
61 | 67 | .antMatchers(apiBootSecurityProperties.getAuthPrefix()); |
62 | 68 | } |
63 | 69 |
|
| 70 | + /** |
| 71 | + * Configure custom serialization authentication error format |
| 72 | + * |
| 73 | + * @return The {@link DefaultAccessTokenInvalidResponse} instance |
| 74 | + * @see DefaultAccessTokenInvalidResponse |
| 75 | + */ |
| 76 | + @Bean |
| 77 | + @ConditionalOnMissingBean |
| 78 | + public AccessTokenInvalidResponse tokenInvalidResponse() { |
| 79 | + return new DefaultAccessTokenInvalidResponse(); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Instance {@link AuthenticationEntryPoint} support class |
| 84 | + * |
| 85 | + * @return {@link AccessTokenInvalidAuthenticationEntryPoint} |
| 86 | + */ |
| 87 | + @Bean |
| 88 | + public AuthenticationEntryPoint tokenInvalidAuthenticationEntryPoint() { |
| 89 | + return new AccessTokenInvalidAuthenticationEntryPoint(tokenInvalidResponse()); |
| 90 | + } |
| 91 | + |
64 | 92 | @Override |
65 | 93 | public void configure(ResourceServerSecurityConfigurer resources) throws Exception { |
66 | | - resources.resourceId(apiBootOauthProperties.getResourceId()); |
| 94 | + resources |
| 95 | + .resourceId(apiBootOauthProperties.getResourceId()) |
| 96 | + .authenticationEntryPoint(tokenInvalidAuthenticationEntryPoint()); |
67 | 97 | } |
68 | 98 | } |
0 commit comments