{"id":8390,"date":"2023-11-28T20:26:58","date_gmt":"2023-11-28T12:26:58","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=8390"},"modified":"2023-12-06T10:27:29","modified_gmt":"2023-12-06T02:27:29","slug":"springboot-ying-yong-an-quan-pei-zhi-you-o-na-xie","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/springboot-ying-yong-an-quan-pei-zhi-you-o-na-xie\/","title":{"rendered":"SpringBoot\u5e94\u7528\u5b89\u5168\u914d\u7f6e\u6709\u54e6\u54ea\u4e9b\uff1f"},"content":{"rendered":"<p>Spring Boot \u5e94\u7528\u7684\u5b89\u5168\u914d\u7f6e\u53ef\u4ee5\u901a\u8fc7 Spring Security \u6765\u5b9e\u73b0\u3002Spring Security\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u4e14\u7075\u6d3b\u7684\u5b89\u5168\u6846\u67b6\uff0c\u7528\u4e8e\u5728Java\u5e94\u7528\u4e2d\u5904\u7406\u8ba4\u8bc1\u548c\u6388\u6743\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684 Spring Boot \u5e94\u7528\u5b89\u5168\u914d\u7f6e\u7684\u4e3b\u9898\uff1a<\/p>\n<ol>\n<li>\n<strong>\u57fa\u672c\u7684\u8eab\u4efd\u9a8c\u8bc1\uff1a<\/strong><\/p>\n<ul>\n<li>\u542f\u7528\u57fa\u672c\u7684HTTP\u8eab\u4efd\u9a8c\u8bc1\uff0c\u8981\u6c42\u7528\u6237\u63d0\u4f9b\u7528\u6237\u540d\u548c\u5bc6\u7801\u3002\u53ef\u4ee5\u4f7f\u7528 Spring Security \u63d0\u4f9b\u7684 <code>configure(HttpSecurity http)<\/code> \u65b9\u6cd5\u6765\u914d\u7f6e\u57fa\u672c\u7684\u8eab\u4efd\u9a8c\u8bc1\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">@Configuration\n@EnableWebSecurity\npublic class SecurityConfig extends WebSecurityConfigurerAdapter {\n\n    @Override\n    protected void configure(HttpSecurity http) throws Exception {\n        http\n            .authorizeRequests()\n                .anyRequest().authenticated()\n                .and()\n            .httpBasic();\n    }\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u8868\u5355\u767b\u5f55\uff1a<\/strong><\/p>\n<ul>\n<li>\u914d\u7f6e\u4f7f\u7528\u8868\u5355\u767b\u5f55\uff0c\u7528\u6237\u901a\u8fc7\u8f93\u5165\u7528\u6237\u540d\u548c\u5bc6\u7801\u767b\u5f55\u3002\u8fd9\u9700\u8981\u914d\u7f6e <code>configure(HttpSecurity http)<\/code> \u65b9\u6cd5\uff0c\u5e76\u53ef\u80fd\u6d89\u53ca\u5230\u7528\u6237\u5b58\u50a8\u548c\u8eab\u4efd\u9a8c\u8bc1\u670d\u52a1\u7684\u914d\u7f6e\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">@Configuration\n@EnableWebSecurity\npublic class SecurityConfig extends WebSecurityConfigurerAdapter {\n\n    @Override\n    protected void configure(HttpSecurity http) throws Exception {\n        http\n            .authorizeRequests()\n                .anyRequest().authenticated()\n                .and()\n            .formLogin();\n    }\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u8ba4\u8bc1\u548c\u6388\u6743\uff1a<\/strong><\/p>\n<ul>\n<li>\u914d\u7f6e\u8ba4\u8bc1\u548c\u6388\u6743\u89c4\u5219\uff0c\u5305\u62ec\u8bbe\u7f6e\u7279\u5b9a\u8def\u5f84\u7684\u8bbf\u95ee\u6743\u9650\uff0c\u5b9a\u4e49\u7528\u6237\u89d2\u8272\uff0c\u914d\u7f6e\u81ea\u5b9a\u4e49\u7684\u7528\u6237\u5b58\u50a8\u7b49\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">@Configuration\n@EnableWebSecurity\npublic class SecurityConfig extends WebSecurityConfigurerAdapter {\n\n    @Override\n    protected void configure(HttpSecurity http) throws Exception {\n        http\n            .authorizeRequests()\n                .antMatchers(&quot;\/public\/**&quot;).permitAll()\n                .antMatchers(&quot;\/admin\/**&quot;).hasRole(&quot;ADMIN&quot;)\n                .anyRequest().authenticated()\n                .and()\n            .formLogin();\n    }\n\n    @Override\n    protected void configure(AuthenticationManagerBuilder auth) throws Exception {\n        auth\n            .inMemoryAuthentication()\n                .withUser(&quot;user&quot;).password(&quot;{noop}password&quot;).roles(&quot;USER&quot;)\n                .and()\n                .withUser(&quot;admin&quot;).password(&quot;{noop}password&quot;).roles(&quot;USER&quot;, &quot;ADMIN&quot;);\n    }\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u4f7f\u7528 HTTPS\uff1a<\/strong><\/p>\n<ul>\n<li>\u914d\u7f6e\u5e94\u7528\u7a0b\u5e8f\u4f7f\u7528HTTPS\uff0c\u786e\u4fdd\u6570\u636e\u5728\u4f20\u8f93\u8fc7\u7a0b\u4e2d\u7684\u5b89\u5168\u6027\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">@Configuration\n@EnableWebSecurity\npublic class SecurityConfig extends WebSecurityConfigurerAdapter {\n\n    @Override\n    protected void configure(HttpSecurity http) throws Exception {\n        http\n            .requiresChannel()\n                .anyRequest().requiresSecure()\n                .and()\n            .authorizeRequests()\n                .anyRequest().authenticated()\n                .and()\n            .formLogin();\n    }\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>CSRF\uff08Cross-Site Request Forgery\uff09\u9632\u62a4\uff1a<\/strong><\/p>\n<ul>\n<li>\u914d\u7f6eCSRF\u9632\u62a4\uff0c\u4ee5\u9632\u6b62\u8de8\u7ad9\u70b9\u8bf7\u6c42\u4f2a\u9020\u653b\u51fb\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">@Configuration\n@EnableWebSecurity\npublic class SecurityConfig extends WebSecurityConfigurerAdapter {\n\n    @Override\n    protected void configure(HttpSecurity http) throws Exception {\n        http\n            .csrf().disable() \/\/ \u7981\u7528 CSRF\n            .authorizeRequests()\n                .anyRequest().authenticated()\n                .and()\n            .formLogin();\n    }\n}\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>\u8fd9\u53ea\u662f\u5b89\u5168\u914d\u7f6e\u7684\u4e00\u4e9b\u57fa\u672c\u65b9\u6848\u3002\u6839\u636e\u5e94\u7528\u7a0b\u5e8f\u7684\u9700\u6c42\uff0c\u53ef\u80fd\u9700\u8981\u8fdb\u4e00\u6b65\u914d\u7f6e\u5176\u4ed6\u7279\u6027\uff0c\u4f8b\u5982\u4f7f\u7528 OAuth 2.0 \u8fdb\u884c\u8eab\u4efd\u9a8c\u8bc1\u3001\u96c6\u6210 LDAP \u6216\u4f7f\u7528\u6570\u636e\u5e93\u8fdb\u884c\u7528\u6237\u8ba4\u8bc1\u7b49\u3002Spring Security \u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u914d\u7f6e\u9009\u9879\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u5b9a\u5236\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Spring Boot \u5e94\u7528\u7684\u5b89\u5168\u914d\u7f6e\u53ef\u4ee5\u901a\u8fc7 Spring Security \u6765\u5b9e\u73b0\u3002Spring Secu [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[100],"tags":[],"class_list":["post-8390","post","type-post","status-publish","format-standard","hentry","category-springboot"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/8390","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=8390"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/8390\/revisions"}],"predecessor-version":[{"id":16607,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/8390\/revisions\/16607"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=8390"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=8390"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=8390"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}