栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

OAuth2调研与案例

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

OAuth2调研与案例

授权框架使得第三方获取对用户的资源的访问

eg:bilibili可以去微信服务器获取你的头像、昵称、openid等

术语

专业称呼

通俗叫法

资源所有者

用户

资源服务器

保存用户信息的服务器并且能验证令牌是否合法

客户端

第三方应用服务

授权服务器

发送令牌到第三方服务器

1、授权码模式、

最常用的模式,前后端分离,避免令牌泄露。

隐式模式、密码模式、客户端模式

一、spring security OAuth2

1.1、授权服务器

    • authorize endpoint:授权端点,进行授权
    • token endpoint : 令牌端点,授权拿到对应的token
    • introspection endpoint: 校验端点,校验token合法性
    • revocat endpoint : 撤销端点,撤销授权

二、实际案例 授权码模式

@Configuration
@EnableWebSecurity  //启用security
public class SecurityConfig  extends WebSecurityConfigurerAdapter {

    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()//关闭csrf
                .authorizeRequests()
                .antMatchers("/oauth
    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .anyRequest()
                .authenticated()//所有请求需要认证
                .and()
                .requestMatchers()
                .antMatchers("/user
  @Override
  public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
    endpoints.authenticationManager(authenticationManager)
            .userDetailsService(userService);
  }

直接申请token 不许要回调发送授权码在申请token

 

{
    "access_token": "8217fb9e-3e2a-4467-bcfd-541206afca4b",
    "token_type": "bearer",
    "expires_in": 3599,
    "scope": "all"
}
{
    "password": null,
    "username": "sen",
    "authorities": [
        {
            "authority": "ROLE_sale"
        },
        {
            "authority": "admin"
        }
    ],
    "accountNonExpired": true,
    "accountNonLocked": true,
    "credentialsNonExpired": true,
    "enabled": true
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/981140.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号