php oauth 服务端,OAuth 2.0 PHP客户端和服务器示例

一旦您知道协议的工作原理,设置OAuth2提供程序就变得非常简单。这是一个2或3步的过程(取决于您的设置,以及是否代表用户或仅从服务器获取令牌)。

你需要什么

> OAuth2提供程序的工作代码

>耐心

你需要知道如何处理你的代码:

>创建客户端(公共和私有访问令牌)

找出授权和令牌端点的命名方式(通常是/授权和/令牌)

找出范围如何处理

获取令牌的第一步是调用/授权?response_type = code& client_id = [您的ID]& redirect_uri = [您的重定向URI]& scope = [您的范围],其中:

> clientid([您的ID])是您的公开访问令牌

redirect_uri([您的REDIRECT URI])是您的重定向URI。完成自动化步骤后,您将被重定向至此

范围是您未来令牌的范围

完成后(通常有一个提交按钮),您的浏览器将被重定向到URL中指定的URI(code = blah)。保存此值。

当你有这个代码时,调用另一端点:/ token?client_id = [你的ID]& client_secret = [你的秘密]& grant_type = authorization_code& scope = [你的范围]& code = [你的代码] & redirect_uri = [您的REDIRECT URI]

参数:

– client_id – 再次,您的客户端公钥

– client_secret – 你的私钥(这应该是一个服务器端的调用)

– 范围 – 令牌的范围 – 必须匹配第一个呼叫

– redirect_uri – 重定向URI – 必须匹配第一个呼叫

– 代码 – 你收到的代码

如果一切顺利,您将在屏幕上看到包含令牌信息的JSON对象。

后台会发生什么

步骤1(授权)

当您确认表单时,服务器将创建一个临时令牌(称为身份验证令牌),通常其使用寿命非常短(我的oauth2 sp代码通常将其设置为60秒)。这是您的服务器从接收代码到触发步骤2的时间。它只是一个确认系统,其目的是还存储步骤1中提供的信息以防止劫持。

步骤2(令牌)

这是您的访问令牌实际创建的位置。许多验证,很多东西,但最终,令牌只是一个链接您的client_id和您的令牌的值。就是这样。

无耻的插件:如果您使用Laravel框架,我从头开始构建了这个(而不是使用肮脏的,无文档的示例代码):http://bundles.laravel.com/bundle/oauth2-sp

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OAuth 2.0 是一种开放标准,用于授权第三方应用程序访问用户在另一个服务上的资源。下面是 Java 中实现 OAuth 2.0服务端客户端的一些简单示例。 ## OAuth 2.0 服务端的实现 ### 使用 Spring Security OAuth2 Spring Security OAuth2 是一个 OAuth2 认证协议的开源实现,它可以帮助开发者快速构建一个 OAuth2 服务端。以下是一个基于 Spring Security OAuth2 的简单示例: 1. 添加依赖 在 Maven 或 Gradle 中添加以下依赖: ```xml <dependency> <groupId>org.springframework.security.oauth</groupId> <artifactId>spring-security-oauth2</artifactId> <version>2.5.0.RELEASE</version> </dependency> ``` 2. 配置 在 Spring Boot 配置类中添加以下配置: ```java @Configuration @EnableAuthorizationServer public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter { @Autowired private AuthenticationManager authenticationManager; @Autowired private UserDetailsService userDetailsService; @Autowired private TokenStore tokenStore; @Autowired private JwtAccessTokenConverter jwtAccessTokenConverter; @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { clients.inMemory() .withClient("client") .secret("{noop}secret") .authorizedGrantTypes("password", "refresh_token") .scopes("read", "write") .accessTokenValiditySeconds(3600) .refreshTokenValiditySeconds(86400); } @Override public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { endpoints.authenticationManager(authenticationManager) .userDetailsService(userDetailsService) .tokenStore(tokenStore) .accessTokenConverter(jwtAccessTokenConverter); } @Bean public TokenStore tokenStore() { return new JwtTokenStore(jwtAccessTokenConverter()); } @Bean public JwtAccessTokenConverter jwtAccessTokenConverter() { JwtAccessTokenConverter converter = new JwtAccessTokenConverter(); converter.setSigningKey("secret"); return converter; } @Bean public BCryptPasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } } ``` 3. 启动应用程序 启动应用程序后,OAuth2 服务端将在默认端口(8080)上运行。接下来,您可以使用基于 OAuth2 的客户端应用程序与 OAuth2 服务端进行交互。 ## OAuth 2.0 客户端的实现 ### 使用 Spring Security OAuth2 Spring Security OAuth2 还提供了一个基于 OAuth2 的客户端实现,它可以帮助开发者快速构建一个 OAuth2 客户端应用程序。以下是一个基于 Spring Security OAuth2 的简单示例: 1. 添加依赖 在 Maven 或 Gradle 中添加以下依赖: ```xml <dependency> <groupId>org.springframework.security.oauth</groupId> <artifactId>spring-security-oauth2</artifactId> <version>2.5.0.RELEASE</version> </dependency> ``` 2. 配置 在 Spring Boot 配置类中添加以下配置: ```java @Configuration @EnableOAuth2Sso public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/", "/login**") .permitAll() .anyRequest() .authenticated(); } } ``` 3. 创建资源服务器 在 Spring Boot 应用程序中创建一个资源服务器,以响应从 OAuth2 服务端返回的令牌。以下是一个简单的资源服务器示例: ```java @RestController public class ResourceController { @GetMapping("/resource") public String getResource() { return "Hello World"; } } ``` 4. 启动应用程序 启动应用程序后,OAuth2 客户端将在默认端口(8080)上运行。接下来,您可以使用 OAuth2 服务端颁发的令牌访问受保护的资源。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值