我已将Spring Boot配置为使用oAuth2与keycloak连接,然后获取用户信息。使用邮递员时一切正常,但是当我尝试从角度使用api时,这是不正确的。
ERROR错误:未捕获(承诺):HttpErrorResponse:{“ headers”:{“ normalizedNames”:{},“ lazyUpdate”:null,“ headers”:{}},“ status”:0,“ statusText” :“”未知错误“,” url“:null,” ok“:false,”名称“:” HttpErrorResponse“,”消息“:”对(未知url)的Http失败响应:0未知错误“,”错误“:{ “ isTrusted”:true}}
EDIT1:
Http配置:
@Override
public void configure(HttpSecurity http) throws Exception {
super.configure(http);
http.csrf().disable().cors().and()
.authorizeRequests()
.antMatchers("/*.js", "/*.css","/*.woff2").permitAll()
.anyRequest().authenticated().and().formLogin().disable()
.oauth2Login()
.loginPage(DEFAULT_AUTHORIZATION_REQUEST_BASE_URI + "/" + realm);
Cors配置:
@Bean
public CorsConfigurationSource corsConfigurationSource() {
final CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(Arrays.asList("*"));
configuration.setAllowedMethods(Arrays.asList("*"));
configuration.setAllowCredentials(true);
configuration.setAllowedHeaders(Arrays.asList("*"));
final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}
keycloak和oauth2的Application.properties:
kc.base-url=http://localhost:8180/auth
kc.realm=my-realm
kc.realm-url=${kc.base-url}/realms/${kc.realm}
spring.security.oauth2.client.registration.my-realm.client-id=clientId
spring.security.oauth2.client.registration.my-realm.client-name=clientId
spring.security.oauth2.client.registration.my-realm.client-secret=12345678-1234-1234-1234-123456723
spring.security.oauth2.client.registration.my-realm.provider=keycloak
spring.security.oauth2.client.registration.my-realm.authorization-grant-type=authorization_code
spring.security.oauth2.client.registration.my-realm.scope=email,profile,openid
spring.security.oauth2.client.registration.my-realm.redirect-uri-template=http://localhost:8080/login/oauth2/code/${kc.realm}
spring.security.oauth2.client.provider.keycloak.authorization-uri=${kc.realm-url}/protocol/openid-connect/auth
spring.security.oauth2.client.provider.keycloak.jwk-set-uri=${kc.realm-url}/protocol/openid-connect/certs
spring.security.oauth2.client.provider.keycloak.user-info-uri=${kc.realm-url}/protocol/openid-connect/userinfo
spring.security.oauth2.client.provider.keycloak.token-uri=${kc.realm-url}/protocol/openid-connect/token
spring.security.oauth2.client.provider.keycloak.user-name-attribute=email
角度要求:
public getUserDetails() {
return this.httpClient.get<UserDetails>(environment.backendServerAddress + '/user/getUserCache')
.map(response => response).toPromise();}
Spring boot api:
@RequestMapping(value="/getUserCache", method = RequestMethod.GET)
public ResponseEntity getUserCache(Principal principal) {
...
}
您知道如何解决该问题吗?