前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >springsecurity oauth2 端点安全源码

springsecurity oauth2 端点安全源码

作者头像
路过君
发布2020-06-19 17:14:33
5370
发布2020-06-19 17:14:33
举报

默认配置

AuthorizationServerSecurityConfigurer

代码语言:javascript
复制
...
// 客户端默认使用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);
}
...
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-03-26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 默认配置
    • AuthorizationServerSecurityConfigurer
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档