Spring Security OAuth2.0(18):资源服务测试 本章代码已分享至Gitee:https://gitee.com/lengcz/distributed-security01.git文章目录资源服务器配置编写资源配置资源服务验证token添加安全访问控制查看令牌权限问题资源服务器配置EnableResourceServer注解到一个Configuration配置类上并且必须使用ResourceServerConfigurer这个配置对象来进行配置(可以选择继承自ResourceServerConfigurerAdapter然后覆写其中的方法参数就是这个对象的实例下面是一些可以配置的属性ResourceServerSecurityConfigurer中主要包括tokenServices: ResourceServerTokenServices类的实例用来实现令牌服务。tokenStore:TokenStore类的实例指定令牌如何访问与tokenServices配置可选resourceId这个资源服务的ID这个属性是可选的但是推荐设置并在授权服务中进行验证。其他的扩展属性例如tokenExtrator令牌提取器用来提取请求中的令牌。HttpSecurity配置这个与Spring Security 类似请求匹配器用来设置需要进行保护的资源路径默认的情况下是保护资源服务的全部路径。通过http.authorizeRequests()来设置受保护的访问规则其他的自定义权限保护规则通过HttpSecurity来进行配置。EnableResourceServer 注解自动增加了一个类型为OAuth2AuthenticationProcessingFilter的过滤器链。编写资源在order模块下创一个资源RestControllerpublicclassOrderController{GetMapping(r1)PreAuthorize(hasAnyAuthority(p1))// 拥有p1权限可以访问此资源publicStringr1(){return资源1;}}配置资源服务在order模块下创建配置 ResourceServerConfigpackagecom.it2.security.distributed.order.config;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importorg.springframework.security.config.annotation.web.builders.HttpSecurity;importorg.springframework.security.config.http.SessionCreationPolicy;importorg.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;importorg.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;importorg.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;importorg.springframework.security.oauth2.provider.token.RemoteTokenServices;importorg.springframework.security.oauth2.provider.token.ResourceServerTokenServices;ConfigurationEnableResourceServerpublicclassResourceServerConfigextendsResourceServerConfigurerAdapter{publicstaticfinalStringRESOURCE_IDres1;Overridepublicvoidconfigure(ResourceServerSecurityConfigurerresources)throwsException{resources.resourceId(RESOURCE_ID)//资源id.tokenServices(tokenService())//验证令牌的服务.stateless(true);}Overridepublicvoidconfigure(HttpSecurityhttp)throwsException{http.authorizeRequests().antMatchers(/**).access(#oauth2.hasScope(all)).and().csrf().disable().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);}}验证tokenResourceServerTokenServices 是组成授权服务的另一半如果你的授权服务器和资源服务在同一个应用程序的话你可以使用DefaultTokenServices这样的话你就不用考虑关于实现所有必要接口的接口的一致性问题。如果你的资源服务器是分离开的那么你就需必须确保能够有匹配授权服务提供的ResourceServerTokenServices它知道如何对令牌进行解码。令牌解析方法使用DefaultTokenServices 在资源服务器本地配置令牌存储解码解析方式使用RemoteTokenServices 资源服务器通过HTTP 请求来解码令牌每次都请求授权服务器端点/oauth/check_token使用授权服务的/oauth/check_token 端点你需要在授权服务将这个端点暴露出去以便资源服务可以进行访问这在授权服务配置中提到过下面是一个例子已经在授权中配置了 /oauth/check_token和/oauth/token_key这两个端点Overridepublicvoidconfigure(AuthorizationServerSecurityConfigurersecurity)throwsException{security.tokenKeyAccess(permitAll())//oauth/token_key 公开.checkTokenAccess(permitAll())//oauth/check_token 公开.allowFormAuthenticationForClients();//表单认证申请令牌}添加安全访问控制*必须配置HttpSecurity ,否则会导致没有该资源权限也能访问。在order的config下添加安全访问控制importorg.springframework.context.annotation.Configuration;importorg.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;importorg.springframework.security.config.annotation.web.builders.HttpSecurity;importorg.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;ConfigurationEnableGlobalMethodSecurity(securedEnabledtrue,prePostEnabledtrue)//开启不同的方法权限注解publicclassWebSecurityConfigextendsWebSecurityConfigurerAdapter{//配置安全拦截机制protectedvoidconfigure(HttpSecuritysecurity)throwsException{security.csrf().disable().authorizeRequests().antMatchers(/r/**).authenticated()//所有的/r/**的请求必须认证通过.anyRequest().permitAll();//除此之外的请求都可以访问}}查看令牌权限http://localhost:53020/uaa/oauth/check_token问题这种方式的缺点每次请求进行鉴权时都需要请求远程接口请求量大时这种网络请求将会占用大量网络资源。