0
点赞
收藏
分享

微信扫一扫

安全框架--OAuth2

上一篇 <<<安全框架--JWT
下一篇 >>>安全架构整体设计方案


OAuth2.0: 不是一们技术,而是一种协议,开放授权协议。

应用场景

第三方联合登陆 微信、QQ、支付宝、钉钉、码云、github底层都是会遵循oauth2.0协议。

OAuth2认证和授权的过程

OAuth认证和授权的原理

  • a.根据appid和appkey 获取授权码
  • b.根据授权码获取accesstoken和refreshToken
  • c.根据accessToken获取openid
  • d.根据openid获取用户信息
  • e.使用refreshToken定时刷新accessToken

Oauth2角色划分

  • a、Resource Server:被授权访问的资源
  • b、Authotization Server:OAUTH2认证授权中心
  • c、Resource Owner: 用户
  • d、Client:使用API的客户端(如Android 、IOS、web app)

Oauth2四种授权方式

Oauth2令牌存放方式

  • 1)、db
  • 2)、memory

代码演示

a、获得授权码

请求:http://localhost:8080/oauth/authorize?client_id=jarye_appid&response_type=code
返回:http://www.jarye.cn/callback?code=w111vA


b、根据授权码获取令牌



c、令牌的有效性验证


a、直接请求报错



b、加上令牌信息访问正常


核心代码

/*
当前项目为认证授权中心
 */
@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
    //允许表单提交 检查accessToken是否有效期的情况下
    security.allowFormAuthenticationForClients()
            .checkTokenAccess("permitAll()");
}

/**
 * 分配我们的appid和appkey clentid clientkey
 *
 * @param clients
 * @throws Exception
 */
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
    // 读数据库
    clients.inMemory()
            // appid
            .withClient("jarye_appid")
            .secret(passwordEncoder.encode("jarye_pwd"))
            // 授权码
            .authorizedGrantTypes("authorization_code")
            // 作用域 表示所有的接口都可以访问 分配我们的appid 调用接口的权限
            .scopes("all")
            // 资源ID
            .resourceIds("jarye_resource")
            // 用户选择授权之后,跳转到该地址传递code授权码
            .redirectUris("http://www.jarye.cn/callback");
}
/**
* 功能描述:远程令牌校验的服务
*/
@Primary
@Bean
public RemoteTokenServices remoteTokenServices() {
    final RemoteTokenServices tokenServices = new RemoteTokenServices();
    //设置授权服务器check_token端点完整地址
    tokenServices.setCheckTokenEndpointUrl("http://localhost:8080/oauth/check_token");
    //设置客户端id与secret,注意:client_secret值不能使用passwordEncoder加密!
    tokenServices.setClientId(appId);
    tokenServices.setClientSecret(appSecret);
    return tokenServices;
}

/**
* 功能描述:配置授权信息
*/
@Override
public void configure(HttpSecurity http) throws Exception {
    //设置创建session策略
    http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED);
    //@formatter:off
    //所有请求必须授权
    http.authorizeRequests()
            .anyRequest().authenticated();
    //@formatter:on
}

/**
* 功能描述:配置资源信息
*/
@Override
public void configure(ResourceServerSecurityConfigurer resources) {
    resources.resourceId("jarye_resource").stateless(true);
}

相关文章链接:
<<<Web常用攻击手段-XSS攻击
<<<Web常用攻击手段-SQL注入
<<<Web常用攻击手段-Http请求防盗链
<<<Web常用攻击手段-CSRF攻击
<<<Web常用攻击手段-上传文件漏洞
<<<Web常用攻击手段-忘记密码
<<<Web常用攻击手段-其他漏洞
<<<安全技术--数据加密/认证技术
<<<安全技术--Https相关知识
<<<安全技术--接口幂等性设计
<<<安全框架--SpringSecurity
<<<安全框架--JWT
<<<安全架构整体设计方案

举报

相关推荐

0 条评论