Spring boot 如何使用keydepostresttemplate
我想实现一个简单的Spring引导客户端应用程序。应该访问OAauth2安全服务的。某种代理。此“代理服务”不应受到保护 我想使用Spring boot 如何使用keydepostresttemplate,spring-boot,keycloak,Spring Boot,Keycloak,我想实现一个简单的Spring引导客户端应用程序。应该访问OAauth2安全服务的。某种代理。此“代理服务”不应受到保护 我想使用keydepostresttemplate进行远程REST调用 下列文件: 到目前为止,我包含了以下依赖项: dependencyManagement { imports { mavenBom "org.keycloak.bom:keycloak-adapter-bom:3.4.0-FINAL" } } dependencies { compil
keydepostresttemplate
进行远程REST调用
下列文件:
到目前为止,我包含了以下依赖项:
dependencyManagement {
imports {
mavenBom "org.keycloak.bom:keycloak-adapter-bom:3.4.0-FINAL"
}
}
dependencies {
compile('org.springframework.boot:spring-boot-starter')
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.boot:spring-boot-starter-security')
compile('org.keycloak:keycloak-spring-boot-starter')
compile('org.keycloak:keycloak-spring-security-adapter')
}
我添加了一个配置类:
@KeycloakConfiguration
public class KeycloakWebSecurityContextConfig extends
KeycloakWebSecurityConfigurerAdapter {
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(keycloakAuthenticationProvider());
}
/**
* Defines the session authentication strategy.
*/
@Bean
@Override
protected SessionAuthenticationStrategy sessionAuthenticationStrategy() {
return new RegisterSessionAuthenticationStrategy(new SessionRegistryImpl());
}
@Bean
public KeycloakConfigResolver KeycloakConfigResolver() {
return new KeycloakSpringBootConfigResolver();
}
@Autowired
public KeycloakClientRequestFactory keycloakClientRequestFactory;
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public KeycloakRestTemplate keycloakRestTemplate() {
return new KeycloakRestTemplate(keycloakClientRequestFactory);
}
@Bean
public FilterRegistrationBean keycloakAuthenticationProcessingFilterRegistrationBean(
KeycloakAuthenticationProcessingFilter filter) {
FilterRegistrationBean registrationBean = new FilterRegistrationBean(filter);
registrationBean.setEnabled(false);
return registrationBean;
}
@Bean
public FilterRegistrationBean keycloakPreAuthActionsFilterRegistrationBean(
KeycloakPreAuthActionsFilter filter) {
FilterRegistrationBean registrationBean = new FilterRegistrationBean(filter);
registrationBean.setEnabled(false);
return registrationBean;
}
@Override
protected void configure(HttpSecurity http) throws Exception
{
super.configure(http);
http
.authorizeRequests()
.anyRequest().permitAll();
}
}
并制作了一个服务组件:
@Autowired
private KeycloakRestTemplate template;
@Value("${person-service.url}")
private String endpoint;
@RequestMapping(value = "/person", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public Collection<Person> getPerson(){
ResponseEntity<Person[]> entity = template.getForEntity(endpoint, Person[].class);
return Arrays.asList(entity.getBody());
}
我认为spring安全性起到了一定的作用,在KeyDoveAdapter有机会创建自己的SecurityContext之前创建了一个匿名AuthenticationToken
那么,如何正确启动和使用keydepostrestTemplate呢?问题可能是您(您的客户机)没有通过keydepose的身份验证,因此缺少keydepose令牌 如果您没有登录到keydape,您应该使用Spring提供的简单Rest模板,而不是keydape Rest模板
import org.springframework.web.client.RestTemplate
...
RestTemplate rt = new RestTemplate();
rt.postForObject(uri, data ,String.class);
问题可能是您(您的客户机)没有通过keydepeat的身份验证,因此缺少keydepeat令牌 如果您没有登录到keydape,您应该使用Spring提供的简单Rest模板,而不是keydape Rest模板
import org.springframework.web.client.RestTemplate
...
RestTemplate rt = new RestTemplate();
rt.postForObject(uri, data ,String.class);
当您的微服务最初被登录的用户调用时,KeyDoveRestTemplate就会工作,然后您可以从那里调用其他受保护的微服务 如果您希望微服务启动对另一个受保护微服务的调用,最好使用OAuth2RestTemplate
import org.springframework.web.client.RestTemplate
...
RestTemplate rt = new RestTemplate();
rt.postForObject(uri, data ,String.class);
下面的RestTemplate将使用KeyClope服务帐户自动登录KeyClope,并在必要时续订承载令牌:
String url = "https://keycloakserver:8888/auth/realms/testrealm/protocol/openid-connect/token";
ClientCredentialsResourceDetails resourceDetails = new ClientCredentialsResourceDetails();
resourceDetails.setGrantType(OAuth2Constants.CLIENT_CREDENTIALS);
resourceDetails.setAccessTokenUri(url);
resourceDetails.setClientId("your-micro-service-client-id");
resourceDetails.setClientSecret("p455word");
RestTemplate restTemplate = new OAuth2RestTemplate(resourceDetails);
当您的微服务最初被登录的用户调用时,KeyDoveRestTemplate就会工作,然后您可以从那里调用其他受保护的微服务 如果您希望微服务启动对另一个受保护微服务的调用,最好使用OAuth2RestTemplate
import org.springframework.web.client.RestTemplate
...
RestTemplate rt = new RestTemplate();
rt.postForObject(uri, data ,String.class);
下面的RestTemplate将使用KeyClope服务帐户自动登录KeyClope,并在必要时续订承载令牌:
String url = "https://keycloakserver:8888/auth/realms/testrealm/protocol/openid-connect/token";
ClientCredentialsResourceDetails resourceDetails = new ClientCredentialsResourceDetails();
resourceDetails.setGrantType(OAuth2Constants.CLIENT_CREDENTIALS);
resourceDetails.setAccessTokenUri(url);
resourceDetails.setClientId("your-micro-service-client-id");
resourceDetails.setClientSecret("p455word");
RestTemplate restTemplate = new OAuth2RestTemplate(resourceDetails);
你解决问题了吗?如果是,你是怎么做到的?不,我解决不了。我的计划是在github上制作一个小的演示应用程序,并打开一个问题。但是时间不多了。你解决问题了吗?如果是,你是怎么做到的?不,我解决不了。我的计划是在github上制作一个小的演示应用程序,并打开一个问题。但是时间不多了,我不明白。当我使用vanilla RestTemplate时,它如何与KeyClope对话并接收accesstoken?我的意思是,我成功地编写了一个RequestInterceptor来实现这一点,但我认为keydepostresttemplate的设计和创建是为了不使用自定义的restmplate。我很困惑…我在未经身份验证的情况下使用KeyClope Rest模板时出错。您可能在登录之前调用“getPerson()”方法。您应该使用香草RestTemplate进行登录。在身份验证之后,您应该使用keydape Rest模板。这对我来说是可行的。但是你是对的,除了登录(和注册)之外,你应该使用keydeporest模板。很遗憾,这不适用于我的问题。我没有任何用户可以登录的web前端。我正在开发两个微服务,它们应该与安全的第三方应用程序进行通信。您可以通过keydove管理客户端获得java中的token/a会话,然后可以使用keydove Rest模板。您可以使用测试用户登录。我不明白。当我使用vanilla RestTemplate时,它如何与KeyClope对话并接收accesstoken?我的意思是,我成功地编写了一个RequestInterceptor来实现这一点,但我认为keydepostresttemplate的设计和创建是为了不使用自定义的restmplate。我很困惑…我在未经身份验证的情况下使用KeyClope Rest模板时出错。您可能在登录之前调用“getPerson()”方法。您应该使用香草RestTemplate进行登录。在身份验证之后,您应该使用keydape Rest模板。这对我来说是可行的。但是你是对的,除了登录(和注册)之外,你应该使用keydeporest模板。很遗憾,这不适用于我的问题。我没有任何用户可以登录的web前端。我正在开发两个微服务,它们应该与安全的第三方应用程序进行通信。您可以通过keydove管理客户端获得java中的token/a会话,然后可以使用keydove Rest模板。您可以使用测试用户登录。现在有必要添加
org.springframework.security.oauth:spring-security-oauth2
dependency。我在本文的帮助下解决了这个问题:创建了类OAuthrestTempalteConfiguler,配置为从KeyClope获取访问令牌。然后我只需要注入OAuthRestTemplate来调用KeyClope安全web服务。现在需要添加org.springframework.security.oauth:spring-security-oauth2
dependency。我在本文的帮助下解决了这个问题:创建了类OAuthRestTempalteConfiguler,通过配置从keydepose获取访问令牌。然后我只需要注入OAuthRestTemplate来调用KeyClope安全web服务。