Spring 仅限 Keycloak Bearer
Spring Keycloak Bearer only
我正在开发一个连接到 java(spring 框架)后端的 angular 网络应用程序。身份验证是通过密钥斗篷服务器完成的。
在我带有嵌入式 tomcat 服务器的本地计算机上,angular 应用程序和 spring 应用程序运行没有错误。
对于部署,我需要使用现有的 tomcat 服务器使用老式的方式。
angular 前端可通过 http://myurl/ 在 ROOT 目录中使用
spring 后端被放置为 war 文件并且可以通过 http://myurl/api/
访问
除身份验证部分外,服务器上的一切正常。
Angular 应用能够通过重定向等方式登录并获取访问令牌。
此令牌根据请求传输到 spring 后端。
但是后端 return 一条未经授权的消息。
欢迎任何帮助!
消息是:
Unable to authenticate using the Authorization header
我创建了一个 SecurityConfig class:
@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>
禁用 csrf 令牌解决了这个问题。
示例:
@Override
protected void configure(HttpSecurity http) throws Exception {
super.configure(http);
http.authorizeRequests()
http
.csrf()
.disable()
.authorizeRequests()
.antMatchers("/*")
.authenticated()
.anyRequest()
我正在开发一个连接到 java(spring 框架)后端的 angular 网络应用程序。身份验证是通过密钥斗篷服务器完成的。
在我带有嵌入式 tomcat 服务器的本地计算机上,angular 应用程序和 spring 应用程序运行没有错误。
对于部署,我需要使用现有的 tomcat 服务器使用老式的方式。
angular 前端可通过 http://myurl/ 在 ROOT 目录中使用 spring 后端被放置为 war 文件并且可以通过 http://myurl/api/
访问除身份验证部分外,服务器上的一切正常。 Angular 应用能够通过重定向等方式登录并获取访问令牌。 此令牌根据请求传输到 spring 后端。 但是后端 return 一条未经授权的消息。
欢迎任何帮助!
消息是:
Unable to authenticate using the Authorization header
我创建了一个 SecurityConfig class:
@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>
禁用 csrf 令牌解决了这个问题。
示例:
@Override
protected void configure(HttpSecurity http) throws Exception {
super.configure(http);
http.authorizeRequests()
http
.csrf()
.disable()
.authorizeRequests()
.antMatchers("/*")
.authenticated()
.anyRequest()