<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ## Spring Security Guides 這一節我們通過Spring官方給出的一個guides例子,來了解Spring Security是如何保護我們的應用的,之后會對進行一個解讀。 ### 2.1 引入依賴 ~~~ <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies> ~~~ 由于我們集成了springboot,所以不需要顯示的引入Spring Security文檔中描述core,config依賴,只需要引入spring-boot-starter-security即可。 ### 2.2 創建一個不受安全限制的web應用 這是一個首頁,不受安全限制 `src/main/resources/templates/home.html` ~~~ <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"> <head> <title>Spring Security Example</title> </head> <body> <h1>Welcome!</h1> <p>Click <a th:href="@{/hello}">here</a> to see a greeting.</p> </body> </html> ~~~ 這個簡單的頁面上包含了一個鏈接,跳轉到”/hello”。對應如下的頁面 `src/main/resources/templates/hello.html` ~~~ <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"> <head> <title>Hello World!</title> </head> <body> <h1>Hello world!</h1> </body> </html> ~~~ 接下來配置Spring MVC,使得我們能夠訪問到頁面。 ~~~ @Configuration public class MvcConfig extends WebMvcConfigurerAdapter { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/home").setViewName("home"); registry.addViewController("/").setViewName("home"); registry.addViewController("/hello").setViewName("hello"); registry.addViewController("/login").setViewName("login"); } } ~~~ ### 2.3 配置Spring Security 一個典型的安全配置如下所示: ~~~ @Configuration @EnableWebSecurity <1> public class WebSecurityConfig extends WebSecurityConfigurerAdapter { <1> @Override protected void configure(HttpSecurity http) throws Exception { http <2> .authorizeRequests() .antMatchers("/", "/home").permitAll() .anyRequest().authenticated() .and() .formLogin() .loginPage("/login") .permitAll() .and() .logout() .permitAll(); } @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth <3> .inMemoryAuthentication() .withUser("admin").password("admin").roles("USER"); } } ~~~ <1> @EnableWebSecurity注解使得SpringMVC集成了Spring Security的web安全支持。另外,WebSecurityConfig配置類同時集成了WebSecurityConfigurerAdapter,重寫了其中的特定方法,用于自定義Spring Security配置。整個Spring Security的工作量,其實都是集中在該配置類,不僅僅是這個guides,實際項目中也是如此。 <2> configure(HttpSecurity)定義了哪些URL路徑應該被攔截,如字面意思所描述:”/“, “/home”允許所有人訪問,”/login”作為登錄入口,也被允許訪問,而剩下的”/hello”則需要登陸后才可以訪問。 <3> configureGlobal(AuthenticationManagerBuilder)在內存中配置一個用戶,admin/admin分別是用戶名和密碼,這個用戶擁有USER角色。 我們目前還沒有登錄頁面,下面創建登錄頁面: ~~~ <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"> <head> <title>Spring Security Example </title> </head> <body> <div th:if="${param.error}"> Invalid username and password. </div> <div th:if="${param.logout}"> You have been logged out. </div> <form th:action="@{/login}" method="post"> <div><label> User Name : <input type="text" name="username"/> </label></div> <div><label> Password: <input type="password" name="password"/> </label></div> <div><input type="submit" value="Sign In"/></div> </form> </body> </html> ~~~ 這個Thymeleaf模板提供了一個用于提交用戶名和密碼的表單,其中name=”username”,name=”password”是默認的表單值,并發送到“/ login”。 在默認配置中,Spring Security提供了一個攔截該請求并驗證用戶的過濾器。 如果驗證失敗,該頁面將重定向到“/ login?error”,并顯示相應的錯誤消息。 當用戶選擇注銷,請求會被發送到“/ login?logout”。 最后,我們為hello.html添加一些內容,用于展示用戶信息。 ~~~ <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"> <head> <title>Hello World!</title> </head> <body> <h1 th:inline="text">Hello [[${#httpServletRequest.remoteUser}]]!</h1> <form th:action="@{/logout}" method="post"> <input type="submit" value="Sign Out"/> </form> </body> </html> ~~~ 我們使用Spring Security之后,HttpServletRequest#getRemoteUser()可以用來獲取用戶名。 登出請求將被發送到“/ logout”。 成功注銷后,會將用戶重定向到“/ login?logout”。 ### 2.4 添加啟動類 ~~~ @SpringBootApplication public class Application { public static void main(String[] args) throws Throwable { SpringApplication.run(Application.class, args); } } ~~~ ### 2.5 測試 略。 ### 2.6 總結 本篇文章沒有什么干貨,基本算是翻譯了Spring Security Guides的內容,稍微了解Spring Security的朋友都不會對這個翻譯感到陌生。考慮到受眾的問題,一個入門的例子是必須得有的,方便后續對Spring Security的自定義配置進行講解。下一節,以此guides為例,講解這些最簡化的配置背后,Spring Security都幫我們做了什么工作。 本節所有的代碼,可以直接在Spring的官方倉庫下載得到,git clone https://github.com/spring-guides/gs-securing-web.git 。不過,建議初學者根據文章先一步步配置,出了問題,再與demo進行對比。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看