{"id":4658,"date":"2020-01-15T15:19:11","date_gmt":"2020-01-15T07:19:11","guid":{"rendered":"https:\/\/www.xttblog.com\/?p=4658"},"modified":"2020-01-15T15:19:11","modified_gmt":"2020-01-15T07:19:11","slug":"springboot-%e9%9b%86%e6%88%90-justauth-%e6%95%b4%e5%90%88-github-%e7%ac%ac%e4%b8%89%e6%96%b9%e7%99%bb%e5%bd%95","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=4658","title":{"rendered":"SpringBoot \u96c6\u6210 justauth \u6574\u5408 github \u7b2c\u4e09\u65b9\u767b\u5f55"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u6700\u8fd1\u6709\u4e00\u4f4d\u65b0\u540c\u4e8b\u5728\u7814\u7a76\u7b2c\u4e09\u65b9\u767b\u5f55\u529f\u80fd\uff0c\u8fd9\u4e2a\u529f\u80fd\u5176\u5b9e\u4e5f\u5f88\u7b80\u5355\uff0c\u4e1a\u754c\u4e5f\u6709\u8457\u540d\u7684\u5f00\u6e90\u6574\u5408\u65b9\u6848\u3002\u6211\u8fd9\u91cc\u5c31\u8bf4\u4e00\u4e0b\uff0cSpringBoot \u96c6\u6210 justauth \u6574\u5408 github \u7b2c\u4e09\u65b9\u767b\u5f55\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u9047\u5230\u7684\u4e00\u4e9b\u95ee\u9898\u548c\u5927\u5bb6\u5206\u4eab\u4e00\u4e0b\uff01<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6574\u5408\u8fc7\u7a0b\u5f88\u7b80\u5355\uff0c\u7b2c\u4e00\u6b65\u6539\u9020\u4f60\u7684 pom.xml\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;properties&gt;\n    &lt;project.build.sourceEncoding&gt;UTF-8&lt;\/project.build.sourceEncoding&gt;\n    &lt;project.reporting.outputEncoding&gt;UTF-8&lt;\/project.reporting.outputEncoding&gt;\n    &lt;java.version&gt;1.8&lt;\/java.version&gt;\n&lt;\/properties&gt;\n&lt;dependencies&gt;\n    &lt;dependency&gt;\n        &lt;groupId&gt;com.xkcoding.justauth&lt;\/groupId&gt;\n        &lt;artifactId&gt;justauth-spring-boot-starter&lt;\/artifactId&gt;\n        &lt;version&gt;1.3.2&lt;\/version&gt;\n    &lt;\/dependency&gt;\n&lt;\/dependencies&gt;\n&lt;build&gt;\n    &lt;plugins&gt;\n        &lt;plugin&gt;\n            &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n            &lt;artifactId&gt;spring-boot-maven-plugin&lt;\/artifactId&gt;\n        &lt;\/plugin&gt;\n    &lt;\/plugins&gt;\n&lt;\/build&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u91cc\u8981\u6ce8\u610f\uff0c\u4e0d\u8981\u5f15\u5165 Spring Security\uff0c\u56e0\u4e3a\u5f15\u5165 Security \u540e\uff0c\u65e0\u8bba\u4f60\u600e\u4e48\u767b\u5f55\uff0c\u90fd\u8fd8\u662f\u4f1a\u5f39\u51fa Security \u7684\u767b\u5f55\u5bf9\u8bdd\u6846\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/github-justauth.jpg\" alt=\"\" \/><figcaption>Springboot \u6574\u5408 github \u7b2c\u4e09\u65b9\u767b\u5f55<\/figcaption><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u7b2c\u4e8c\u6b65\u7f16\u5199\u4e00\u4e2a\u56de\u8c03\u5730\u5740\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Slf4j\n@RestController\n@RequestMapping(\"\/oauth\")\npublic class XttblogController {\n    @Autowired\n    private AuthRequestFactory factory;\n    @GetMapping\n    public List&lt;String&gt; list() {\n        return factory.oauthList();\n    }\n    @GetMapping(\"\/login\/{type}\")\n    public void login(@PathVariable String type, \n        HttpServletResponse response) throws IOException {\n        AuthRequest authRequest = factory.get(type);\n        response.sendRedirect(authRequest.authorize(AuthStateUtils.createState()));\n    }\n    @RequestMapping(\"\/callback\/{type}\")\n    public AuthResponse login(@PathVariable String type, AuthCallback callback) {\n        AuthRequest authRequest = factory.get(type);\n        AuthResponse response = authRequest.login(callback);\n        log.info(\"\u3010response\u3011= {}\", JSONUtil.toJsonStr(response));\n        return response;\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u7b2c\u4e09\u6b65\uff0c\u8bbf\u95ee https:\/\/github.com\/settings\/developers\uff0c\u521b\u5efa\u7b2c\u4e09\u65b9\u5e94\u7528\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u5e94\u7528\u7ba1\u7406\u9875\u9762\uff0c\u9009\u62e9 OAuth App\uff0c\u7136\u540e\u70b9\u51fb\u53f3\u4e0a\u65b9\u7684 New OAuth App \u6309\u94ae\u8fdb\u5165\u65b0\u5efa\u5e94\u7528\u9875\u9762\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/github-just-auth.png\" alt=\"\" \/><figcaption>Github \u914d\u7f6e oauth \u5e94\u7528<\/figcaption><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u914d\u7f6e\u5b8c\u6210\u540e\uff0c\u4f60\u4f1a\u83b7\u5f97\u4e00\u4e2a Client ID\uff1a7bbXXXX4d2 \u548c Client Secret\uff1a8247bXXXX5bfe1\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7b2c\u56db\u6b65\uff0c\u5728 application.yml \u4e2d\u914d\u7f6e\u4e00\u4e0b\u5185\u5bb9\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>justauth:\n  enabled: true\n  type:\n    github:\n      client-id: 7bb1bXXXX824d2\n      client-secret: 8247741acdXXXXb50841b5bfe1\n      redirect-uri: http:\/\/localhost:8080\/oauth\/callback\/github\n  cache:\n    type: default\n\nspring:\n  session:\n    store-type: none<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5927\u529f\u544a\u6210\u4e4b\u540e\uff0c\u542f\u52a8 SpringBoot \u7a0b\u5e8f\uff0c\u5728\u6d4f\u89c8\u5668\u91cc\u8f93\u5165\uff1ahttp:\/\/localhost:8080\/oauth\/login\/github\uff0c\u5c31\u4f1a\u8df3\u8f6c\u5230 github \u7684\u767b\u5f55\u6388\u6743\u9875\u9762(\u5982\u679c\u4f60\u5df2\u7ecf\u767b\u5f55\uff0c\u5219\u76f4\u63a5\u8df3\u8f6c\u5230\u6388\u6743\u9875\u9762\uff1b\u5982\u679c\u4f60\u6ca1\u6709\u767b\u5f55\uff0c\u5219 github \u4f1a\u63d0\u793a\u4f60\u8fdb\u884c\u767b\u5f55)\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/WX20200115-150940%402x.png\" alt=\"\" \/><figcaption>Github \u6388\u6743\u767b\u5f55<\/figcaption><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u70b9\u51fb\u786e\u5b9a\u6388\u6743\u540e\uff0c\u5982\u679c\u4f60\u6536\u5230 code = 5009\uff0cIllegal state [GITHUB] \u7684\u4fe1\u606f\u3002\u5219\u8bf4\u660e\u4f60\u7684\u72b6\u6001\u5f02\u5e38\u4e86\uff0c\u6709\u53ef\u80fd\u662f\u4f60\u505c\u5728\u6388\u6743\u7684\u9875\u9762\u65f6\u95f4\u592a\u4e45\u4e86\uff0c\u4f60\u91cd\u65b0\u5237\u65b0\uff0c\u91cd\u65b0\u6388\u6743\u5373\u53ef\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0a\uff0c\u9700\u8981\u6e90\u7801\u7684\u6216\u9047\u5230\u56f0\u96be\u7684\u8bf7\u52a0\u6211\u5fae\u4fe1\u53f7\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=4658\">SpringBoot \u96c6\u6210 justauth \u6574\u5408 github \u7b2c\u4e09\u65b9\u767b\u5f55<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u6709\u4e00\u4f4d\u65b0\u540c\u4e8b\u5728\u7814\u7a76\u7b2c\u4e09\u65b9\u767b\u5f55\u529f\u80fd\uff0c\u8fd9\u4e2a\u529f\u80fd\u5176\u5b9e\u4e5f\u5f88\u7b80\u5355\uff0c\u4e1a\u754c\u4e5f\u6709\u8457\u540d\u7684\u5f00\u6e90\u6574\u5408\u65b9\u6848\u3002\u6211\u8fd9\u91cc\u5c31\u8bf4\u4e00\u4e0b\uff0cSpr [&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":[1437,996,980,587,184],"class_list":["post-4658","post","type-post","status-publish","format-standard","hentry","category-java","tag-github","tag-shiro-","tag-spring-boot","tag-184"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/4658","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=4658"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/4658\/revisions"}],"predecessor-version":[{"id":4659,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/4658\/revisions\/4659"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4658"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4658"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4658"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}