仅限春季钥匙斗篷承载者

时间:2018-06-27 14:21:49

标签: spring keycloak

我正在开发一个有角度的Web应用程序,该应用程序连接到Java(Spring框架)后端。验证通过密钥斗篷服务器完成。

在具有嵌入式tomcat服务器的本地计算机上,角度应用程序和spring应用程序运行无误。

对于部署,我需要通过使用现有的tomcat服务器来使用老式方法。

可通过http://myurl/在ROOT目录中使用角度前端 春天后端被放置为战争文件,可以通过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>

1 个答案:

答案 0 :(得分:1)

禁用csrf令牌可以解决此问题。

示例:

@Override
protected void configure(HttpSecurity http) throws Exception {
      super.configure(http);
      http.authorizeRequests()
      http
          .csrf()
          .disable()
          .authorizeRequests()
          .antMatchers("/*")
          .authenticated()
          .anyRequest() 
相关问题