oauth简化模式android,OAuth2简化模式

package cn.linst.authserver.config;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.http.HttpMethod;

import org.springframework.security.authentication.AuthenticationManager;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;

import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;

import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;

import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;

import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;

import org.springframework.security.oauth2.provider.ClientDetailsService;

import org.springframework.security.oauth2.provider.code.AuthorizationCodeServices;

import org.springframework.security.oauth2.provider.code.InMemoryAuthorizationCodeServices;

import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;

import org.springframework.security.oauth2.provider.token.DefaultTokenServices;

import org.springframework.security.oauth2.provider.token.TokenStore;

import java.lang.ref.SoftReference;

/**

* 简化模式。

*

* 在这个auth-server demo中,就是相当于第三方平台。

* 授权服务器。模拟第三方平台。授权服务器一方面是校验客户端,另一方面则是校验用户。

* 用户信息校验在SecurityConfig配置了。

*/

@EnableAuthorizationServer // 表示开启授权服务器的自动化配置

@Configuration

public class AuthorizationServer extends AuthorizationServerConfigurerAdapter {

@Autowired

TokenStore tokenStore;

/**

* 保存的客户端信息

*/

@Autowired

ClientDetailsService clientDetailsService;

/**

* 令牌相关配置。用来配置 Token 的一些基本信息。

* 如 Token 是否支持刷新、Token 的存储位置、Token 的有效期以及刷新 Token 的有效期等。

* 当 Token 快要过期的时候,需要获取一个新的token,获取新的 Token 时候,需要有一个凭证信息,refresh_token。这个 refresh_token 也是有有效期的

*/

@Bean

AuthorizationServerTokenServices tokenServices() {

DefaultTokenServices services = new DefaultTokenServices();

services.setClientDetailsService(clientDetailsService);

services.setSupportRefreshToken(true);

services.setTokenStore(tokenStore);

// access_token 有效期

services.setAccessTokenValiditySeconds(60 * 60 * 2);

// refresh_token 有效期

services.setRefreshTokenValiditySeconds(60 * 60 * 24 * 3);

return services;

}

/**

* AuthorizationServerSecurityConfigurer用来配置令牌端点的安全约束,就是这个端点谁能访问,谁不能访问。

* checkTokenAccess是指一个 Token 校验的端点,这个端点我们设置为可以直接访问。在资源服务器收到 Token 之后,需要去校验 Token 的合法性,就会访问这个端点。

*/

@Override

public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {

// 1. /oauth/check_token 资源服务器会自动校验这个接口,检查token对应的信息。

// 2. 这里配置为了让这个接口都能访问到。

// 3. 允许表单登录。

security.checkTokenAccess("permitAll()")

.allowFormAuthenticationForClients();

}

//

// /** 授权码模式

// * ClientDetailsServiceConfigurer 用来配置客户端的信息。

// * 校验客户端

// */

// @Override

// public void configure(ClientDetailsServiceConfigurer clients) throws Exception {

// clients.inMemory()

// .withClient("clientIdForMyApplication") //应用在第三方平台注册,平台给应用生成appid。

// .secret(new BCryptPasswordEncoder().encode("123"))

// .resourceIds("res1") // 资源id

// .authorizedGrantTypes("authorization_code","refresh_token") // authorization_code:授权码模式 ;refresh_token:刷新token

// .scopes("all")

.autoApprove(true)

// .redirectUris("http://localhost:8082/index.html"); // 授权完成后,重定向的地址。

// }

/**

* 简化模式

* ClientDetailsServiceConfigurer 用来配置客户端的信息。

* 校验客户端

*/

@Override

public void configure(ClientDetailsServiceConfigurer clients) throws Exception {

clients.inMemory()

.withClient("clientIdForMyApplication") //应用在第三方平台注册,平台给应用生成appid。

.secret(new BCryptPasswordEncoder().encode("123"))

.resourceIds("res1") // 资源id

.authorizedGrantTypes("refresh_token","implicit") // authorization_code:授权码模式 ;implicit:支持简化模式

.scopes("all")

// .autoApprove(true)

.redirectUris("http://localhost:8082/01.html"); // 授权完成后,重定向的地址。

}

/**

* AuthorizationServerEndpointsConfigurer用来配置令牌的访问端点和令牌服务。

* authorizationCodeServices用来配置授权码的存储。

* tokenServices 这个 Bean 主要用来配置 Token 的一些基本信息。

*/

@Override

public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {

endpoints.authorizationCodeServices(authorizationCodeServices())

.tokenServices(tokenServices());

}

/**

* 授权码保存的地方,存哪都行,一般存内存,授权码用一次就失效了没用了。

* 授权码与令牌区别:授权码是用来获取令牌的,使用一次就失效,令牌则是用来获取资源。

*

*/

@Bean

AuthorizationCodeServices authorizationCodeServices() {

return new InMemoryAuthorizationCodeServices();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值