启动应用程序:
@SpringBootApplication
@EnableZuulProxy
public class ZuulServer {
public static void main(String[] args) {
new SpringApplicationBuilder(ZuulServer.class).web(true).run(args);
}
}
Run Code Online (Sandbox Code Playgroud)
我的YAML文件是这样的:
server:
port:8080
spring:
application:
name: zuul
eureka:
client:
enabled: true
serviceUrl:
defaultZone: http://localhost:8761/eureka/
zuul:
proxy:
route:
springapp: /springapp
Run Code Online (Sandbox Code Playgroud)
我有一个名为springapp的微服务应用程序(在端口8081上),并有一些休息服务.以下是我的客户端UI应用:
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script type="text/javascript" src="js/libs/jquery/jquery.min.js" ></script>
</head>
<body>
<script type="text/javascript">
$.ajax({
url: 'http://localhost:8080/zuul/springapp/departments',
type: 'GET'
}).done(function (data) {
consoe.log(data);
document.write(data);
});
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
但我得到了一个
XMLHttpRequest cannot load http://localhost:8080/zuul/springapp/departments. No
'Access-Control-Allow-Origin' header is …Run Code Online (Sandbox Code Playgroud) zookeeper与spring cloud配置服务器有什么区别?它们都将配置存储在服务器中并使其可供客户端使用.
应该何时使用另一个?
我有一个使用Spring Cloud Config的Spring Boot应用程序,但我想在Spring Boot apps bootstrap.yml文件中加密Spring Cloud Config密码.有没有办法做到这一点?以下是一个例子.
Spring Boot应用程序bootstrap.yml
spring:
cloud:
config:
uri: http://locahost:8888
username: user
password: '{cipher}encryptedpassword'
Run Code Online (Sandbox Code Playgroud) 我看到的大多数示例都是一个服务的路由定义.
所以这样的事情:
zuul:
routes:
myserver:
path: /mypath/**
Run Code Online (Sandbox Code Playgroud)
让我们说我想将几条路由路由到一个服务,所以实际上它会是这样的:
zuul:
routes:
myserver:
path: /mypath/**, /anotherpath/**
Run Code Online (Sandbox Code Playgroud)
配置文件中不允许这样做,也不允许两次使用相同的路由名称.有没有真正的方法来做到这一点?
我想知道是否有人举例说明如何使用Spring Cloud Security(使用OAuth2)实现"令牌交换"技术.
目前,我已经在微服务环境中使用ZuulProxy实现了"令牌中继"技术,以"中继"OAuth2令牌并实现SSO.这很好,但暗示每个微服务使用相同的clientId(在ZuulProxy设置中指定为ZuulProxy仅使用authorization_code grant类型和clientId提供的中继).但是,对于微服务内部调用,我想"交换"令牌.这意味着在某些情况下,ZuulProxy中继的令牌不是我需要用来验证/授权微服务A作为微服务B的客户端的令牌.
Spring Cloud参考文档目前说:"基于Spring Boot和Spring Security OAuth2,我们可以快速创建实现常见模式的系统,如单点登录,令牌中继和令牌交换." (http://cloud.spring.io/spring-cloud-security/spring-cloud-security.html)
我想在参考文档中使用"Token Exchange"它们意味着OAuth2的这个扩展的实现,在本规范中解释,这基本上是我需要的:https: //tools.ietf.org/html/draft-ietf- OAuth的令牌交换-03
正如我所说,我了解如何使用SSO和令牌中继,但我无法在参考文档中看到有关如何实现"令牌交换"的进一步说明.我也无法找到实现示例.
有谁知道在哪里可以找到更多信息或示例?
非常感谢!
我正在尝试使用JWT实现spring AuthorizationServer.我能够生成JWT令牌并登录,直到我将BCrypt添加到混音中.现在,当我尝试登录时,我从API获得"Bad credentials".
OAuth2Configuration.java
@Configuration
@EnableAuthorizationServer
public class OAuth2Configuration extends AuthorizationServerConfigurerAdapter {
private DataSource dataSource;
private AuthenticationManager authenticationManager;
private BCryptPasswordEncoder passwordEncoder;
public OAuth2Configuration(AuthenticationManager authenticationManager) {
this.authenticationManager = authenticationManager;
this.dataSource = new Jdbc3PoolingDataSource();
this.passwordEncoder = new BCryptPasswordEncoder();
}
@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
security.passwordEncoder(passwordEncoder);
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("api-client")
.secret("verysecretivesecret")
.scopes("READ", "WRITE", "DELETE")
.authorizedGrantTypes("implicit", "refresh_tokens", "password", "authorization_code");
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authorizationCodeServices(authorizationCodeServices())
.tokenStore(tokenStore())
.tokenEnhancer(jwtTokenEnhancer())
.authenticationManager(authenticationManager);
}
@Bean …Run Code Online (Sandbox Code Playgroud) java spring spring-security spring-security-oauth2 spring-cloud
java spring spring-cloud netflix-eureka spring-cloud-netflix
免责声明:老实说,我尝试在 google/github 上搜索,扫描了OAuth2 迁移指南,但找不到答案,所以我们开始吧。
来自 spring-cloud-security 项目的org.springframework.cloud.security.oauth2.client.feign.OAuth2FeignRequestInterceptor获取 OAuth2 令牌并将其设置为对客户端调用者透明的 Feign 的 RequestTemplate。
但是,它依赖于已弃用的OAuth2ClientContext类,该类引用了前面提到的迁移指南,该指南仍然说
对于其他流,需要构造和公开 OAuth2ClientContext 实例。
所以知道几件事会很好:
1.它真的被弃用了,或者只是它的用法应该改变(至少在某些情况下)?
2. 如果是前者 - 正确的选择是什么?
3. 是否有计划OAuth2FeignRequestInterceptor从使用已弃用的类迁移?
我有以下Spring cloud config application.yml:
spring:
application:
name: configserver
cloud:
config:
server:
git:
uri: https://xyz@bitbucket.org/xyz/microservices-configs.git
username: xyz
password: xyz
basedir: target/configs
server:
port: 8881
Run Code Online (Sandbox Code Playgroud)
以下是我bootstrap.yml的用户微服务:
spring:
application:
name: userservice
cloud:
config:
uri: http://localhost:8881/
Run Code Online (Sandbox Code Playgroud)
场景 - 1
当我在浏览器中点击配置服务器时:
http://localhost:8881/development/userservice-development.yml
它正确地提供文件.当我看到basedir目标/配置时,我看到:
- userservice.yml
- gateway.yml
Run Code Online (Sandbox Code Playgroud)
正是我想要的,因为我只在开发分支中添加了这两个文件.
场景 - 2
当我使用以下命令运行userservice微服务项目时:
mvn clean spring-boot:run -Dspring.profiles.active=development
它从git中获取正确的文件,但它从master分支结账!但不是我期待的开发分支.我期待对吗?(仅供参考我在master分支中有开发和生产yml)
所以问题是,我们如何使用配置服务器?是否有任何配置我们可以设置为仅从该特定分支获取yml?我认为我们需要设置一些标签,因为根据文档,默认标签是master.任何人都可以让我知道我们如何在上述场景中设置标签?
我使用以下Maven依赖项来自动配置所有必要的参数,以使我的项目在AWS上运行:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-aws</artifactId>
<version>1.2.2.RELEASE</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
我没有任何关键功能,具体取决于AWS,它只是在运行时从S3加载一些文件.因此,在本地开发(以及测试)期间,我不需要任何AWS自动配置.
我在本地运行时得到的逻辑错误是:
...
Caused by: java.lang.IllegalStateException: There is no EC2 meta data available, because the application is not running in the EC2 environment. Region detection is only possible if the application is running on a EC2 instance
at org.springframework.util.Assert.state(Assert.java:392) ~[spring-core-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.cloud.aws.core.region.Ec2MetadataRegionProvider.getRegion(Ec2MetadataRegionProvider.java:39) ~[spring-cloud-aws-core-1.2.2.RELEASE.jar:1.2.2.RELEASE]
at org.springframework.cloud.aws.core.config.AmazonWebserviceClientFactoryBean.createInstance(AmazonWebserviceClientFactoryBean.java:98) ~[spring-cloud-aws-core-1.2.2.RELEASE.jar:1.2.2.RELEASE]
at org.springframework.cloud.aws.core.config.AmazonWebserviceClientFactoryBean.createInstance(AmazonWebserviceClientFactoryBean.java:44) ~[spring-cloud-aws-core-1.2.2.RELEASE.jar:1.2.2.RELEASE]
...
Run Code Online (Sandbox Code Playgroud)
对于测试和本地开发,是否有一个干净,可行的解决方案?
spring-cloud ×10
spring ×7
java ×4
spring-boot ×4
netflix-zuul ×2
cloud ×1
config ×1
git ×1
maven ×1
spring-mvc ×1