{"id":3732,"date":"2019-01-15T15:07:55","date_gmt":"2019-01-15T07:07:55","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=3732"},"modified":"2019-01-15T15:07:55","modified_gmt":"2019-01-15T07:07:55","slug":"springboot-spring-security-oauth2-%e7%9a%84%e7%ae%80%e5%8d%95%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=3732","title":{"rendered":"SpringBoot + Spring Security OAuth2 \u7684\u7b80\u5355\u5b9e\u73b0"},"content":{"rendered":"<p>\n\t\u968f\u7740 OAuth2\uff08\u5f00\u653e\u6388\u6743\uff09\u7684\u706b\u70ed\uff0c\u8d8a\u6765\u8d8a\u591a\u7684\u7f51\u7ad9\u9009\u62e9\u4f7f\u7528 OAuth2 \u6280\u672f\u83b7\u5f97\u7528\u6237\u3002\u8fd9\u6837\u505a\u4e0d\u9700\u8981\u7528\u6237\u8fc7\u591a\u7684\u6ce8\u518c\u8d26\u53f7\uff0c\u7ef4\u62a4\u8d26\u53f7\u4ee5\u53ca\u62c5\u5fc3\u7528\u6237\u4e2a\u4eba\u4fe1\u606f\u6cc4\u9732\u7b49\u95ee\u9898\u3002\u6240\u4ee5\u8bf4\u4e0e OAuth2 \u76f8\u5173\u7684\u5173\u952e\u8bcd\u90fd\u706b\u70ed\u4e86\u8d77\u6765\u3002\u6bd4\u5982\uff1aOAuth 2 c#\u3001oauth 2.0 \u962e\u4e00\u5cf0\u3001spring OAuth2\u3001oauth2\u4f7f\u7528\u3001oauth2\u4e0ejwt\u3001oauth2.0 \u8ba4\u8bc1\u3001oauth2 cookie\u3001oauth2 sso\u3001ajax token\u8ba4\u8bc1\u5b9e\u4f8b\u3001oauth2\u6d41\u7a0b\u3001oauth2 jwt\u3001oauth2 token\u3001oauth2 spring\u3001oauth2 java\u3001oauth2 server\u3001oauth2 code\u3001oauth2 grant_type\u3001oauth2\u7ef4\u57fa\u3001oauth2 scope\u7b49\u3002\u672c\u6587\u6211\u4eec\u6765\u505a\u4e00\u4e2a OAuth2 \u7684\u7b80\u5355\u5b9e\u73b0\u6848\u4f8b\u3002\n<\/p>\n<p>\n\t\u7f51\u4e0a\u6709\u5f88\u591a\u5173\u4e8e OAuth2 \u7684\u76f8\u5173\u5f00\u6e90\u5b9e\u73b0\u3002\u6211\u4eec\u4eca\u5929\u501f\u52a9 spring-security-oauth2 \u5b9e\u73b0 Authorization Code Grant \u6388\u6743\u6a21\u5f0f\u3002&nbsp;\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"OAuth2 \u7684\u8ba4\u8bc1\u6d41\u7a0b\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/wx1.sinaimg.cn\/large\/8fa5dcfcgy1fz793g7qa8j20m80d3q44.jpg\" style=\"width: 800px;height: 471px\" \/>\n<\/p>\n<p>\n\t\u4e0a\u56fe\u7684\u8fd9\u4e2a OAuth2 \u6388\u6743\u8ba4\u8bc1\u8fc7\u7a0b\u6709\u51e0\u4e2a\u91cd\u8981\u7684 URL\uff0c\u4e5f\u662f spring-security-oauth2 \u4e2d\u7684\u6838\u5fc3 URL\u3002\u5177\u4f53\u5217\u4e3e\u5982\u4e0b\uff1a\n<\/p>\n<ul>\n<li>\n\t\t\/oauth\/authorize \uff1a \u6388\u6743AuthorizationEndpoint\n\t<\/li>\n<li>\n\t\t\/oauth\/token \uff1a \u4ee4\u724cTokenEndpoint\n\t<\/li>\n<li>\n\t\t\/oauth\/check_token \uff1a \u4ee4\u724c\u6821\u9a8cCheckTokenEndpoint\n\t<\/li>\n<li>\n\t\t\/oauth\/confirm_access \uff1a \u6388\u6743\u9875\u9762WhitelabelApprovalEndpoint\n\t<\/li>\n<li>\n\t\t\/oauth\/error \uff1a \u9519\u8bef\u9875\u9762WhitelabelErrorEndpoint\n\t<\/li>\n<\/ul>\n<p>\n\t\u540e\u9762\u6211\u4eec\u6240\u6709\u7684\u4f8b\u5b50\uff0c\u57fa\u672c\u90fd\u662f\u56f4\u7ed5\u8fd9 5 \u4e2a URL \u5c55\u5f00\u3002\n<\/p>\n<p>\n\t\u65b0\u5efa\u4e00\u4e2a xttblog-oauth2-server \u5de5\u7a0b\u3002pom.xml \u6587\u4ef6\u4e2d\u7684\u914d\u7f6e\u5982\u4e0b\uff1a\n<\/p>\n<pre class=\"brush:xml;\">\r\n&lt;dependencies&gt;\r\n    &lt;dependency&gt;\r\n        &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\r\n        &lt;artifactId&gt;spring-boot-starter-web&lt;\/artifactId&gt;\r\n    &lt;\/dependency&gt;\r\n    &lt;dependency&gt;\r\n        &lt;groupId&gt;org.springframework.security.oauth&lt;\/groupId&gt;\r\n        &lt;artifactId&gt;spring-security-oauth2&lt;\/artifactId&gt;\r\n        &lt;version&gt;${oauth.version}&lt;\/version&gt;\r\n    &lt;\/dependency&gt;\r\n    &lt;dependency&gt;\r\n        &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\r\n        &lt;artifactId&gt;spring-boot-starter-thymeleaf&lt;\/artifactId&gt;\r\n    &lt;\/dependency&gt;\r\n&lt;\/dependencies&gt;<\/pre>\n<p>\n\t@EnableAuthorizationServer \u6ce8\u89e3\u76f8\u5173\u7684\u4f7f\u7528\u5982\u4e0b\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\npublic class Config {  \r\n    public static final String OAUTH_CLIENT_ID = &quot;oauth_client&quot;;  \r\n    public static final String OAUTH_CLIENT_SECRET = &quot;oauth_client_secret&quot;;  \r\n    public static final String RESOURCE_ID = &quot;my_resource_id&quot;;  \r\n    public static final String[] SCOPES = { &quot;read&quot;, &quot;write&quot; };  \r\n    @Configuration  \r\n    @EnableAuthorizationServer  \r\n    static class OAuthAuthorizationConfig extends AuthorizationServerConfigurerAdapter {  \r\n        @Override  \r\n        public void configure(ClientDetailsServiceConfigurer clients) throws Exception {  \r\n            clients.inMemory()  \r\n                .withClient(OAUTH_CLIENT_ID)  \r\n                .secret(OAUTH_CLIENT_SECRET)  \r\n                .resourceIds(RESOURCE_ID)  \r\n                .scopes(SCOPES)  \r\n                .authorities(&quot;ROLE_USER&quot;)  \r\n                .authorizedGrantTypes(&quot;authorization_code&quot;, &quot;refresh_token&quot;)  \r\n                .redirectUris(&quot;http:\/\/default-oauth-callback.com&quot;)  \r\n                .accessTokenValiditySeconds(60*30) \/\/ 30min  \r\n                .refreshTokenValiditySeconds(60*60*24); \/\/ 24h  \r\n        }  \r\n    }  \r\n    @Configuration  \r\n    @EnableResourceServer  \r\n    static class OAuthResourceConfig extends ResourceServerConfigurerAdapter {  \r\n        @Override  \r\n        public void configure(ResourceServerSecurityConfigurer resources) throws Exception {  \r\n            resources.resourceId(RESOURCE_ID);  \r\n        }  \r\n        @Override  \r\n        public void configure(HttpSecurity http) throws Exception {  \r\n            http.authorizeRequests()  \r\n                .antMatchers(HttpMethod.GET, &quot;\/api\/**&quot;).access(&quot;#oauth2.hasScope(&#039;read&#039;)&quot;)  \r\n                .antMatchers(HttpMethod.POST, &quot;\/api\/**&quot;).access(&quot;#oauth2.hasScope(&#039;write&#039;)&quot;);  \r\n        }  \r\n    }  \r\n    @Configuration  \r\n    @EnableWebSecurity  \r\n    static class SecurityConfig extends WebSecurityConfigurerAdapter {  \r\n        @Override  \r\n        protected void configure(AuthenticationManagerBuilder auth) throws Exception {  \r\n            auth.inMemoryAuthentication()  \r\n                .withUser(&quot;user&quot;).password(&quot;123&quot;).roles(&quot;USER&quot;)  \r\n                .and()  \r\n                .withUser(&quot;admin&quot;).password(&quot;123&quot;).roles(&quot;ADMIN&quot;);  \r\n        }  \r\n        @Override  \r\n        protected void configure(HttpSecurity http) throws Exception {  \r\n            http.csrf().disable();  \r\n            http.authorizeRequests()  \r\n                .antMatchers(&quot;\/oauth\/authorize&quot;).authenticated()  \r\n                .and()  \r\n                .httpBasic().realmName(&quot;OAuth Server&quot;);  \r\n        }  \r\n    }  \r\n}  <\/pre>\n<p>\n\tController \u7c7b\uff0c\u6d4b\u8bd5\u6388\u6743\u540e\u624d\u80fd\u8bbf\u95ee\u3002\n<\/p>\n<pre class=\"brush:java;\">\r\n@RestController  \r\npublic class Controller {  \r\n    @GetMapping(&quot;\/api\/get&quot;)  \r\n    public String get() {  \r\n        return &quot;Hello World!&quot;;  \r\n    }  \r\n    @PostMapping(&quot;\/api\/post&quot;)  \r\n    public String post() {  \r\n        return &quot;POST process has finished.&quot;;  \r\n    }  \r\n    @GetMapping(&quot;\/api\/user&quot;)  \r\n    public Object get(HttpServletRequest req) {  \r\n        SecurityContextImpl sci = (SecurityContextImpl) \r\n        req.getSession().getAttribute(&quot;SPRING_SECURITY_CONTEXT&quot;);  \r\n        if (sci != null) {  \r\n            Authentication authentication = sci.getAuthentication();  \r\n            if (authentication != null) {  \r\n                return authentication.getPrincipal();  \r\n            }  \r\n        }  \r\n        return &quot;none&quot;;  \r\n    }  \r\n}  <\/pre>\n<p>\n\tSpringBoot \u542f\u52a8\u7c7b\u4ee3\u7801\u5982\u4e0b\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\n@SpringBootApplication  \r\npublic class Application {  \r\n    public static void main(String[] args) {  \r\n        SpringApplication.run(Application.class, args);  \r\n    }  \r\n}  <\/pre>\n<p>\n\t\u4e0a\u9762\u7684 oauth_client \u548c oauth_client_secret \u662f\u4e24\u4e2a Base64 \u7684\u52a0\u5bc6\u5b57\u7b26\u4e32\u3002\u5bf9\u63a5\u8fc7\u5fae\u4fe1\u7684\u5e94\u8be5\u90fd\u4e86\u89e3\u3002\n<\/p>\n<p>\n\t\u7136\u540e\u6211\u4eec\u542f\u52a8\u7a0b\u5e8f\uff0c\u8bbf\u95ee \/api\/get \u3002\u8bf7\u6c42\u5c31\u4f1a\u88ab\u62e6\u622a\u5230 \/oauth\/authorize \u8fdb\u884c HTTP Basic \u8ba4\u8bc1\uff0c\u56e0\u4e3a\u6211\u4eec\u524d\u9762\u5728 WebSecurityConfigurerAdapter \u4e2d\u5f00\u542f\u4e86 HTTP Basic \u8ba4\u8bc1\u3002\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"OAuth2 HTTP Basic \u8ba4\u8bc1\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/wx1.sinaimg.cn\/large\/8fa5dcfcgy1fz79o78w1ej20d007k3yp.jpg\" style=\"width: 468px;height: 272px\" \/>\n<\/p>\n<p>\n\t\u8f93\u5165\u6b63\u786e\u7528\u6237\u540d\u5bc6\u7801\uff08user\/123\uff09\u540e\u663e\u793a\u6388\u6743\u9875\uff1a\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"OAuth \u9ed8\u8ba4\u7684\u6388\u6743\u9875\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/wx1.sinaimg.cn\/large\/8fa5dcfcgy1fz79pyvtjcj20e2064gly.jpg\" style=\"width: 506px;height: 220px\" \/>\n<\/p>\n<p>\n\t\u4e0a\u9762\u8fd9\u4e24\u4e2a\u8fc7\u7a0b\uff0c\u6211\u4eec\u90fd\u53ef\u4ee5\u8fdb\u884c\u5b9a\u5236\u3002URL \u4e2d\u7684 code \u53c2\u6570\u503c\u5373\u4e3a\u6388\u6743\u7801\uff0c\u8be5\u503c\u662f\u4e00\u4e2a 6 \u4f4d\u6570\u7684\u968f\u673a\u6570\u3002\u7531 RandomValueStringGenerator.generate() \u751f\u6210\u3002\n<\/p>\n<p>\n\t\u4e00\u822c\u7684\uff0c\u6388\u6743\u7801\u90fd\u6709\u4e00\u4e2a\u8fc7\u671f\u65f6\u95f4\uff0c\u8fd9\u91cc\u6211\u4eec\u540e\u9762\u518d\u8bf4\u3002\u4ee4\u724c access_token \u662f\u4e00\u4e2a UUID\uff0c\u9ed8\u8ba4\u5b9e\u73b0\u662f DefaultTokenServices.createAccessToken() \u3002\n<\/p>\n<p>\n\t\u5237\u65b0\u4ee4\u724c\uff0c\u4f7f\u7528\u7684\u662f \/oauth\/token?grant_type=refresh_token&amp;refresh_token=5b319fed-5600-4ea2-8c4f-61f6e3ea6e41 \u8fd9\u4e2a URL\uff0crefresh_token \u662f\u4e00\u4e2a\u65e7\u7684\u4ee4\u724c\u3002\u8c03\u7528\u8fd9\u4e2a URL \u540e\uff0c\u4e45\u7684\u4ee4\u724c\u4f1a\u5931\u6548\uff0c\u53e6\u5916\u4f1a\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u4ee4\u724c\u3002\n<\/p>\n<h2>\n\t\u53c2\u8003\u8d44\u6599<br \/>\n<\/h2>\n<ul>\n<li>\n\t\t<a href=\"http:\/\/projects.spring.io\/spring-security-oauth\/docs\/oauth2.html\">http:\/\/projects.spring.io\/spring-security-oauth\/docs\/oauth2.html<\/a>&nbsp;\n\t<\/li>\n<li>\n\t\t<a href=\"https:\/\/segmentfault.com\/a\/1190000010540911%C2%A0\">https:\/\/segmentfault.com\/a\/1190000010540911&nbsp;<\/a>\n\t<\/li>\n<li>\n\t\t<a href=\"http:\/\/ifeve.com\/oauth2-tutorial-all\/\">http:\/\/ifeve.com\/oauth2-tutorial-all\/<\/a>&nbsp;\n\t<\/li>\n<li>\n\t\t<a href=\"http:\/\/websystique.com\/spring-security\/secure-spring-rest-api-using-oauth2\/\">http:\/\/websystique.com\/spring-security\/secure-spring-rest-api-using-oauth2\/<\/a>\n\t<\/li>\n<li>\n\t\t<a href=\"http:\/\/qiita.com\/TakahikoKawasaki\/items\/200951e5b5929f840a1f\">http:\/\/qiita.com\/TakahikoKawasaki\/items\/200951e5b5929f840a1f<\/a>\n\t<\/li>\n<li>\n\t\t<a href=\"https:\/\/murashun.jp\/blog\/20150920-01.html\">https:\/\/murashun.jp\/blog\/20150920-01.html<\/a>\n\t<\/li>\n<li>\n\t\t<a href=\"https:\/\/www.xttblog.com\/?p=3730\">\u6781\u5ba2\u65f6\u95f4\u5168\u5957\u89c6\u9891\u8d2d\u4e70\u4f18\u60e0\u6307\u5357\uff0c\u6781\u5ba2\u65f6\u95f4\u600e\u4e48\u6837\uff01<\/a>\n\t<\/li>\n<\/ul>\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=3732\">SpringBoot + Spring Security OAuth2 \u7684\u7b80\u5355\u5b9e\u73b0<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u968f\u7740 OAuth2\uff08\u5f00\u653e\u6388\u6743\uff09\u7684\u706b\u70ed\uff0c\u8d8a\u6765\u8d8a\u591a\u7684\u7f51\u7ad9\u9009\u62e9\u4f7f\u7528 OAuth2 \u6280\u672f\u83b7\u5f97\u7528\u6237\u3002\u8fd9\u6837\u505a\u4e0d\u9700\u8981\u7528\u6237\u8fc7\u591a [&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,1522],"class_list":["post-3732","post","type-post","status-publish","format-standard","hentry","category-java","category-yyzt","tag-oauth2","tag-securitymanager","tag-security_err","tag-sessionssecuritymanager","tag-springboot"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3732","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=3732"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3732\/revisions"}],"predecessor-version":[{"id":3733,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3732\/revisions\/3733"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3732"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3732"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3732"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}