【发布时间】:2017-11-22 13:34:57
【问题描述】:
我用 springboot 1.4 创建了多个微服务。上周我决定实施 oauth2 授权服务。我的计划是这样的,
每个请求都应该由Eureka注册的zuul网关处理。
所以eureka会调用新的Authorization服务来获取access_token。
我发现的问题是我可以直接从另一个端口(8081)中运行的授权服务器获取 JWT 访问令牌。当我尝试通过 zuul 网关获取 jwt 令牌时,不幸的是我得到了一个空字符串。
请看一下我的网关配置
application.yml (zuul-gateway)
zuul:
routes:
static:
path: /static/**
uaa:
path: /uaa/**
sensitive-headers:
serviceId: microservice-security-oauth2-server
users:
path: /users/**
serviceId: microservice-core-user
security:
basic:
enabled: false
zuul的应用类是
@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
@ComponentScan(basePackages={"com.configuration","com.zullfilter"})
public class ZullGatewayServerApplication {
public static void main(String[] args) {
SpringApplication.run(ZullGatewayServerApplication.class, args);
}
}
授权服务器配置类是
@Configuration
@EnableAuthorizationServer
public class OAuth2AuthorizationConfiguration extends AuthorizationServerConfigurerAdapter {
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private Environment environment;
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.tokenStore(tokenStore())
.tokenEnhancer(jwtTokenEnhancer())
.authenticationManager(authenticationManager);
}
@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
security.tokenKeyAccess("permitAll()")
.checkTokenAccess("isAuthenticated()");
}
@Bean
public TokenStore tokenStore() {
return new JwtTokenStore(jwtTokenEnhancer());
}
@Bean
protected JwtAccessTokenConverter jwtTokenEnhancer() {
String pwd = environment.getProperty("keystore.password");
KeyStoreKeyFactory keyStoreKeyFactory = new KeyStoreKeyFactory(
new ClassPathResource("jwt.jks"),
pwd.toCharArray());
JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
converter.setKeyPair(keyStoreKeyFactory.getKeyPair("jwt"));
return converter;
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("service-account-1")
.secret("service-account-1-secret")
.authorizedGrantTypes("client_credentials")
.scopes("resource-server-read", "resource-server-write")
.accessTokenValiditySeconds(6000);
}
}
我正在获取 access_token 直接向
请求 curl service-account-1:service-account-1-secret@localhost:8081/uaa/oauth/token -d grant_type=client_credentials
但是当我尝试使用 zuul 代理时,我得到一个空字符串
curl service-account-1:service-account-1-secret@localhost:8765/uaa/oauth/token -d grant_type=client_credentials
我正在使用 zuul 网关版本 1.3 和弹簧靴 1.4
如果有人之前遇到过这个问题,请告诉我
【问题讨论】:
-
有没有可能已经解决了这个问题?...解决了什么问题?
标签: spring-boot oauth-2.0 microservices netflix-zuul