代码之家  ›  专栏  ›  技术社区  ›  Simon

仅限弹簧钥匙斗篷支架

  •  1
  • Simon  · 技术社区  · 7 年前

    在我的带有嵌入式Tomcat服务器的本地机器上,Angular应用程序和Spring应用程序运行无误。

    角度前端在根目录中通过 http://myurl/ http://myurl/api/

    除身份验证部分外,其他功能都在服务器上工作。 此令牌在请求时传输到Spring后端。

    任何帮助都是必要的!

    无法使用授权头进行身份验证

    @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 回复  |  直到 6 年前
        1
  •  1
  •   xmlParser    6 年前

    禁用CSRF令牌解决了这个问题。

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