赞
踩
一、配置入口: 1. AuthorizationServerEndpointsConfiguration类:
生成AuthorizationEndpoint、TokenEndpoint、CheckTokenEndpoint、TokenKeyEndpoint 、
WhitelabelApprovalEndpoint、WhitelabelErrorEndpoint Bean 端点。 通过endpoints.getDefaultAuthorizationServerTokenServices();完成AuthorizationServerEndpointsConfigurer的初始化生成相关的对象。
- @Import(TokenKeyEndpointRegistrar.class)
- public class AuthorizationServerEndpointsConfiguration {
-
- //配置导出给全局使用
- private AuthorizationServerEndpointsConfigurer endpoints = new AuthorizationServerEndpointsConfigurer();
-
- @Autowired
- private ClientDetailsService clientDetailsService;
-
- @Autowired
- private List<AuthorizationServerConfigurer> configurers = Collections.emptyList();
-
- // AuthorizationServerEndpointsConfigurer 注入到AuthorizationServerConfigurer
- // 达到引用目的
- @PostConstruct
- public void init() {
- for (AuthorizationServerConfigurer configurer : configurers) {
- try {
- configurer.configure(endpoints);
- } catch (Exception e) {
- throw new IllegalStateException("Cannot configure enpdoints", e);
- }
- }
- endpoints.setClientDetailsService(clientDetailsService);
- }
-
- // AuthorizationServerEndpointsConfigurer endpoints 作为一个beean 存在
- @Bean
- public FactoryBean<AuthorizationServerTokenServices> defaultAuthorizationServerTokenServices() {
- return new AuthorizationServerTokenServicesFactoryBean(endpoints);
- }
- //端点的处理入口
- @Bean
- public FrameworkEndpointHandlerMapping oauth2EndpointHandlerMapping() throws Exception {
- return getEndpointsConfigurer().getFrameworkEndpointHandlerMapping();
- }
- }
2. 自定义EndPoint: TokenKeyEndpoint.class注册进bean容器就可以。
- @Component
- protected static class TokenKeyEndpointRegistrar implements BeanDefinitionRegistryPostProcessor {
-
- private BeanDefinitionRegistry registry;
-
- @Override
- public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
- String[] names = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(beanFactory,
- JwtAccessTokenConverter.class, false, false);
- if (names.length > 0) {
- BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(TokenKeyEndpoint.class);
- builder.addConstructorArgReference(names[0]);
- registry.registerBeanDefinition(TokenKeyEndpoint.class.getName(), builder.getBeanDefinition());
- }
- }
-
- @Override
- public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
- this.registry = registry;
- }
-
- }
二、实际配置AuthorizationServerEndpointsConfigurer类,(容器)
- public final class AuthorizationServerEndpointsConfigurer {
-
- private AuthorizationServerTokenServices tokenServices;
-
- private ConsumerTokenServices consumerTokenServices;
-
- private AuthorizationCodeServices authorizationCodeServices;
-
- private ResourceServerTokenServices resourceTokenServices;
-
- private TokenStore tokenStore;
-
- private TokenEnhancer tokenEnhancer;
-
- private AccessTokenConverter accessTokenConverter;
-
- private ApprovalStore approvalStore;
-
- private TokenGranter tokenGranter;
-
- private OAuth2RequestFactory requestFactory;
-
- private OAuth2RequestValidator requestValidator;
-
- private UserApprovalHandler userApprovalHandler;
-
- private AuthenticationManager authenticationManager;
-
- private ClientDetailsService clientDetailsService;
-
- private String prefix;
-
- private Map<String, String> patternMap = new HashMap<String, String>();
-
- private Set<HttpMethod> allowedTokenEndpointRequestMethods = new HashSet<HttpMethod>();
-
- private FrameworkEndpointHandlerMapping frameworkEndpointHandlerMapping;
-
- private boolean approvalStoreDisabled;
-
- private List<Object> interceptors = new ArrayList<Object>();
-
- private DefaultTokenServices defaultTokenServices;
-
- private UserDetailsService userDetailsService;
-
- private boolean tokenServicesOverride = false;
-
- private boolean userDetailsServiceOverride = false;
-
- private boolean reuseRefreshToken = true;
-
- private WebResponseExceptionTranslator<OAuth2Exception> exceptionTranslator;
-
- private RedirectResolver redirectResolver;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。