繁体   English   中英

Spring Boot Rest API安全性

[英]Spring Boot Rest API Secruity

我正在寻找一种为Spring Boot创建注释的方法,该方法可以应用于我的REST API中的API方法或控制器,以保护端点。 但是我在寻找有关如何执行此操作的指南或文档时遇到了问题。

例:

@RestController
@RequestMapping("auth")
public class AuthenticationController {

    @RequestMapping(value = "login", method = RequestMethod.POST)
    public TokenResponse login(@RequestBody LogInRequest request) throws InvalidLogInException {}

    @Authorize
    @RequestMapping(value = "me", method = RequestMethod.GET)
    public UserResponse getMe() {}

}

要么

@Authorize
@RestController
@RequestMapping("books")
public class AuthenticationController {

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public BooksResponse getMyBooks() {}

    @RequestMapping(value = "/wish", method = RequestMethod.GET)
    public BooksResponse getWishList() {}
}

我已经研究了Spring Security中的构建,但是比我需要的更深入。 我只需要一个中间件,它将验证标头中提供的令牌:如果有效,则将用户ID添加到请求上下文中,并让请求通过。 如果未返回401未经授权错误,并且不允许运行API方法。

如果我正确理解您的意思,则可以尝试一下。

首先,新建一个名为SecurityConfig.java的文件,它将启用注释配置的配置并处理“ 401”。

import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

/*
    Enable annotation.
    "an annotation for Spring Boot that i can apply to my API methods".
    https://docs.spring.io/spring-security/site/docs/current/reference/html5/#jc-method
 */
@EnableGlobalMethodSecurity(securedEnabled = true)
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        /*
            Enable "401".
            "If not return a 401 Unauthorized Error and don't allow the API method to run".
        */
        http.exceptionHandling().accessDeniedPage("/401");

        // You can add more configuration, Please check the Source code of HttpSecurity.
    }

}

接下来,您可以对提供的代码示例进行如下修改:

// For specific use of annotations, you can check here:
// https://docs.spring.io/spring-security/site/docs/current/reference/html5/#jc-method
@Secured("ROLE_USER")
@RestController
@RequestMapping("auth")
public class AuthenticationController {

    @RequestMapping(value = "login", method = RequestMethod.POST)
    public TokenResponse login(@RequestBody LogInRequest request) throws InvalidLogInException {}

    @Authorize
    @RequestMapping(value = "me", method = RequestMethod.GET)
    public UserResponse getMe() {}

}

emmmm,您还应该添加此内容。

...
@RequestMapping("/401")
...

有一个非常简单的方法可以执行此操作。 步骤如下:

  1. 创建一个JWT身份验证控制器以生成JWT令牌
  2. 创建一个JWTRequestFilter来认证请求
  3. 配置Spring Security以在REST API调用中启用预授权
  4. 定义访问控制的角色/特权。

此处有详细且出色的教程

我已经将其付诸实践并已经使用它。 您可能需要根据需要进行一些自定义。 同样,请随时问我您的疑问。 乐于提供帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM