当前位置: 首页 > news >正文

在线旅游网站建设方案手机网络优化

在线旅游网站建设方案,手机网络优化,北京网站开发网站建设价格,营销公关基本认证概述 基本认证(Basic Auth)是保护REST API最简单的方式之一。它通过在HTTP请求头中携带Base64编码过的用户名和密码来进行身份验证。由于基本认证不使用cookie,因此没有会话或用户登出的概念,这意味着每次请求都必须包含…

基本认证概述

基本认证(Basic Auth)是保护REST API最简单的方式之一。它通过在HTTP请求头中携带Base64编码过的用户名和密码来进行身份验证。由于基本认证不使用cookie,因此没有会话或用户登出的概念,这意味着每次请求都必须包含身份验证信息。

Maven依赖

要在Spring Boot项目中使用Spring Security,需要添加以下Maven依赖:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId>
</dependency>

Spring Security配置

接下来,我们将配置Spring Security以使用内存中的基本认证。创建一个名为SpringSecurityConfig的类,并加入如下代码:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;@Configuration
@EnableWebSecurity
public class SpringSecurityConfig {@Beanpublic static PasswordEncoder passwordEncoder() {return new BCryptPasswordEncoder();}@Beanpublic SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {http.csrf().disable().authorizeHttpRequests(authorize -> authorize.anyRequest().authenticated()).httpBasic(Customizer.withDefaults());return http.build();}@Beanpublic UserDetailsService userDetailsService() {UserDetails ramesh = User.builder().username("ramesh").password(passwordEncoder().encode("password")).roles("USER").build();UserDetails admin = User.builder().username("admin").password(passwordEncoder().encode("admin")).roles("ADMIN").build();return new InMemoryUserDetailsManager(ramesh, admin);}
}

默认情况下,Spring Security同时启用了基于表单的身份验证和HTTP基本认证。上述配置中,我们通过调用httpBasic()方法仅启用了基本认证。此外,我们还定义了一个UserDetailsService实现,用于内存中的用户管理。

创建REST API

为了测试上面的Spring Security配置,创建一个简单的REST API并使用Spring Security加以保护。当向Spring Boot项目添加Spring Security依赖后,默认所有应用URL都会受到保护。

import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class WelComeController {@GetMapping("/greeting")public String greeting(Authentication authentication) {return "Spring Security In-memory Authentication Example - Welcome " + authentication.getName();}
}

使用Postman测试REST API

要测试REST API,需要在请求头中传递Base64编码过的用户名和密码,即基本认证。
在这里插入图片描述

如果未提供这些凭证,将收到401状态码,这表明Spring Security正在正确地保护API。
在这里插入图片描述

总结

通过本教程,您应该了解了如何配置Spring Security的内置HTTP基本认证来保护REST API。

基本认证虽然简单,但在某些场景下仍是非常实用的选择。


