【Curl测试http2.0】

由于 CentOS 7 内置的 curl 和 libcurl 源为较旧的 7.29.0,不支持一些新特性且有安全性问题,所以需要更新一下。

在这里使用 city-fan 的更新源来更新。

一、 更新 ca-bundle
首先备份一下:

cp /etc/pki/tls/certs/ca-bundle.crt /etc/pki/tls/certs/ca-bundle.crt.bak

更新并替换:

curl http://curl.haxx.se/ca/cacert.pem -o /etc/pki/tls/certs/ca-bundle.crt

二、 新增 repo 源
新增 repo:

vim /etc/yum.repos.d/city-fan-for-curl.repo

内容为:

[CityFanforCurl]
name=City Fan Repo
baseurl=http://www.city-fan.org/ftp/contrib/yum-repo/rhel7/x86_64/
enabled=0
gpgcheck=0

更新 curl
直接使用如下命令进行更新:

yum update curl --enablerepo=CityFanforCurl -y

测试http2.0

curl --http2 -vo /dev/null  "https://www.example.com"  --resolve "www.example.com:443:IP"

> GET / HTTP/2
> Host: www.example.com
> user-agent: curl/7.70.0
> accept: */*
> 
* Connection state changed (MAX_CONCURRENT_STREAMS == 128)!
< HTTP/2 200 
< server: nginx
< date: Thu, 28 May 2020 00:02:42 GMT
< content-type: text/html
< content-length: 430032
< accept-ranges: bytes
< etag: "28c465b661fc10bd3048b0d83bafd969"
< last-modified: Wed, 27 May 2020 23:51:39 GMT
< set-cookie: secure=true;HttpOnly=true
< x-frame-options: SAMEORIGIN
< x-amz-id-2: PSnM9Fz7rdZq0QXWKTuXIpFVqM1YEADbtVzmXZqSrys6TIzn0QhDw/4xT59GAZypXsLQ4sM7Tp4=
< x-amz-request-id: 12112A493B7B0AEC
< x-amz-version-id: lS08YmEi2sk1lSd7J4LoVv0Hms70R_fu
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Springboot Cloud集成OAuth2.0可以让你的应用支持用户认证和授权,可以通过几个简单的步骤实现。 以下是基本的步骤: 1. 添加Spring Security和OAuth2依赖 ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.security.oauth.boot</groupId> <artifactId>spring-security-oauth2-autoconfigure</artifactId> <version>2.0.0.RELEASE</version> </dependency> ``` 2. 创建Authorization Server配置 在Spring Boot应用程序中,您可以使用@EnableAuthorizationServer注释来启用OAuth2授权服务器。创建一个类,使用该注释,如下所示: ```java @Configuration @EnableAuthorizationServer public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter { // ... } ``` 在此类中,您需要实现以下方法: ```java @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { clients.inMemory() .withClient("clientapp") .secret("123456") .authorizedGrantTypes("password", "refresh_token") .scopes("read", "write") .accessTokenValiditySeconds(1800) .refreshTokenValiditySeconds(3600 * 24); } @Override public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { endpoints.authenticationManager(authenticationManager); } ``` 这里我们已经实现了一个简单的ClientDetailsService,使用内存中的客户端信息。客户端名为“clientapp”,密码为“123456”,并且支持密码授权和刷新令牌。还设置了访问令牌和刷新令牌的有效期。 3. 配置Resource Server 现在,您需要配置Resource Server以保护您的REST API。在Spring Boot应用程序中,您可以使用@EnableResourceServer注释启用OAuth2资源服务器。 ```java @Configuration @EnableResourceServer public class ResourceServerConfig extends ResourceServerConfigurerAdapter { // ... } ``` 在这个配置类中,需要实现以下方法: ```java @Override public void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/api/**").authenticated() .and() .exceptionHandling().accessDeniedHandler(new OAuth2AccessDeniedHandler()); } ``` 这里我们将“/api/**”路径下的所有请求保护起来,只有经过授权的用户才能访问。如果用户未经授权尝试访问,我们将返回一个403 Forbidden响应。 4. 配置UserDetailsService 为了使资源服务器能够对用户进行身份验证,您需要提供一个UserDetailsService。这个类将使用用户名和密码对用户进行身份验证。 ```java @Service public class UserDetailsServiceImpl implements UserDetailsService { // ... } ``` 5. 配置WebSecurityConfigurerAdapter 为了使Spring Security能够使用UserDetailsService进行身份验证,您需要扩展WebSecurityConfigurerAdapter并将UserDetailsService注入其中。 ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { // ... @Autowired private UserDetailsService userDetailsService; // ... } ``` 在这个类中,你需要实现以下方法: ```java @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService) .passwordEncoder(passwordEncoder()); } @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } ``` 这里我们使用BCryptPasswordEncoder作为密码编码器,以确保用户密码的安全性。 6. 测试OAuth2 现在您已经完成了OAuth2的所有配置,可以测试它是否正常工作。您可以使用cURL或Postman等工具发送HTTP请求来测试OAuth2。以下是一个使用cURL测试OAuth2的示例: ```bash curl -X POST \ http://localhost:8080/oauth/token \ -H 'Authorization: Basic Y2xpZW50YXBwOjEyMzQ1Ng==' \ -H 'Content-Type: application/x-www-form-urlencoded' \ -d 'grant_type=password&username=user&password=pass' ``` 这里我们向Authorization Server发送密码授权请求,使用用户名“user”和密码“pass”进行身份验证。如果身份验证成功,将返回一个访问令牌和刷新令牌。 7. 使用访问令牌访问受保护的API 现在您已经获得了访问令牌,可以使用它来访问受保护的API。以下是一个使用cURL测试API的示例: ```bash curl -X GET \ http://localhost:8080/api/greeting \ -H 'Authorization: Bearer <access_token>' ``` 这里我们向Resource Server发送GET请求,使用访问令牌进行身份验证。如果身份验证成功,将返回一个响应。 这就是Springboot Cloud集成OAuth2.0的基本步骤。在实际项目中,您还需要做一些额外的工作,例如存储用户信息和客户端信息,实现授权码授权和客户端凭据授权等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值