将Spring Boot后端+ oauth与前端应用程序集成的问题

卡米斯:

我有一个后端Spring Boot应用程序,该应用程序使用Spotify api中的数据,它需要用户登录才能为我的应用程序提供身份验证令牌。它工作正常,但我不知道如何将其与前端应用程序集成。从服务器应用程序外部发送请求时(localhost:8080),我总是得到403代码。

问题可能是我应该在前端请求Spotify令牌,然后以某种方式将其传递到后端,但是老实说,我什至不知道我应该用谷歌搜索才能实现此目的。

以下是一些关键类:

@Configuration
@EnableOAuth2Sso
public class SpotifyConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception{
        http.authorizeRequests().antMatchers("/login").authenticated();
    }

}
And inside @RestController:
@GetMapping("/user")
    public Principal user(Principal principal){
        return principal;
    }

@GetMapping("/")
    public Principal home(Principal principal){
        return principal;
    }

@GetMapping("/login")
    public Principal login(Principal principal){
        return principal;
    }

这是我第一次与Spring Security相关的东西,我不知道xd发生了什么

耶稣卡斯蒂略:

首先,您将被spring安全性禁止的所有请求导致此行代码。

http.authorizeRequests().antMatchers("/login").authenticated();

您必须授予对某些请求或任何请求的访问权限,但是在这种情况下,您将被禁止每个请求。这就是为什么当您尝试通过休息访问spring boot应用程序时收到403状态(禁止)的原因

因此,您有多种选择来解决此问题。我给你两次。

  1. 如果您希望从每个请求中都授予访问权限,请将Spring安全性中的代码行从上面更改为:
http.authorizeRequests().antMatchers("/**").permitAll();
  • “ / **”表示所有文件夹和子文件夹
  • permitAll()表示对与antMatchers函数匹配的所有请求授予Spring安全性访问权限。
  1. 如果您想授予访问某些请求的权限,通过登录名和其他安全性东西对用户进行身份验证,则可以这样开始:
http.authorizeRequests()
.antMatchers("/somePath").authenticated();
.antMatchers("/somePath2").authenticated();
.antMatchers("/somePath3").permitAll();

但是Spring Security的功能远不止于此,因此,如果您想深入了解可用的倍数配置,则需要阅读官方文档。https://docs.spring.io/spring-security/site/docs/current/reference/html5/#preface,并且还有很多简单的配置示例可以使您开心。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Spring Boot应用程序部署问题

如何对Spring Boot应用程序进行集成测试?

是否可以使用Spring Boot后端创建Cordova应用程序?

Camunda集成到Angular / Spring Boot应用程序中

在以Angular 6为前端,以Spring Boot为后端的应用程序中,SESSIONID不会存储在Cookie中

将Spring应用程序与Express&Node后端结合

将本地React前端连接到本地Spring Boot中间件应用程序时发生CORS错误

如何在前端使用Angular 7并在后端使用Spring Boot调试应用程序

Spring Boot中多战应用程序的集成测试

将Spring Boot与现有的Spring应用程序集成

使用单独的前端/后端服务器将应用程序部署到Heroku

如何将数据从React客户端应用发送到Spring Boot后端应用?

Spring Boot应用程序与Spring Cloud的集成测试

Angular应用程序和Spring Boot与Azure AD的集成

如何将React Native应用程序与Spring Boot Oauth2集成

如何使用jar包装将React Webapp集成到Spring Boot应用程序内部

具有Spring Boot后端的android native应用的oauth授予类型

更好的做法是在后端准备html或将数据从后端发送到前端应用程序

将AngularJS前端添加到Spring Boot应用程序

在 Spring Boot 后端/Angularjs 前端应用程序中允许多个跨源?

wso2 集成 Spring Boot 应用程序

将后端集成到前端

如何使用 Spring Boot 后端部署 Angular 7 前端

Spring Boot 应用程序 - Maven 问题

将 Ember.js 前端应用程序添加到 Node.js 后端应用程序

如何在 Azure(首选通过 Azure Pipelines)上部署 Vue(前端)和 Spring Boot(后端)应用程序?

spring boot 应用程序将 kafka 与活动 mq 集成

部署 spring boot 应用程序的问题

如何将数据从 Angular 前端传递到 Spring Boot 后端?