这是我的Spring Boot Blog帖子系列的第三篇文章。 在第一篇文章中,我谈到了我使用Spring Boot创建RESTFul Services的经验。 然后我将样本扩展到
与Swagger文档集成 。 在这篇文章中,我将在安全方面扩展上述示例。

什么是API安全性

API安全性广泛,具有许多不同的定义,含义和解决方案。 API安全性中的主要关键术语是授权,身份验证,加密,联合和委派。 但是,在这里我不会谈论它们。

什么是认证

身份验证用于可靠地确定最终用户的身份,并根据正确标识的用户授予对资源的访问权限。

什么是基本身份验证

基本身份验证是对资源实施访问控制的最简单方法。 在此,HTTP用户代理在发出请求时提供用户名和密码。 当需要身份验证时,包含用户名和密码的字符串由冒号分隔,并在发送到后端之前经过Base64编码。

如何调用基本身份验证受保护的API

选项1:发送授权标头。 该值是base64编码的username:password Ex:“授权:基本Y2hhbmRhbmE6Y2hhbmRhbmE =”

curl -X GET http://localhost:8080/admin/hello/chandana -H 'authorization: Basic Y2hhbmRhbmE6Y2hhbmRhbmE='

选项2:使用网址:

curl -X GET -u username:password  http://localhost:8080/admin/hello/chandana

好的,我们讨论了一些基本的东西。 因此,让我们来看一下如何使用Spring Security保护REST API。 您可以从我的GitHub存储库下载初始示例代码(Swagger Spring Boot Project源代码)

为了使用基本的auth安全性增强我们先前的示例,首先我将在pom文件中添加“ spring-boot-starter-security”和“ spring-boot-starter-tomcat”依赖项。

<!-- --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0</version></dependency>

下一步是使用@EnableWebSecurity批注对我们的配置类进行批注,并从WebSecurityConfigurerAdapter扩展配置类。 EnableWebSecurity批注将启用Spring-Security Web安全支持。

@Configuration
@EnableSwagger2
@EnableWebSecurity
public class ApplicationConfig extends WebSecurityConfigurerAdapter {

重写的configure(HttpSecurity)方法用于定义哪些URL路径应该受到保护,哪些不应该受到保护。 在我的示例中,不需要“ /”和“ / api”路径进行任何身份验证,并且任何其他路径(例如:“ admin”)都应使用基本身份验证进行身份验证。

@Override
protected void configure(HttpSecurity http) throws Exception {http.csrf().disable();http.authorizeRequests().antMatchers("/", "/api/**").permitAll().anyRequest().authenticated();http.httpBasic().authenticationEntryPoint(basicAuthenticationPoint);
}

在configureGlobal(AuthenticationManagerBuilder)方法中,我创建了一个内存用户存储,其中包含一个名为“ chandana”的用户。 在那里,我为内存中的用户添加了用户名,密码和userole。

@Autowiredpublic void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {auth.inMemoryAuthentication().withUser("chandana").password("chandana").roles("USER");}

除此之外,您还可以看到我已将自动装配的BasicAuthenticationPoint添加到我的配置类中。 BasicAuthenticationEntryPoint类的目的是将“ WWW-Authenticate”标头设置为响应。 因此,Web浏览器将显示一个对话框,用于基于基本身份验证机制(WWW-Authenticate标头)输入用户名和密码

然后,您可以使用“ mvn spring-boot:run”运行示例。 当您访问“ localhost:8080 / api / hello / chandana”时,调用api不需要基本身份验证。 但是,如果您尝试访问“ localhost:8080 / admin / hello / chandana”,则需要提供基本的身份验证凭据才能访问资源。

AppConfig类:

package com.chandana.helloworld.config;  import org.springframework.beans.factory.annotation.Autowired;  import org.springframework.context.annotation.Bean;  import org.springframework.context.annotation.Configuration;  import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;  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;  import springfox.documentation.builders.ApiInfoBuilder;  import springfox.documentation.builders.PathSelectors;  import springfox.documentation.builders.RequestHandlerSelectors;  import springfox.documentation.service.ApiInfo;  import springfox.documentation.service.Contact;  import springfox.documentation.spi.DocumentationType;  import springfox.documentation.spring.web.plugins.Docket;  import springfox.documentation.swagger2.annotations.EnableSwagger2;  @Configuration  @EnableSwagger2  @EnableWebSecurity  public class ApplicationConfig extends WebSecurityConfigurerAdapter {  @Autowired  private BasicAuthenticationPoint basicAuthenticationPoint;  @Bean  public Docket api() {  return new Docket(DocumentationType.SWAGGER_2)  .apiInfo(getApiInfo())  .select()  .apis(RequestHandlerSelectors.basePackage("com.chandana.helloworld.controllers"))  .paths(PathSelectors.any())  .build();  }  @Override  protected void configure(HttpSecurity http) throws Exception {  http.csrf().disable();  http.authorizeRequests().antMatchers("/", "/api/**").permitAll()  .anyRequest().authenticated();  http.httpBasic().authenticationEntryPoint(basicAuthenticationPoint);  }  private ApiInfo getApiInfo() {  Contact contact = new Contact("Chandana Napagoda", "http://blog.napagoda.com", "cnapagoda@gmail.com");  return new ApiInfoBuilder()  .title("Example Api Title")  .description("Example Api Definition")  .version("1.0.0")  .license("Apache 2.0")  .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0")  .contact(contact)  .build();  }  @Autowired  public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {  auth.inMemoryAuthentication().withUser("chandana").password("chandana").roles("USER");  }  }

BasicAuthenticationEntryPoint类:

package com.chandana.helloworld.config;  import org.springframework.security.core.AuthenticationException;  import org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint;  import org.springframework.stereotype.Component;  import java.io.IOException;  import java.io.PrintWriter;  import javax.servlet.ServletException;  import javax.servlet.http.HttpServletRequest;  import javax.servlet.http.HttpServletResponse;  @Component  public class BasicAuthenticationPoint extends BasicAuthenticationEntryPoint {  @Override  public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authEx)  throws IOException, ServletException {  response.addHeader("WWW-Authenticate", "Basic realm=" +getRealmName());  response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);  PrintWriter writer = response.getWriter();  writer.println("HTTP Status 401 - " + authEx.getMessage());  }  @Override  public void afterPropertiesSet() throws Exception {  setRealmName("Chandana");  super.afterPropertiesSet();  }  }

您也可以从我的GitHub存储库下载Spring Boot Basic Auth Project源代码。

翻译自: https://www.javacodegeeks.com/2017/10/secure-spring-boot-rest-api-using-basic-authentication.html

使用基本身份验证来保护Spring Boot REST API相关推荐

