当前位置: 首页 > 知识库问答 >
问题:

带键斗篷的角形/Spring靴子403

萧凡
2023-03-14

在本文档之后,我使用KeyCloak11.0.2和Spring Security来保护我的Spring Boot应用程序。

我在application.properties中使用了基本的Keycloak配置:

    keycloak.auth-server-url=http://localhost:8085/auth
    keycloak.realm=cirta
    keycloak.resource=cirta-api
    keycloak.public-client=false

我有一个单独的前端角应用程序,在Keylocak中配置为不同的客户端;但与Spring Boot应用程序的领域相同。从Angular应用程序中,我将在HTTP标头中发送KeyCloak提供的令牌:

'Authorization' : 'Bearer ' + this.securityService.kc.token
Access to XMLHttpRequest at 'http://localhost:8080/api/modePaiements' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request.
@Override
protected void configure(HttpSecurity http) throws Exception {
    super.configure(http);
    http.authorizeRequests().antMatchers("/api/*").hasRole("app-manager").anyRequest().permitAll();
}

Angular示例应用程序:https://github.com/bilaldekar/kc-ang

请求标题:

共有1个答案

翟功
2023-03-14

在我看来是csrf问题。

将以下内容添加到安全配置中

http.authorizeRequests().antMatchers("/api/*").hasRole("app-manager").anyRequest().permitAll()
                .and().csrf()
                .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());

上面的代码将在名为xsrf-token的cookie中设置CSRF令牌。Angular在默认情况下为CSRF令牌读取此cookie,并在后续请求中添加此cookie。为了确保Angular可以读取它,我们将HTTP only设置为false。这有其自身的安全含义,因为现在可以通过脚本访问CSRF令牌。如果您不喜欢这样做,另一种方法是从响应头中读取x-xsrf-token标记,并使用Angular Interceptor将其发送到后续的请求头中。

apiUrl: '', //Spring Boot API

然后在src文件夹下添加一个proxy.conf.json,内容如下

{
    "/api": {
        "target": "http://localhost:8080",
        "secure": false
    },
    "logLevel": "debug"
}

Angular.jsonserve命令中添加代理配置

"options": {
    "browserTarget": "kc-ang:build",
    "proxyConfig": "src/proxy.conf.json"
}

现在您会注意到api请求将转到localhost:4200/api/*,并通过上面的配置路由到spring boot应用程序。

如果您的前端和后端在不同的域上,如果您必须支持CORS,那么这就是需要做的

Spring上的安全配置应该允许前端源使用CORS(在本例中是http://localhost:4200):

protected void configure(HttpSecurity http) throws Exception {
    super.configure(http);
    http.authorizeRequests().antMatchers("/api/*").hasRole("app-manager").anyRequest()
            .permitAll()
    .and().csrf()
            .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
    .and().cors();
}

@Bean
CorsConfigurationSource corsConfigurationSource() {
    CorsConfiguration configuration = new CorsConfiguration();
    configuration.setAllowedOrigins(Arrays.asList("http://localhost:4200"));
    configuration.setAllowedMethods(Arrays.asList("GET","POST"));
    configuration.setAllowCredentials(Boolean.TRUE);
    configuration.addAllowedHeader("*");

    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    source.registerCorsConfiguration("/api/*", configuration);
    return source;
}

在Angular上,将environment.ts文件中的apiUrl更新为

apiUrl: '//localhost:8085'
addFournisseur(fournisseur: Fournisseur): Observable<Fournisseur> {
    return this.httpClient.post<Fournisseur>(this.envUrl + '/api/fournisseurs', fournisseur, {withCredentials: true});
  }
 类似资料:
  • 我正试图实现一个带有Spring Boot Restservice的Angular应用程序,该服务由KeyClope保护。 在我的电脑上,一切正常。Angular应用程序是由KeyClope引导的(使用KeyClope Angular),因此我必须登录才能查看该应用程序。应用程序将REST调用与令牌一起发送到Spring Boot。REST服务是KeyClope。承载者仅接收令牌,并使用令牌中的角

  • 我用这个例子:https://github.com/foo4u/keycloak-spring-demo 我有钥匙斗篷。json文件在我的WEB-INF文件夹中,但当我运行应用程序时,会出现以下异常: 此异常的完整堆栈: 这段代码只是Spring Boot和KeyCloak集成的一个例子。 你能告诉我我做错了什么吗? 谢谢你的回答和时间。

  • 我有一个问题运行Spring启动和钥匙斗篷都在docker容器。 我从在docker中运行的数据库mysql的KeyClope开始。 然后我添加了我的领域(SpringBootKey斗篷)、我的客户机(testclient)和一个角色为“user”的用户。之后,我将Spring Security性添加到我的Spring Boot应用程序中。编辑了我的申请。yml 根据我添加了我的SecurityC

  • 我有一个Spring Boot项目,使用Jersey作为我的REST服务,并使用AngularJS进行我的前端开发。当我在不使用任何控制器的情况下运行它并转到index.html(位于resource/statig/index.html中)时,它运行得很好。当我添加一个控制器时,它呈现给出字符串“index.html”作为输出。Spring Boot配置: 球衣配置: 控制器类:

  • 我正在尝试从我的前端应用程序访问KeyClope服务器/openid connect/token/introspect中的introspectendpoint,但我遇到下一个错误: 使用邮递员,卷曲或节点应用程序此请求工作正常,但从我的前端应用程序使用获取方法会出现此错误。我不确定是否可以从浏览器中的前台应用程序查询内省endpoint,或者是否只能从服务器应用程序查询。 其他endpoint,如

  • 使用密钥斗篷创建组并为组分配角色。然后创建用户并将用户分配到特定的组。 要在我的应用程序中访问所有这些,我使用Python Keycoat 如github文档中所述,使用以下代码访问用户信息。 正在获取以下用户信息 如何访问用户的组和角色信息。