﻿{"id":696,"date":"2020-08-18T18:40:22","date_gmt":"2020-08-18T10:40:22","guid":{"rendered":"http:\/\/blog.sway.com.cn\/?p=696"},"modified":"2020-08-18T20:50:47","modified_gmt":"2020-08-18T12:50:47","slug":"springboot2-1-1%e5%be%ae%e6%9c%8d%e5%8a%a1%e6%9e%b6%e6%9e%84%e5%bc%95%e5%85%a5springcloudsecurity%e5%ae%89%e5%85%a8%e8%ae%a4%e8%af%81","status":"publish","type":"post","link":"http:\/\/blog.sway.com.cn\/?p=696","title":{"rendered":"SpringBoot2.1.1\u5fae\u670d\u52a1\u67b6\u6784\u5f15\u5165SpringCloudSecurity\u5b89\u5168\u8ba4\u8bc1"},"content":{"rendered":"<p>1\u3001\u5728eureka\u670d\u52a1\u5668\u7684pom.xml\u4e2d\u5f15\u5165\u4f9d\u8d56\uff1a<\/p>\n<pre class=\"lang:default decode:true \">\t&lt;!-- Spring Cloud Security \u4f9d\u8d56 --&gt;\r\n\t&lt;dependency&gt;\r\n            &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\r\n            &lt;artifactId&gt;spring-boot-starter-security&lt;\/artifactId&gt;\r\n        &lt;\/dependency&gt;<\/pre>\n<p>2\u3001\u521b\u5efa\u5bc6\u7801\u5b89\u5168\u8ba4\u8bc1\u5bc6\u7801\u5339\u914d\u89c4\u5219\u7c7bMyPasswordEncoder.java\uff1a<\/p>\n<pre class=\"lang:default decode:true \">package com.zero4j.config;\r\n\r\nimport org.springframework.security.crypto.password.PasswordEncoder;\r\n\r\npublic class MyPasswordEncoder implements PasswordEncoder {\r\n\r\n\t@Override\r\n    public String encode(CharSequence charSequence) {\r\n        return charSequence.toString();\r\n    }\r\n\r\n    @Override\r\n    public boolean matches(CharSequence charSequence, String s) {\r\n        return s.equals(charSequence.toString());\r\n    }\r\n    \r\n}\r\n<\/pre>\n<p>3\u3001\u5728eureka\u670d\u52a1\u5668\u4e2d\u521b\u5efa\u914d\u7f6e\u7c7bSecurityConfig.java\uff1a<\/p>\n<pre class=\"lang:default decode:true \">package com.zero4j.config;\r\n\r\nimport org.springframework.beans.factory.annotation.Autowired;\r\nimport org.springframework.context.annotation.Bean;\r\nimport org.springframework.context.annotation.Configuration;\r\nimport org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;\r\nimport org.springframework.security.crypto.factory.PasswordEncoderFactories;\r\nimport org.springframework.security.crypto.password.PasswordEncoder;\r\nimport org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;\r\nimport org.springframework.security.config.annotation.web.builders.HttpSecurity;\r\nimport org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;\r\nimport org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;\r\n\r\n@Configuration\r\n@EnableWebSecurity\r\npublic class SecurityConfig extends WebSecurityConfigurerAdapter {\r\n    \r\n\t@Bean\r\n    public PasswordEncoder passwordEncoder() {\r\n        return PasswordEncoderFactories.createDelegatingPasswordEncoder();\r\n    }\r\n    \r\n    \/\/@Autowired\r\n    \/\/BCryptPasswordEncoder passwordEncoder;\r\n\r\n    @Override\r\n    protected void configure(AuthenticationManagerBuilder auth) throws Exception {\r\n    \t\r\n    \t\/\/\u4ee5\u524d\u53ef\u4ee5\u4e0d\u6307\u5b9aPasswordEncoder,\u4f46\u662f\u65b0\u7684SpringBoot\u4f9d\u8d56\u7684SpringCloudScurity\u9700\u8981\u4e86\r\n    \t\/\/auth.inMemoryAuthentication().withUser(\"admin\").password(\"123456\").roles(\"ADMIN\");\r\n    \t\/\/\u8fd9\u6837\uff0c\u5bc6\u7801\u4ee5\u660e\u6587\u7684\u65b9\u5f0f\u8fdb\u884c\u5339\u914d\r\n    \tauth.inMemoryAuthentication().passwordEncoder(new MyPasswordEncoder()).withUser(\"admin\").password(\"123456\").roles(\"ADMIN\");\r\n    \t\/\/auth.inMemoryAuthentication().withUser(\"admin\").password(passwordEncoder.encode(\"123456\")).roles(\"ADMIN\");\r\n    }\r\n\r\n    @Override\r\n    protected void configure(HttpSecurity http) throws Exception {\r\n        http.csrf().ignoringAntMatchers(\"\/eureka\/**\");\r\n        super.configure(http);\r\n    }\r\n}<\/pre>\n<p>4\u3001\u542f\u52a8\u670d\u52a1\uff0c\u8bbf\u95eeeureka\u670d\u52a1\u4e2d\u5fc3\uff0c\u4f7f\u7528admin:123456\u8fdb\u884c\u767b\u5f55\uff0c\u6210\u529f\u8fdb\u5165eureka\u63a7\u5236\u53f0<\/p>\n<p>5\u3001\u5bf9\u5fae\u670d\u52a1\u63d0\u4f9b\u8005\u7684application.propertites\u8bf4\u5f15\u7528\u7684eureka\u670d\u52a1\u4e2d\u5fc3\u5730\u5740\u7684\u524d\u9762\u52a0\u5165\u201cadmin:123456@\u201d\uff0c\u5982\uff1ahttp\\:\/\/admin:123456@localhost\\:8761\/eureka\/ \uff0c\u7136\u540e\u542f\u52a8\u5373\u53ef~<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u5728eureka\u670d\u52a1\u5668\u7684pom.xml\u4e2d\u5f15\u5165\u4f9d\u8d56\uff1a &lt;!&#8211; Spring Cloud Securit [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[167],"tags":[210,168,178],"class_list":["post-696","post","type-post","status-publish","format-standard","hentry","category-springboot","tag-security","tag-springboot","tag-springcloud"],"_links":{"self":[{"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/696","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=696"}],"version-history":[{"count":2,"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/696\/revisions"}],"predecessor-version":[{"id":698,"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/696\/revisions\/698"}],"wp:attachment":[{"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=696"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=696"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=696"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}