【发布时间】:2018-12-06 11:53:18
【问题描述】:
我正在开发一个连接到 java(spring 框架)后端的 angular webapp。身份验证通过 keycloak 服务器完成。
在我的带有嵌入式 tomcat 服务器的本地机器上,angular 应用程序和 spring 应用程序运行没有错误。
对于部署,我需要使用现有的 tomcat 服务器来使用老式方式。
Angular 前端可通过http://myurl/ 在 ROOT 目录中使用 Spring后端被放置为war文件,可通过http://myurl/api/访问
除了身份验证部分之外,一切都在服务器上运行。 Angular 应用程序能够通过重定向等方式登录并获得访问令牌。 这个令牌是根据请求传输到 spring 后端的。 但是后端返回一个未授权的消息。
感谢您的帮助!
消息是:
无法使用授权标头进行身份验证
我创建了一个 SecurityConfig 类:
@Configuration
@EnableWebSecurity
@ComponentScan(basePackageClasses = KeycloakSecurityComponents.class)
class SecurityConfig extends KeycloakWebSecurityConfigurerAdapter {
@Autowired
public void configureGlobal(
AuthenticationManagerBuilder auth) throws Exception {
KeycloakAuthenticationProvider keycloakAuthenticationProvider
= keycloakAuthenticationProvider();
keycloakAuthenticationProvider.setGrantedAuthoritiesMapper(
new SimpleAuthorityMapper());
auth.authenticationProvider(keycloakAuthenticationProvider);
}
@Bean
public KeycloakSpringBootConfigResolver KeycloakConfigResolver() {
return new KeycloakSpringBootConfigResolver();
}
@Bean
@Override
protected SessionAuthenticationStrategy sessionAuthenticationStrategy() {
return new RegisterSessionAuthenticationStrategy(
new SessionRegistryImpl());
}
@Override
protected void configure(HttpSecurity http) throws Exception {
super.configure(http);
http.authorizeRequests()
.antMatchers("/*")
.authenticated()
.anyRequest()
.permitAll();
}
}
将此行添加到应用程序属性中
keycloak
keycloak.auth-server-url=https://authserver.net/auth
keycloak.realm=myRealm keycloak.bearer-only=true
keycloak.resource=myclient
keycloak.cors=true
并添加了这个依赖项
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<version>1.5.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-spring-boot-starter</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.keycloak.bom</groupId>
<artifactId>keycloak-adapter-bom</artifactId>
<version>3.3.0.Final</version>
<type>pom</type>
<scope>import</scope>
</dependency>
【问题讨论】: