@Configuration@Order(0)@Import({ClientDetailsServiceConfiguration.class,AuthorizationServerEndpointsConfiguration.class})publicclassAuthorizationServerSecurityConfigurationextendsWebSecurityConfigurerAdapter{// 注入客制化的认证服务器配置@AutowiredprivateList<AuthorizationServerConfigurer> configurers =Collections.emptyList();// 注入客户端服务(动态代理)@AutowiredprivateClientDetailsService clientDetailsService;@AutowiredprivateAuthorizationServerEndpointsConfiguration endpoints;// 自动注入客户端信息配置器,并合并客制化配置@Autowiredpublicvoidconfigure(ClientDetailsServiceConfigurer clientDetails)throwsException{for(AuthorizationServerConfigurer configurer : configurers){
configurer.configure(clientDetails);}}@Overrideprotectedvoidconfigure(AuthenticationManagerBuilder auth)throwsException{// Over-riding to make sure this.disableLocalConfigureAuthenticationBldr = false// This will ensure that when this configurer builds the AuthenticationManager it will not attempt// to find another 'Global' AuthenticationManager in the ApplicationContext (if available),// and set that as the parent of this 'Local' AuthenticationManager.// This AuthenticationManager should only be wired up with an AuthenticationProvider// composed of the ClientDetailsService (wired in this configuration) for authenticating 'clients' only.}@Overrideprotectedvoidconfigure(HttpSecurity http)throwsException{AuthorizationServerSecurityConfigurer configurer =newAuthorizationServerSecurityConfigurer();FrameworkEndpointHandlerMapping handlerMapping = endpoints.oauth2EndpointHandlerMapping();
http.setSharedObject(FrameworkEndpointHandlerMapping.class, handlerMapping);configure(configurer);
http.apply(configurer);String tokenEndpointPath = handlerMapping.getServletPath("/oauth/token");String tokenKeyPath = handlerMapping.getServletPath("/oauth/token_key");String checkTokenPath = handlerMapping.getServletPath("/oauth/check_token");if(!endpoints.getEndpointsConfigurer().isUserDetailsServiceOverride()){UserDetailsService userDetailsService = http.getSharedObject(UserDetailsService.class);
endpoints.getEndpointsConfigurer().userDetailsService(userDetailsService);}// @formatter:off
http
.authorizeRequests().antMatchers(tokenEndpointPath).fullyAuthenticated().antMatchers(tokenKeyPath).access(configurer.getTokenKeyAccess()).antMatchers(checkTokenPath).access(configurer.getCheckTokenAccess()).and().requestMatchers().antMatchers(tokenEndpointPath, tokenKeyPath, checkTokenPath).and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER);// @formatter:on
http.setSharedObject(ClientDetailsService.class, clientDetailsService);}protectedvoidconfigure(AuthorizationServerSecurityConfigurer oauthServer)throwsException{for(AuthorizationServerConfigurer configurer : configurers){
configurer.configure(oauthServer);}}}