  1. 这些保护Spring Boot 应用的方法,你都用了吗?

    转载自   这些保护Spring Boot 应用的方法,你都用了吗? Spring Boot大大简化了Spring应用程序的开发.它的自动配置和启动依赖大大减少了开始一个应用所需的代码和配置量,如果你 ...

  2. 前后端分离的用户验证原理及Spring Boot + JWT的框架搭建(附完整的框架代码)之二

    本篇承接上一篇,关于Session以及JWT Token参考: 前后端分离的用户验证原理及Spring Boot + JWT的框架搭建(附完整的框架代码)之一 框架整体描述 框架使用Spring Bo ...

  3. 10 种保护 Spring Boot 应用的绝佳方法

    Spring Boot大大简化了Spring应用程序的开发.它的自动配置和启动依赖大大减少了开始一个应用所需的代码和配置量,如果你已经习惯了Spring和大量XML配置,Spring Boot无疑是一 ...

  4. okta-spring_通过Okta的单点登录保护Spring Boot Web App的安全

    okta-spring "我喜欢编写身份验证和授权代码." 〜从来没有Java开发人员. 厌倦了一次又一次地建立相同的登录屏幕? 尝试使用Okta API进行托管身份验证,授权和多 ...

  5. 通过Okta的单点登录保护Spring Boot Web App的安全

    "我喜欢编写身份验证和授权代码." 〜从来没有Java开发人员. 厌倦了一次又一次地建立相同的登录屏幕? 尝试使用Okta API进行托管身份验证,授权和多因素身份验证. 您可以使 ...

  6. 10 种保护 Spring Boot 应用的绝佳方法 1

    Spring Boot大大简化了Spring应用程序的开发.它的自动配置和启动依赖大大减少了开始一个应用所需的代码和配置量,如果你已经习惯了Spring和大量XML配置,Spring Boot无疑是一 ...

  7. api分层内部外部 spring_java - Spring boot restful API分层架构验证 - SO中文参考 - www.soinside.com...

    [今天,我与我们的一位团队成员就Controller和Service层中的RESTful API输入的验证进行了大讨论,我觉得这是提出更大论点的糟糕日子.因此,我们有一个具有分层体系结构的spring ...

  8. java webservice 身份验证_java-Http基本身份验证不适用于Spring WS和WebS...

    我尝试使用Spring(-WS)将HTTP基本身份验证凭据添加到我的SOAP请求中.该请求本身有效,但是没有凭据提交. HTTP标头应如下所示: [...] Connection: Keep-Aliv ...

  9. 【232期】面试官:如何保护 Spring Boot 配置文件敏感信息?

    点击上方"Java精选",选择"设为星标" 别问别人为什么,多问自己凭什么! 下方有惊喜,留言必回,有问必答! 每天 08:15 更新文章,每天进步一点点... ...

最新文章

  1. canvas绘制时钟
  2. 正态分布随机数 C语言,C语言产生满足正态分布的随机数
  3. ArcGIS Engine10.4版本
  4. 从菜鸟成为数据科学家的养成方案
  5. 每日程序C语言3-三个数大小排序
  6. WPF的样式(Style)继承
  7. ocp linux 基础要点
  8. pytorch 实现 LSTM AutoEncoder 与案例
  9. Mac电脑用CrossOver安装的Windows软件在哪?
  10. 文本框中呈现文字,用鼠标点击一下消失
  11. 10款滑动门代码_jquery 滑动门_js滑动门_tab滑动门_jquery 选项卡_js选项卡_tab选项卡效果(三)
  12. 用java编写某年某月的日历_Java-查询某年某月一个月的数据,以日历的形式展示。...
  13. 无线打印机服务器安装教程,网络打印服务器怎么安装和使用?
  14. 阿里云个人银行卡四要素验证接口
  15. Android开发wifi功能(附近Wi-Fi,输入密码,链接Wi-Fi)
  16. 易捷行云获选国际开源基础设施基金会OIF“双董事” 席位
  17. EasyDSS高性能RTMP、HLS(m3u8)、HTTP-FLV、RTSP流媒体服务器解决方案之CDN内容分发网络
  18. 游戏盾能防住几T的攻击吗
  19. SQLyog中如何导入mysql数据库
  20. 关于SQL注入,绕过逗号过滤

热门文章

  1. Java进阶学习路线
  2. Streaming的算法Reservoir Sampling
  3. 自定义ClassLoader和双亲委派机制
  4. 2016蓝桥杯省赛---java---B---1(有奖猜谜)
  5. 2020蓝桥杯省赛---java---B---8(走方格)
  6. Mybatis主要内容
  7. springboot创建项目2 开发环境的搭建
  8. python中math库_Python的math库、random库实际应用
  9. java线程——信号量(Semaphore)+障栅(CyclicBarrier)
  10. DFS应用——寻找欧拉回路