• spring cloud笔记 oauth2授权服务 clientDetails配置源码


    clientDetails

    @EnableAuthorizationServer

    ...
    @Import({... AuthorizationServerSecurityConfiguration.class})
    ...
    

    AuthorizationServerSecurityConfiguration

    ...
    // 引入clientDetails配置
    @Import({ ClientDetailsServiceConfiguration.class... })
    ...
    // 注入客制实现的AuthorizationServerConfigurerAdapter
    @Autowired 
    private List<AuthorizationServerConfigurer> configurers = Collections.emptyList();
    @Autowired
    private ClientDetailsService clientDetailsService;
    @Autowired
    public void configure(ClientDetailsServiceConfigurer clientDetails) throws Exception {
    	for (AuthorizationServerConfigurer configurer : configurers) {
    		configurer.configure(clientDetails); // 此处完成clientDetailsService配置
    	}
    }
    ...
    

    ClientDetailsServiceConfiguration

    @Configuration
    public class ClientDetailsServiceConfiguration {
    
    	@SuppressWarnings("rawtypes")
    	private ClientDetailsServiceConfigurer configurer = new ClientDetailsServiceConfigurer(new ClientDetailsServiceBuilder());
    	
    	@Bean
    	public ClientDetailsServiceConfigurer clientDetailsServiceConfigurer() {
    		return configurer;
    	}
    
    	@Bean
    	@Lazy
    	@Scope(proxyMode=ScopedProxyMode.INTERFACES)
    	public ClientDetailsService clientDetailsService() throws Exception {
    		return configurer.and().build();
    	}
    
    }
    

    ClientDetailsServiceConfigurer

    ...
    // 替换为自定义客户端信息服务
    public ClientDetailsServiceBuilder<?> withClientDetails(ClientDetailsService clientDetailsService) throws Exception {
    setBuilder(getBuilder().clients(clientDetailsService));
    	return this.and();
    }
    // 替换为内存客户端信息服务
    public InMemoryClientDetailsServiceBuilder inMemory() throws Exception {
    	InMemoryClientDetailsServiceBuilder next = getBuilder().inMemory();
    	setBuilder(next);
    	return next;
    }
    // 替换为JDBC客户端信息服务
    public JdbcClientDetailsServiceBuilder jdbc(DataSource dataSource) throws Exception {
    	JdbcClientDetailsServiceBuilder next = getBuilder().jdbc().dataSource(dataSource);
    	setBuilder(next);
    	return next;
    }
    ...
    

    passwordEncoder

    AuthorizationServerConfigurerAdapter

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        security.passwordEncoder(passwordEncoder);
    }
    

    AuthorizationServerSecurityConfigurer

    @Override
    public void init(HttpSecurity http) throws Exception {
    ...
    // 根据有无配置编码器实例化ClientDetailsUserDetailsService
    	if (passwordEncoder != null) {
    		ClientDetailsUserDetailsService clientDetailsUserDetailsService = new ClientDetailsUserDetailsService(clientDetailsService());
    		clientDetailsUserDetailsService.setPasswordEncoder(passwordEncoder());
    		http.getSharedObject(AuthenticationManagerBuilder.class)
    				.userDetailsService(clientDetailsUserDetailsService)
    				.passwordEncoder(passwordEncoder());
    	}
    	else {
    		http.userDetailsService(new ClientDetailsUserDetailsService(clientDetailsService()));
    	}
    }
    
  • 相关阅读:
    使用python-docx生成Word文档
    python 日期格式转换
    Android开发:关于WebView
    用 jQuery.ajaxSetup 实现对请求和响应数据的过滤
    Mysql 命令大全
    旋转木马的小效果!
    CSS3 background-image背景图片相关介绍
    PHP的高效IOC框架——CanoeDI
    CSS3与页面布局学习总结(一)——概要、选择器、特殊性与刻度单位
    PHP入门介绍与环境配置
  • 原文地址:https://www.cnblogs.com/luguojun/p/14294804.html
Copyright © 2020-2023  润新知