springsecurity oauth2 端点安全源码

默认配置

AuthorizationServerSecurityConfigurer

...
// 客户端默认使用BASIC AUTH认证,设置此项兼容表单认证(参数传递客户端ID、密码)
private boolean allowFormAuthenticationForClients = false;
// 默认禁止访问
private String tokenKeyAccess = "denyAll()";
// 默认禁止访问/oauth/check_token端点
private String checkTokenAccess = "denyAll()";
// 默认不阻止http请求
private boolean sslOnly = false;
...
// 注册默认认证入口
private void registerDefaultAuthenticationEntryPoint(HttpSecurity http) {
	ExceptionHandlingConfigurer<HttpSecurity> exceptionHandling = http
			.getConfigurer(ExceptionHandlingConfigurer.class);
	if (exceptionHandling == null) {
		return;
	}
	if (authenticationEntryPoint==null) {
	// 默认使用Basic 认证
		BasicAuthenticationEntryPoint basicEntryPoint = new BasicAuthenticationEntryPoint();
		basicEntryPoint.setRealmName(realm);
		authenticationEntryPoint = basicEntryPoint;
	}
	ContentNegotiationStrategy contentNegotiationStrategy = http.getSharedObject(ContentNegotiationStrategy.class);
	if (contentNegotiationStrategy == null) {
		contentNegotiationStrategy = new HeaderContentNegotiationStrategy();
	}
	MediaTypeRequestMatcher preferredMatcher = new MediaTypeRequestMatcher(contentNegotiationStrategy,
			MediaType.APPLICATION_ATOM_XML, MediaType.APPLICATION_FORM_URLENCODED, MediaType.APPLICATION_JSON,
			MediaType.APPLICATION_OCTET_STREAM, MediaType.APPLICATION_XML, MediaType.MULTIPART_FORM_DATA,
			MediaType.TEXT_XML);
	preferredMatcher.setIgnoredMediaTypes(Collections.singleton(MediaType.ALL));
	exceptionHandling.defaultAuthenticationEntryPointFor(postProcess(authenticationEntryPoint), preferredMatcher);
}
// 客户端
private ClientCredentialsTokenEndpointFilter clientCredentialsTokenEndpointFilter(HttpSecurity http) {
		ClientCredentialsTokenEndpointFilter clientCredentialsTokenEndpointFilter = new ClientCredentialsTokenEndpointFilter(
				frameworkEndpointHandlerMapping().getServletPath("/oauth/token"));
		clientCredentialsTokenEndpointFilter
				.setAuthenticationManager(http.getSharedObject(AuthenticationManager.class));
		OAuth2AuthenticationEntryPoint authenticationEntryPoint = new OAuth2AuthenticationEntryPoint();
		authenticationEntryPoint.setTypeName("Form");
		authenticationEntryPoint.setRealmName(realm);
		clientCredentialsTokenEndpointFilter.setAuthenticationEntryPoint(authenticationEntryPoint);
		clientCredentialsTokenEndpointFilter = postProcess(clientCredentialsTokenEndpointFilter);
		http.addFilterBefore(clientCredentialsTokenEndpointFilter, BasicAuthenticationFilter.class);
		return clientCredentialsTokenEndpointFilter;
	}
// 配置接口
@Override
public void configure(HttpSecurity http) throws Exception {
	
	// ensure this is initialized
	frameworkEndpointHandlerMapping();
	// 注册
	if (allowFormAuthenticationForClients) {
		clientCredentialsTokenEndpointFilter(http);
	}

	for (Filter filter : tokenEndpointAuthenticationFilters) {
		http.addFilterBefore(filter, BasicAuthenticationFilter.class);
	}

	http.exceptionHandling().accessDeniedHandler(accessDeniedHandler);
}
...

posted on   路过君  阅读(91)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2020-04-11 spring 启动时自动运行
2020-04-11 spring cloud oauth2授权服务 默认tokenService配置源码
2020-04-11 spring cloud 搭建oauth2授权服务 使用redis存储令牌
2020-04-11 spring cloud oauth2授权服务 clientDetails配置源码
2020-04-11 spring 验证框架
2020-04-11 IDEA 插件整理
2020-04-11 spring security笔记 默认登陆页面源码

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示