Tôi đang cố chia máy chủ tài nguyên khỏi máy chủ ủy quyền trong khởi động mùa xuân. Tôi có hai ứng dụng khác nhau mà tôi đang chạy riêng. Trong máy chủ ủy quyền, tôi có thể nhận được mã thông báo mang từ oauth/token nhưng khi tôi đang cố gắng truy cập vào tài nguyên (gửi mã thông báo trong tiêu đề), tôi nhận được một mã thông báo lỗi không hợp lệ. Ý định của tôi là sử dụng InMemoryTokenStore và mã thông báo mang. Bất cứ ai có thể cho tôi biết những gì là sai trong mã của tôi?Máy chủ ủy quyền chia tách oauth2 Spring-boot oauth2 và máy chủ tài nguyên
Authorization Server:
@SpringBootApplication
public class AuthorizationServer extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(AuthorizationServer.class, args);
}
@Configuration
@EnableAuthorizationServer
protected static class OAuth2Config extends AuthorizationServerConfigurerAdapter {
private TokenStore tokenStore = new InMemoryTokenStore();
@Autowired
private AuthenticationManager authenticationManager;
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints
.authenticationManager(authenticationManager)
.tokenStore(tokenStore);
}
@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
security.checkTokenAccess("hasAuthority('ROLE_USER')");
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients
.inMemory()
.withClient("user")
.secret("password")
.authorities("ROLE_USER")
.authorizedGrantTypes("password")
.scopes("read", "write")
.accessTokenValiditySeconds(1800);
}
}
Resource Server:
@SpringBootApplication
@RestController
@EnableOAuth2Resource
@EnableWebSecurity
@Configuration
public class ResourceServer extends WebSecurityConfigurerAdapter {
public static void main(String[] args){
SpringApplication.run(ResourceServer.class, args);
}
@RequestMapping("/")
public String home(){
return "Hello Resource World!";
}
@Bean
public ResourceServerTokenServices tokenService() {
RemoteTokenServices tokenServices = new RemoteTokenServices();
tokenServices.setClientId("user");
tokenServices.setClientSecret("password");
tokenServices.setTokenName("tokenName");
tokenServices.setCheckTokenEndpointUrl("http://localhost:8080/oauth/check_token");
return tokenServices;
}
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
OAuth2AuthenticationManager authenticationManager = new OAuth2AuthenticationManager();
authenticationManager.setTokenServices(tokenService());
return authenticationManager;
}
@Configuration
@EnableResourceServer
protected static class ResourceServerConfig extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http
.requestMatchers()
.antMatchers("/","/home")
.and()
.authorizeRequests()
.anyRequest().access("#oauth2.hasScope('read')");
}
@Override
public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
TokenStore tokenStore = new InMemoryTokenStore();
resources.resourceId("Resource Server");
resources.tokenStore(tokenStore);
}
}
Xin chào, tôi cũng đang cố gắng làm điều tương tự, bạn có thể vui lòng cung cấp cho tôi tài liệu tham khảo mà bạn đang theo dõi/theo dõi để thiết lập dự án –