{"id":3742,"date":"2019-01-16T16:33:46","date_gmt":"2019-01-16T08:33:46","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=3742"},"modified":"2019-01-16T16:33:46","modified_gmt":"2019-01-16T08:33:46","slug":"spring-security-oauth2-%e4%bd%bf%e7%94%a8-tokenenhancer-%e8%87%aa%e5%ae%9a%e4%b9%89%e7%94%9f%e6%88%90%e4%bb%a4%e7%89%8c","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=3742","title":{"rendered":"spring-security-oauth2 \u4f7f\u7528 TokenEnhancer \u81ea\u5b9a\u4e49\u751f\u6210\u4ee4\u724c"},"content":{"rendered":"<p>\n\t\u4e0d\u7ba1\u662f spring-security-oauth2 \u6846\u67b6\uff0c\u8fd8\u662f\u5176\u4ed6\u6846\u67b6\uff0c\u5b83\u4eec\u7684\u5171\u540c\u70b9\u5c31\u662f\u53ef\u4ee5\u9ad8\u5ea6\u5b9a\u5236\u5316\u3002\u4eca\u5929\u6211\u4eec\u5c31\u6765\u4e2a\u5b9a\u5236\u5316\u7b2c\u4e8c\u7bc7\uff0c\u4f7f\u7528 TokenEnhancer \u81ea\u5b9a\u4e49\u751f\u6210\u4ee4\u724c\u3002\n<\/p>\n<p>\n\t\u6240\u8c13\u4ee4\u724c\uff0c\u4e5f\u5c31\u662f\u6211\u4eec\u5e38\u8bf4\u7684 Token\u3002\u901a\u8fc7\u6211\u7684\u8fd9\u7bc7\u6587\u7ae0\u300a<a href=\"https:\/\/www.xttblog.com\/?p=3728\">SpringBoot + spring-security-oauth2 \u5b9e\u73b0\u4eff\u5fae\u4fe1\uff0cQQ\uff0c\u5fae\u535a\u7b49\u6388\u6743\u8ba4\u8bc1<\/a>\u300b\u53ef\u4ee5\u77e5\u9053\uff0c\u6211\u4eec\u62ff\u5230 Token\uff0c\u4e5f\u5c31\u662f\u4ee4\u724c\u540e\u53ef\u4ee5\u83b7\u53d6\u5bf9\u5e94\u7684\u8d44\u6e90\u3002access_token \u548c refresh_token \u90fd\u5f88\u91cd\u8981\u3002\n<\/p>\n<p>\n\t\u7136\u540e\uff0c\u6211\u4eec\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\u300a<a href=\"https:\/\/www.xttblog.com\/?p=3732\">SpringBoot + Spring Security OAuth2 \u7684\u7b80\u5355\u5b9e\u73b0<\/a>\u300b\u77e5\u9053\uff0cspring-security-oauth2 \u4e2d\u9ed8\u8ba4\u7684 Token \u751f\u6210\u65b9\u5f0f\u662f\u4e00\u4e2a UUID\u3002\u4f46\u662f\u73b0\u5728\u6211\u4e0d\u60f3\u7528 UUID \u6765\u751f\u6210 Token\uff0c\u6211\u60f3\u5c06\u6240\u6709\u7684 Token \u524d\u7f00\u90fd\u5e26\u4e0a&ldquo;\u4e1a\u4f59\u8349&rdquo;\uff0c\u90a3\u8be5\u600e\u4e48\u505a\u5462\uff1f\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"\u5b9a\u5236\u5316\u5f00\u53d1\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/wx1.sinaimg.cn\/large\/8fa5dcfcgy1fz8icycdvnj20fp0aon0o.jpg\" style=\"width: 565px;height: 384px\" \/>\n<\/p>\n<p>\n\t\u7b54\u6848\u5c31\u662f\u5b9e\u73b0&nbsp;TokenEnhancer \u63a5\u53e3\u3002\u5e76\u91cd\u5199\u5b83\u7684 enhance \u65b9\u6cd5\u3002\u5177\u4f53\u4ee3\u7801\u5982\u4e0b\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\npublic class CustomTokenEnhancer implements TokenEnhancer {  \r\n    @Override  \r\n    public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, \r\n        OAuth2Authentication authentication) {  \r\n        if (accessToken instanceof DefaultOAuth2AccessToken) {  \r\n            DefaultOAuth2AccessToken token = ((DefaultOAuth2AccessToken) accessToken);  \r\n            token.setValue(getNewToken());  \r\n            OAuth2RefreshToken refreshToken = token.getRefreshToken();  \r\n            if (refreshToken instanceof DefaultOAuth2RefreshToken) {  \r\n                token.setRefreshToken(new DefaultOAuth2RefreshToken(getNewToken()));  \r\n            }  \r\n            Map&lt;String, Object&gt; additionalInformation = new HashMap&lt;String, Object&gt;();  \r\n            additionalInformation.put(&quot;client_id&quot;, authentication.getOAuth2Request().getClientId());  \r\n            token.setAdditionalInformation(additionalInformation);  \r\n            return token;  \r\n        }  \r\n        return accessToken;  \r\n    }  \r\n    private String getNewToken() {  \r\n        return &quot;\u4e1a\u4f59\u8349&quot; + UUID.randomUUID().toString().replace(&quot;-&quot;, &quot;&quot;);  \r\n    }  \r\n}  <\/pre>\n<p>\n\t\u7136\u540e\u5728 AuthorizationServerConfigurerAdapter \u4e2d\u4f7f\u7528\u5b83\u3002\n<\/p>\n<pre class=\"brush:java;\">\r\n@Configuration  \r\n@EnableAuthorizationServer  \r\npublic class AuthorizationServerConfiguration \r\n    extends AuthorizationServerConfigurerAdapter {  \r\n    @Bean  \r\n    public TokenEnhancer tokenEnhancer() {  \r\n        return new CustomTokenEnhancer();  \r\n    }  \r\n} <\/pre>\n<p>\n\t\u8fd0\u884c\u6548\u679c\uff0c\u6211\u5c31\u4e0d\u622a\u56fe\u4e86\u3002\u60f3\u8981\u6e90\u7801\u7684\u53ef\u4ee5\u52a0\u6211\u5fae\u4fe1\u597d\u53cb\uff01<\/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=3742\">spring-security-oauth2 \u4f7f\u7528 TokenEnhancer \u81ea\u5b9a\u4e49\u751f\u6210\u4ee4\u724c<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u4e0d\u7ba1\u662f spring-security-oauth2 \u6846\u67b6\uff0c\u8fd8\u662f\u5176\u4ed6\u6846\u67b6\uff0c\u5b83\u4eec\u7684\u5171\u540c\u70b9\u5c31\u662f\u53ef\u4ee5\u9ad8\u5ea6\u5b9a\u5236\u5316\u3002\u4eca\u5929 [&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,6],"tags":[1001,418,1317,990,420],"class_list":["post-3742","post","type-post","status-publish","format-standard","hentry","category-java","category-yyzt","tag-oauth2","tag-securitymanager","tag-security_err","tag-sessionssecuritymanager","tag-usernamepasswordtoken"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3742","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=3742"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3742\/revisions"}],"predecessor-version":[{"id":3743,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3742\/revisions\/3743"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3742"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3742"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3742"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}