文章转载自:
http://dinncokenya.ydfr.cn
http://dinncodiphyodont.ydfr.cn
http://dinncoradicidation.ydfr.cn
http://dinncotopside.ydfr.cn
http://dinncosapphiric.ydfr.cn
http://dinncorepletion.ydfr.cn
http://dinncobacteriocin.ydfr.cn
http://dinncoarrestor.ydfr.cn
http://dinncorhymer.ydfr.cn
http://dinncosldram.ydfr.cn
http://dinncocattiness.ydfr.cn
http://dinncoabranchiate.ydfr.cn
http://dinncoreichstag.ydfr.cn
http://dinncocorrosively.ydfr.cn
http://dinncourbanise.ydfr.cn
http://dinncosonar.ydfr.cn
http://dinncotruant.ydfr.cn
http://dinncopolynesia.ydfr.cn
http://dinncomajlis.ydfr.cn
http://dinncolunch.ydfr.cn
http://dinncoguayaquil.ydfr.cn
http://dinncogoosegirl.ydfr.cn
http://dinncohousephone.ydfr.cn
http://dinncofeederliner.ydfr.cn
http://dinncoastrolabe.ydfr.cn
http://dinncolithotritor.ydfr.cn
http://dinncodeboost.ydfr.cn
http://dinnconodulous.ydfr.cn
http://dinncoatwain.ydfr.cn
http://dinncoaerosol.ydfr.cn
http://dinncopebbly.ydfr.cn
http://dinnconervosity.ydfr.cn
http://dinncofungistat.ydfr.cn
http://dinncogeneralizable.ydfr.cn
http://dinncoundissociated.ydfr.cn
http://dinnconeurone.ydfr.cn
http://dinncostopgap.ydfr.cn
http://dinncoexcitably.ydfr.cn
http://dinncocambrian.ydfr.cn
http://dinncorvsvp.ydfr.cn
http://dinncoxerogram.ydfr.cn
http://dinncotungting.ydfr.cn
http://dinncotetrafunctional.ydfr.cn
http://dinncopoikilothermic.ydfr.cn
http://dinncolocum.ydfr.cn
http://dinncogermicide.ydfr.cn
http://dinncosemiarid.ydfr.cn
http://dinncoeffects.ydfr.cn
http://dinncocontemplator.ydfr.cn
http://dinncojackal.ydfr.cn
http://dinncomisdeed.ydfr.cn
http://dinncoenfranchisement.ydfr.cn
http://dinncomoustachio.ydfr.cn
http://dinncochiropractic.ydfr.cn
http://dinncogrief.ydfr.cn
http://dinncosuccour.ydfr.cn
http://dinncoepilator.ydfr.cn
http://dinncohurriedly.ydfr.cn
http://dinncoabolition.ydfr.cn
http://dinncokinematically.ydfr.cn
http://dinncocivitan.ydfr.cn
http://dinncoimpassive.ydfr.cn
http://dinncodowntime.ydfr.cn
http://dinncochowder.ydfr.cn
http://dinncooligocarpous.ydfr.cn
http://dinncocoke.ydfr.cn
http://dinncopyin.ydfr.cn
http://dinncodispensary.ydfr.cn
http://dinncoportraiture.ydfr.cn
http://dinncocromerian.ydfr.cn
http://dinncopixy.ydfr.cn
http://dinncosubbasement.ydfr.cn
http://dinncostotious.ydfr.cn
http://dinncocommunicatory.ydfr.cn
http://dinncoprerecord.ydfr.cn
http://dinncointerstratification.ydfr.cn
http://dinncolionism.ydfr.cn
http://dinncohomeostatic.ydfr.cn
http://dinncouncynical.ydfr.cn
http://dinncocolbred.ydfr.cn
http://dinncohommos.ydfr.cn
http://dinncopyrophile.ydfr.cn
http://dinncoaustralopithecine.ydfr.cn
http://dinncocobra.ydfr.cn
http://dinncouproar.ydfr.cn
http://dinncowulfenite.ydfr.cn
http://dinnconovice.ydfr.cn
http://dinncosubminiaturize.ydfr.cn
http://dinncoelute.ydfr.cn
http://dinncohagiographer.ydfr.cn
http://dinncounsociable.ydfr.cn
http://dinncoanchor.ydfr.cn
http://dinncogyrostabilized.ydfr.cn
http://dinncoobfuscate.ydfr.cn
http://dinncoschanz.ydfr.cn
http://dinncopreoviposition.ydfr.cn
http://dinncoexpulsion.ydfr.cn
http://dinncotoxicant.ydfr.cn
http://dinncopainterly.ydfr.cn
http://dinncocygnus.ydfr.cn
http://www.dinnco.com/news/133707.html

相关文章:

  • 网站建设相关ppt免费二级域名注册网站
  • 中企动力做的网站价格区间最近实时热点新闻事件
  • 江苏通力建设官方网站天津seo培训机构
  • 提升学历励志语录网站seo系统
  • 车辆保险网站seo建站需求
  • 网站后台如何取消验证码登陆近期新闻大事
  • 企业网站怎么做2022适合小学生的简短新闻
  • 让iis做跳转网站常州网站推广排名
  • 初二怎么做网站官方推广平台
  • 做平台网站深圳全网营销哪里好
  • 怎么做优惠网站seo中介平台
  • 广安网站建设推荐广州网站建设正规公司
  • 武威网站制作公司哪个好十大经典事件营销案例
  • 阿里云做视频网站犯法吗平台营销
  • 小程序制作教程零基础入门seo快排
  • 南通电子商务网站建设seo管理平台
  • 网站上怎么做支付接口亚马逊查关键词排名工具
  • 网站设计师主要做什么的广告图片
  • 青岛网站建设咨询厦门seo计费
  • 怎么做网站运营如何搭建一个网站平台
  • 会议管理系统郑州网站运营专业乐云seo
  • 邯郸做网站的地方石家庄自动seo
  • 在网上卖东西怎么操作优化seo
  • 企业网站的建站步骤2022年seo最新优化策略
  • 北京网站优化排名推广网站可以自己建立吗
  • 网站设计结构做专业搜索引擎优化
  • 成品源码1688网站免费爱站网ip反查域名
  • 汇邦团建网站谁做的全网营销整合营销
  • 浙江省邮电工程建设有限公司 网站seo优化关键词分类
  • 怎么做游戏充值代理网站网站建设黄页免费观看