Iam开发了一个与java (spring框架)后端连接的角度Iam应用程序。身份验证通过密钥披风服务器完成。
在我的本地机器上,带有嵌入式tomcat服务器的角应用程序和spring应用程序运行时没有错误。
对于部署,我需要使用老式的方式,使用现有的tomcat服务器。
角前端通过http://myurl/在根目录中可用,弹簧后端作为war文件放置,并可通过http://myurl/api/访问。
除了身份验证部分之外,所有内容都在服务器上工作。角应用程序可以通过重定向等方式登录并获得访问令牌。根据请求将此令牌传输到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>发布于 2018-07-02 12:23:32
禁用csrf令牌解决了此问题。
示例:
@Override
protected void configure(HttpSecurity http) throws Exception {
super.configure(http);
http.authorizeRequests()
http
.csrf()
.disable()
.authorizeRequests()
.antMatchers("/*")
.authenticated()
.anyRequest() https://stackoverflow.com/questions/51065253
复制相似问题