初识Spring Security OAuth2(三)

本文介绍了如何在Spring Security OAuth2中配置资源服务器,重点讲解了ResourceServerSecurityConfigurer和HttpSecurity的使用,以及OAuth2AuthenticationProcessingFilter在处理携带token访问受限资源时的角色。讨论了TokenExtractor的默认实现BearerTokenExtractor,以及OAuth2AuthenticationManager如何验证token。同时,提到了三种携带token的方式:Header、URL参数和表单参数。
摘要由CSDN通过智能技术生成

3.携带token访问受限资源

携带token访问资源,这涉及到@EnableResourceServer相关的资源服务的配置。

@Configuration

    @EnableResourceServer

    protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {

 

        @Override

        public void configure(ResourceServerSecurityConfigurer resources) {

            resources.resourceId(DEMO_RESOURCE_ID).stateless(true);

        }

 

        @Override

        public void configure(HttpSecurity http) throws Exception {

            http

                    .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)

                    .and()

                    .requestMatchers().anyRequest()

                    .and()

                    .anonymous()

                    .and()

                    .authorizeRequests()

                    .antMatchers("/order/**").authenticated();//配置order访问控制,必须认证过后才可以访问

        }

    }

涉及到ResourceServerSecurityConfigurer和HttpSecurity;前者与资源安全配置相关,后者与http安全配置相关。

ResourceServerSecurityConfigurer主要是这个方法configure

@Override

    public void configure(HttpSecurity http) throws Exception {

 

       AuthenticationManager oauthAuthenticationManager = oauthAuthenticationManager(http);

       resourcesServerFilter = new OAuth2AuthenticationProcessingFilter();

       resourcesServerFilter.setAuthenticationEntryPoint(authenticationEntryPoint);

       resourcesServerFilter.setAuthenticationManager(oauthAuthenticationManager);

       if (eventPublisher != null) {

          

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值