pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-oauth2</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
</dependencies>
**ResourceServerConfig 配置类**
@Configuration
@EnableResourceServer
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
private final static String RESOURCE_ID = “resource1”;
@Override
public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
resources.resourceId(RESOURCE_ID)
.tokenServices(tokenService())
.stateless(true);
}
@Bean
public ResourceServerTokenServices tokenService() {
RemoteTokenServices remoteTokenServices = new RemoteTokenServices();
remoteTokenServices.setCheckTokenEndpointUrl(“http://localhost:8080/oauth/check_token”);
remoteTokenServices.setClientId(“client”);
remoteTokenServices.setClientSecret(“123456”);
return remoteTokenServices;
}
@Override
public void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
// 设置所有请求的资源作用域
.authorizeRequests().antMatchers(“/**”).access(“#oauth2.hasAnyScope(‘server’)”)
.anyRequest().permitAll()
.and()
// 设置session为无状态 提升效率
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
;
}
}
application.yml
# 避免和上节认证服务器端口冲突
server:
port: 8081