【Spring Security OAuth2】客户端授权模式(client credentials)~校验访问令牌

前文 【Spring Security OAuth2】客户端授权模式(client credentials)~资源服务配置 我们设定 授权服务 和 资源服务 是两个单独的应用,在 资源服务 中使用RemoteTokenServicesToken进行校验。
在这里插入图片描述
从图中可以看出,ResourceServerTokenServices接口有两个实现类(DefaultTokenServicesRemoteTokenServices),而DefaultTokenServices类同时实现了AuthorizationServerTokenServicesResourceServerTokenServices接口。
当 授权服务 和 资源服务 是两个单独的应用时,资源服务端使用RemoteTokenServices校验Token,而授权服务端使用DefaultTokenServices校验Token
当 授权服务 和 资源服务 是同一个应用时,使用DefaultTokenServices校验Token
在这里插入图片描述
ResourceServerTokenServices接口定义了两个方法,一个是根据指定的Token获取认证信息,另一个是根据指定的Token获取完整的Token详情。

在请求头中设置Authorization参数
将客户端ID和客户端密钥以 : 间隔进行拼接,然后将拼接后的字符串使用 BASE64 编码与Basic拼接,便可生成 Authorization 参数的值
在这里插入图片描述
设置完参数后像授权服务发送Token校验请求

通过AccessTokenConverter接口的extractAuthentication方法从请求结果中提取认证信息
在这里插入图片描述
AccessTokenConverter接口有多个实现类,由于我们在配置资源服务时并未指定AccessTokenConverter接口的实现类,故系统使用默认的实现类DefaultAccessTokenConverter
在这里插入图片描述
如果资源服务设置了resourceId且客户端详情中也设置了可访问的资源ID,则校验客户端详情中设置的可访问资源ID集合中是否包含当前资源ID
在这里插入图片描述

若资源服务端设置了clientDetailsService则校验认证信息中可访问的范围是否包含客户端可访问范围

【Spring Security OAuth2】客户端授权模式(client credentials)~资源服务配置 一文中,我们曾在application.properties文件中设置两个属性

security.oauth2.client.clientid=app
security.oauth2.client.clientSecret=app

这两个值与 我们获取Token时设置的值无关
在这里插入图片描述
调整application.properties文件中这两个属性为

security.oauth2.client.clientid=secret
security.oauth2.client.clientSecret=secret

当我们使用app获取访问令牌并访问资源resourceA时,可以正常访问
当我们使用secret获取访问令牌并访问资源resourceA时,无法访问
在这里插入图片描述

但属性文件中设置的那两个属性,必须在oauth_client_details表中存在

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好,关于在 Spring Security OAuth2.0 客户端动态跳过认证,可以使用自定义过滤器来实现,具体实现如下: 1.自定义过滤器: ```java public class SkipTokenFilter extends OncePerRequestFilter { private AntPathMatcher antPathMatcher = new AntPathMatcher(); @Autowired private OAuth2AuthorizedClientService authorizedClientService; @Autowired private OAuth2AuthorizedClientRepository authorizedClientRepository; @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { String requestURI = request.getRequestURI(); String clientRegistrationId = "your-client-registration-id"; // 客户端注册ID if (!antPathMatcher.match("/oauth2/**", requestURI) && !antPathMatcher.match("/error", requestURI)) { OAuth2AuthorizedClient authorizedClient = authorizedClientService.loadAuthorizedClient(clientRegistrationId, getAuthentication(request), request); if (authorizedClient == null) { // 跳过认证 HttpSession session = request.getSession(false); if (session != null) { session.setAttribute(OAuth2AuthorizationContext.class.getName(), new OAuth2AuthorizationContext(null, null, clientRegistrationId)); } filterChain.doFilter(request, response); return; } request.setAttribute(OAuth2AuthorizationContext.class.getName(), new OAuth2AuthorizationContext(authorizedClient.getAccessToken(), null, clientRegistrationId)); } filterChain.doFilter(request, response); } private Authentication getAuthentication(HttpServletRequest request) { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (authentication == null) { String authorization = request.getHeader(HttpHeaders.AUTHORIZATION); if (StringUtils.isNotEmpty(authorization)) { int index = authorization.indexOf(' '); if (index > 0) { String type = authorization.substring(0, index); if ("Bearer".equalsIgnoreCase(type)) { String token = authorization.substring(index + 1); authentication = new BearerTokenAuthentication(new BearerTokenAuthenticationToken(token)); } } } } return authentication; } } ``` 2.在 Spring Security 配置中添加自定义过滤器: ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { // ... http.addFilterBefore(new SkipTokenFilter(), OAuth2AuthorizationRequestRedirectFilter.class); } } ``` 以上就是通过自定义过滤器在 Spring Security OAuth2.0 客户端动态跳过认证的实现方法,希望能对您有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值