Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >springsecurity oauth2 端点安全源码

springsecurity oauth2 端点安全源码

作者头像
路过君
发布于 2020-06-19 09:14:33
发布于 2020-06-19 09:14:33
61200
代码可运行
举报
运行总次数:0
代码可运行

默认配置

AuthorizationServerSecurityConfigurer

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
...
// 客户端默认使用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 删除。

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

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
spring security oauth2 allowFormAuthenticationForClients原理解析
本文主要解析一下spring security oauth2中AuthorizationServerConfigurerAdapter的allowFormAuthenticationForClients的原理
code4it
2018/09/17
1.6K0
聊聊spring security oauth2的password方式的认证
本文主要来聊聊spring security oauth2的password方式的认证
code4it
2018/09/17
2.6K0
聊聊spring security oauth2的几个endpoint的认证
本文就来讲一下spring security oauth2的几个endpoint的认证
code4it
2018/09/17
4K0
Spring security oauth2的认证流程2 原
OAuth2AuthenticationProcessingFilter中作为filter拦截认证会借助tokenExtractor从request中获取token的值,将其转换为Authentication 对象。
用户1499526
2019/08/20
1.1K0
3. spring security & oauth2
解释看代码字面意思就懂了,没什么特殊的,还可以修改比如登录表单里的用户名和密码的名字,还可以添加各种登录成功之后的handler等等,写法都一样。
MasterVin
2018/08/30
1.2K0
3. spring security & oauth2
spring oauth2 authorization server 配置源码解析
路过君
2024/01/06
6960
基于SpringSecurity实现的基本认证及OAuth2
如果Spring Security位于类路径上,则所有HTTP端点上默认使用基本认证,这样就能使Web应用程序得到一定的安全保障。最为快捷的方式是在依赖中添加Spring Boot Security Starter。
愿天堂没有BUG
2022/10/28
1.2K0
基于SpringSecurity实现的基本认证及OAuth2
SpringSecurity 跨域
​ 浏览器出于安全的考虑,使用 XMLHttpRequest对象发起 HTTP请求时必须遵守同源策略,否则就是跨域的HTTP请求,默认情况下是被禁止的。 同源策略要求源相同才能正常进行通信,即协议、域名、端口号都完全一致。
用户9615083
2022/12/30
2820
4. spring-security-oauth2 server
主要就加了@EnableAuthorizationServer注解告诉spring启动Server模式,github登录跟上篇文章的代码一样,就是封装了一下,因为以前代码的注解ResourceServerProperties会和spring 的EnableAuthorizationServer冲突,加上了EnableResourceServer,配置/api/**底下的资源是需要权限的,重写AuthenticationManager这个方法很重要,目的是将web登录和oauth登录的manager共享,不然只能有一方生效,这个想了解的可以读一读源码,一时解释不太清楚。
MasterVin
2018/08/30
1.1K0
4. spring-security-oauth2 server
从零开始的Spring Security Oauth2(三)
上一篇文章中我们介绍了获取token的流程,这一篇重点分析一下,携带token访问受限资源时,内部的工作流程。 @EnableResourceServer与@EnableAuthorizationServer 还记得我们在第一节中就介绍过了OAuth2的两个核心概念,资源服务器与身份认证服务器。我们对两个注解进行配置的同时,到底触发了内部的什么相关配置呢? 上一篇文章重点介绍的其实是与身份认证相关的流程,即如果获取token,而本节要分析的携带token访问受限资源,自然便是与@EnableResource
程序猿DD
2018/03/26
1.8K0
从零开始的Spring Security Oauth2(三)
实战!Spring Boot Security+JWT前后端分离架构认证登录,居然还有人不会?
前后端分离不同于传统的web服务,无法使用session,因此我们采用JWT这种无状态机制来生成token,大致的思路如下:
爱撒谎的男孩
2023/08/28
5.1K0
实战!Spring Boot Security+JWT前后端分离架构认证登录,居然还有人不会?
OAuth2.0实战!玩转认证、资源服务异常自定义这些骚操作!
感觉如何?是不是很酸爽?很显然这返回的信息不适合前后端交互,别着急,下面介绍解决方案
爱撒谎的男孩
2023/08/28
6230
OAuth2.0实战!玩转认证、资源服务异常自定义这些骚操作!
Spring Security入门到实践(一)HTTP Basic在Spring Security中的应用原理浅析
上面的两点是应用安全的基本关注点,Spring Security存在的意义就是帮助开发者更加便捷地实现了应用的认证和授权能力。
itlemon
2020/04/03
2.4K0
Spring Security入门到实践(一)HTTP Basic在Spring Security中的应用原理浅析
我扒了半天源码,终于找到了Oauth2自定义处理结果的最佳方案!
https://github.com/macrozheng/springcloud-learning/tree/master/micro-oauth2
macrozheng
2020/08/04
3.6K0
实战!Spring Boot Security+JWT前后端分离架构登录认证!
Spring security这里就不再过多介绍了,相信大家都用过,也都恐惧过,相比Shiro而言,Spring Security更加重量级,之前的SSM项目更多企业都是用的Shiro,但是Spring Boot出来之后,整合Spring Security更加方便了,用的企业也就多了。
码猿技术专栏
2024/01/29
1.3K0
实战!Spring Boot Security+JWT前后端分离架构登录认证!
Spring Security 6.x 一文快速搞懂配置原理
Spring Security框架看似比较复杂,但说到底,框架中的各种安全功能,基本上也就是一个个Filter(javax.servlet.Filter)组成的所谓“过滤器链”实现的,这些Filter以职责链的设计模式组织起来,环环相扣,不过在刚接触Spring Security框架时不必盯着每个Filter着重去研究,我们首要的目的是学会如何对Spring Security进行配置,很多人,特别是新手,在看过官方文档中配置示例代码(如下所示)之后,在没有足够背景知识的情况下,都会对这个http.build()方法感到莫名的困惑,想要定制开发也不知从何下手,本文主要对整个Spring Security配置过程做一定的剖析,希望可以对学习Spring Sercurity框架的同学所有帮助。
fullstackyang
2024/05/27
1.6K0
Spring Security 6.x 一文快速搞懂配置原理
spring security自定义指南
AuthenticationManager接口有个实现ProviderManager相当于一个provider chain,它里头有个List providers,通过provider来实现认证。
code4it
2018/09/17
1.6K0
spring security authorization server token端点配置跨域访问
在网页端跨域访问spring-security-oauth2搭建的授权服务器,以ClientCredentials授权模式获取token,发生跨域请求失败。
路过君
2021/12/07
9580
附DEMO| 绝活!Spring Security过滤器就该这么配置
以前胖哥带大家用Spring Security过滤器实现了手机验证码认证,今天我们来改良一下验证码认证的配置方式。这绝对是绝活666,不再看、点赞一波吗?天天白嫖,晚上睡得着觉?
码农小胖哥
2022/04/06
3950
SpringSecurity 遗留小问题
​ 我们前面都是使用@PreAuthorize注解,然后在在其中使用的是hasAuthority方法进行校验。SpringSecurity还为我们提供了其它方法例如:hasAnyAuthority,hasRole,hasAnyRole等。
用户9615083
2022/12/30
4220
推荐阅读
相关推荐
spring security oauth2 allowFormAuthenticationForClients原理解析
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验