{"id":3736,"date":"2019-01-15T17:47:42","date_gmt":"2019-01-15T09:47:42","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=3736"},"modified":"2020-02-06T15:21:42","modified_gmt":"2020-02-06T07:21:42","slug":"spring-security-oauth2-%e8%87%aa%e5%ae%9a%e4%b9%89%e7%94%9f%e6%88%90%e6%8e%88%e6%9d%83%e7%a0%81","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=3736","title":{"rendered":"spring-security-oauth2 \u81ea\u5b9a\u4e49\u751f\u6210\u6388\u6743\u7801"},"content":{"rendered":"<p>\u4ece\u524d\u9762\u7684\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4e86\u89e3\u5230 spring-security-oauth2 \u4ea7\u751f\u7684\u6388\u6743\u7801\u9ed8\u8ba4\u662f 6 \u4f4d\u7684\u3002<\/p>\n<p>\u5e76\u4e14\uff0c\u6211\u4eec\u901a\u8fc7\u62ff\u5230 code \u540e\uff0c\u518d\u901a\u8fc7 code \u83b7\u53d6 access_token\u3002\u90a3\u4e48\u4f1a\u4e0d\u4f1a\u56e0\u4e3a code \u592a\u77ed\uff0c\u6216\u8005 code \u592a\u7b80\u5355\u88ab\u731c\u4e2d\u4e86\uff0c\u7136\u540e\u83b7\u53d6 access_token \u5462\uff1f<\/p>\n<p>\u6211\u4eec\u80fd\u4e0d\u80fd\u628a Code \u7ed9\u53d8\u957f\u4e00\u70b9\uff0c\u6216\u8005\u6211\u4eec\u60f3\u8981\u6709\u81ea\u5df1\u7684\u5b9e\u73b0\uff01<\/p>\n<p>\u901a\u8fc7\u6211\u5bf9\u4ee3\u7801\u7684\u8ffd\u8e2a\uff0c\u4ee5\u53ca\u6211\u524d\u9762\u8bf4\u7684\uff0cURL \u4e2d\u7684 code \u53c2\u6570\u503c\u5373\u4e3a\u6388\u6743\u7801\uff0c\u5b83\u662f\u5728 org.springframework.security.oauth2.common.util.RandomValueStringGenerator.generate() \u4e2d\u751f\u5b58\u7684\u3002\u90a3\u6211\u5c31\u627e\u770b\u8c01\u8c03\u7528\u4e86\u5b83\uff0c\u6700\u7ec8\u53d1\u73b0\u6211\u4eec\u53ea\u9700\u8981\u901a\u8fc7\u6269\u5c55 AuthorizationCodeServices \u6765\u8986\u5199\u5df2\u6709\u7684\u751f\u6210\u89c4\u5219\u3002\u901a\u8fc7\u8986\u5199 createAuthorizationCode() \u65b9\u6cd5\u53ef\u4ee5\u8bbe\u7f6e\u6210\u4efb\u610f\u7684\u751f\u6210\u89c4\u5219\u3002&nbsp;<\/p>\n<p>\u6269\u5c55 AuthorizationServerConfigurerAdapter \u548c JdbcAuthorizationCodeServices \u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush:java;\">@Configuration  \n@EnableAuthorizationServer  \npublic class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {\n    @Bean\n    protected AuthorizationCodeServices authorizationCodeServices() {\n        return new CustomJdbcAuthorizationCodeServices(dataSource());\n    }\n}  \npublic class CustomJdbcAuthorizationCodeServices extends JdbcAuthorizationCodeServices {\n    private RandomValueStringGenerator generator = new RandomValueStringGenerator();\n    public CustomJdbcAuthorizationCodeServices(DataSource dataSource) {\n        super(dataSource);\n        this.generator = new RandomValueStringGenerator(32);\n    }\n    public String createAuthorizationCode(OAuth2Authentication authentication) {\n        String code = this.generator.generate();\n        store(code, authentication);\n        return code;\n    }\n}<\/pre>\n<p>\u77ed\u77ed 20 \u884c\u4ee3\u7801\u641e\u5b9a\u4e00\u5207\u3002\u6700\u7ec8\u8fd0\u884c\u6548\u679c\u56fe\uff0c\u622a\u56fe\u5982\u4e0b\uff1a<\/p>\n<p><img decoding=\"async\" style=\"width: 853px;height: 81px\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/wx1.sinaimg.cn\/large\/8fa5dcfcgy1fz7era3r7xj20np029jrl.jpg\" alt=\"AuthorizationCodeServices\"><\/p>\n<p>\u9664\u6b64\u4e4b\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u81ea\u5b9a\u4e49\u751f\u6210 Token\u3001\u6388\u6743\u9875\u3001\u8ba4\u8bc1\u65b9\u5f0f\u3001\u767b\u5f55\u9875\u9762\u7b49\u3002\u6211\u4eec\u540e\u9762\u7ee7\u7eed\u6df1\u5165\u5b66\u4e60 spring-security-oauth2\u3002<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=3736\">spring-security-oauth2 \u81ea\u5b9a\u4e49\u751f\u6210\u6388\u6743\u7801<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u4ece\u524d\u9762\u7684\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4e86\u89e3\u5230 spring-security-oauth2 \u4ea7\u751f\u7684\u6388\u6743\u7801\u9ed8\u8ba4\u662f 6 \u4f4d\u7684\u3002  [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[1887,1892,1888,1893,1001],"class_list":["post-3736","post","type-post","status-publish","format-standard","hentry","category-java","tag-enableauthorizationserver","tag-authorizationcodeservices","tag-authorizationserverconfigureradapter","tag-jdbcauthorizationcodeservices","tag-oauth2"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3736","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3736"}],"version-history":[{"count":2,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3736\/revisions"}],"predecessor-version":[{"id":4736,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3736\/revisions\/4736"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3736"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3736"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3736"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}