前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >OAuth2简易实战(三)-JWT

OAuth2简易实战(三)-JWT

作者头像
老梁
发布2019-09-10 18:31:05
4820
发布2019-09-10 18:31:05
举报
文章被收录于专栏:Java工程师成长之路

1. OAuth2简易实战(三)-JWT

1.1. 与OAuth2授权码模式差别

  1. 授权服务器代码修改
代码语言:javascript
复制
@Configuration
@EnableAuthorizationServer
public class OAuth2AuthorizationServer extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private AuthenticationManager authenticationManager;

    @Bean
    public JwtAccessTokenConverter accessTokenConverter() {
        JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
        converter.setSigningKey("test-secret");
        return converter;
    }

    @Bean
    public JwtTokenStore jwtTokenStore() {
        return new JwtTokenStore(accessTokenConverter());
    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints
            .authenticationManager(authenticationManager)
            .tokenStore(jwtTokenStore())
            .accessTokenConverter(accessTokenConverter());
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory()
            .withClient("clientapp")
            .secret("112233")
            .scopes("read_userinfo")
            .authorizedGrantTypes(
                "password",
                "authorization_code",
                "refresh_token");
    }

}
  1. 可以看到主要是增加了 JwtAccessTokenConverter JWT访问令牌转换器和JwtTokenStore JWT令牌存储组件,通过AuthorizationServerEndpointsConfigurer 授权服务器端点配置加入两个实例

1.2. 操作步骤

  1. 使用password模式,访问以下链接,获得token,记得加上授权账户密码
代码语言:javascript
复制
http://localhost:8080/oauth/token?password=xyz&grant_type=password&username=bobo&scope=read_userinfo
  1. 返回值
  1. 使用access_token调用,成功

1.3. JWT的特殊性

  1. 可以看到代码改动其实不大,jwt具有自解释的特性,客户端不需要再去授权服务器认证这个token的合法性
  2. 代码中可以看到我们添加了一个签名秘钥test-secret,这个秘钥需要自己保管好
  3. 通过访问 https://jwt.io 把我试验中返回的access_token加入,填上签名秘钥,可以看到验证成功
  1. 可以看出来,通过token的解码,参数中带有请求的一些信息,我们通过解码可以直接获取
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-03-14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. OAuth2简易实战(三)-JWT
    • 1.1. 与OAuth2授权码模式差别
      • 1.2. 操作步骤
        • 1.3. JWT的特殊性
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档