快试试用 API Key 来保护你的 SpringBoot 接口安全吧!

打印 上一主题 下一主题

主题 882|帖子 882|积分 2646

来源:baeldung.com/spring-boot-api-key-secret
1、概述

安全性在REST API开发中扮演着重要的角色。一个不安全的REST API可以直接访问到后台系统中的敏感数据。因此,企业组织需要关注API安全性。
Spring Security 提供了各种机制来保护我们的 REST API。其中之一是 API 密钥。API 密钥是客户端在调用 API 调用时提供的令牌。
在本教程中,我们将讨论如何在Spring Security中实现基于API密钥的身份验证。
2、REST API Security

Spring Security可以用来保护REST API的安全性。REST API是无状态的,因此不应该使用会话或cookie。相反,应该使用Basic authentication,API Keys,JWT或OAuth2-based tokens来确保其安全性。
2.1. Basic Authentication

Basic authentication是一种简单的认证方案。客户端发送HTTP请求,其中包含Authorization标头的值为Basic base64_url编码的用户名:密码。Basic authentication仅在HTTPS / SSL等其他安全机制下才被认为是安全的。
2.2. OAuth2

OAuth2是REST API安全的行业标准。它是一种开放的认证和授权标准,允许资源所有者通过访问令牌将授权委托给客户端,以获得对私有数据的访问权限。
2.3. API Keys

一些REST API使用API密钥进行身份验证。API密钥是一个标记,用于向API客户端标识API,而无需引用实际用户。标记可以作为查询字符串或在请求头中发送。
3、用API Keys保护REST API

Spring Boot 基础就不介绍了,推荐看这个实战项目:
https://github.com/javastacks/spring-boot-best-practice
3.1  添加Maven 依赖

让我们首先在我们的pom.xml中声明spring-boot-starter-security依赖关系:
  1. <dependency>
  2.     <groupId>org.springframework.boot</groupId>
  3.     <artifactId>spring-boot-starter-security</artifactId>
  4. </dependency>
复制代码
3.2 创建自定义过滤器(Filter)

实现思路是从请求头中获取API Key,然后使用我们的配置检查秘钥。在这种情况下,我们需要在Spring Security 配置类中添加一个自定义的Filter。
我们将从实现GenericFilterBean开始。GenericFilterBean是一个基于javax.servlet.Filter接口的简单Spring实现。
让我们创建AuthenticationFilter类:
  1. public class AuthenticationFilter extends GenericFilterBean {
  2.     @Override
  3.     public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
  4.       throws IOException, ServletException {
  5.         try {
  6.             Authentication authentication = AuthenticationService.getAuthentication((HttpServletRequest) request);
  7.             SecurityContextHolder.getContext().setAuthentication(authentication);
  8.         } catch (Exception exp) {
  9.             HttpServletResponse httpResponse = (HttpServletResponse) response;
  10.             httpResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
  11.             httpResponse.setContentType(MediaType.APPLICATION_JSON_VALUE);
  12.             PrintWriter writer = httpResponse.getWriter();
  13.             writer.print(exp.getMessage());
  14.             writer.flush();
  15.             writer.close();
  16.         }
  17.         filterChain.doFilter(request, response);
  18.     }
  19. }
复制代码
我们只需要实现doFilter()方法,在这个方法中我们从请求头中获取API Key,并将生成的Authentication对象设置到当前的SecurityContext实例中。
然后请求被传递给其余的过滤器处理,接着转发给DispatcherServlet最后到达我们的控制器。
在AuthenticationService类中,实现从Header中获取API Key并构造Authentication对象,代码如下:
  1. public class AuthenticationService {
  2.     private static final String AUTH_TOKEN_HEADER_NAME = "X-API-KEY";
  3.     private static final String AUTH_TOKEN = "Baeldung";
  4.     public static Authentication getAuthentication(HttpServletRequest request) {
  5.         String apiKey = request.getHeader(AUTH_TOKEN_HEADER_NAME);
  6.         if ((apiKey == null) || !apiKey.equals(AUTH_TOKEN)) {
  7.             throw new BadCredentialsException("Invalid API Key");
  8.         }
  9.         return new ApiKeyAuthentication(apiKey, AuthorityUtils.NO_AUTHORITIES);
  10.     }
  11. }
复制代码
在这里,我们检查请求头是否包含 API Key,如果为空 或者Key值不等于密钥,那么就抛出一个 BadCredentialsException。如果请求头包含 API Key,并且验证通过,则将密钥添加到安全上下文中,然后调用下一个安全过滤器。getAuthentication 方法非常简单,我们只是比较 API Key 头部和密钥是否相等。
为了构建 Authentication 对象,我们必须使用 Spring Security 为了标准身份验证而构建对象时使用的相同方法。所以,需要扩展 AbstractAuthenticationToken 类并手动触发身份验证。
3.3. 扩展AbstractAuthenticationToken

为了成功地实现我们应用的身份验证功能,我们需要将传入的API Key转换为AbstractAuthenticationToken类型的身份验证对象。AbstractAuthenticationToken类实现了Authentication接口,表示一个认证请求的主体和认证信息。
让我们创建ApiKeyAuthentication类:
[code]public class ApiKeyAuthentication extends AbstractAuthenticationToken {    private final String apiKey;    public ApiKeyAuthentication(String apiKey,        Collection
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

何小豆儿在此

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表