Spring Framework在Spring 3.1中添加了Java配置支持。在Spring Security中,Java配置已添加到Spring Security 3.2中,使我們可以配置Spring Security 而無需編寫XML單行。
在這里,我們將創(chuàng)建一個實現(xiàn)Spring的示例。安全性且未使用XML進行配置。它包括以下步驟。
第一步是創(chuàng)建Spring Security Java配置。下面給出了一個簡單的基本Java配置。
WebSecurityConfig.java
package com.nhooo; import org.springframework.context.annotation.*; //import org.springframework.security.config.annotation.authentication.builders.*; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.*; import org.springframework.security.core.userdetails.User; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.provisioning.InMemoryUserDetailsManager; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @EnableWebSecurity @ComponentScan("com.nhooo") public class WebSecurityConfig implements WebMvcConfigurer { @Bean public UserDetailsService userDetailsService() throws Exception { InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager(); manager.createUser(User.withDefaultPasswordEncoder().username("Nhooo"). password("java123").roles("USER").build()); return manager; } protected void configure(HttpSecurity http) throws Exception { http .antMatcher("/") .authorizeRequests() .anyRequest().hasRole("ADMIN") .and() .httpBasic(); } }
此配置創(chuàng)建一個稱為 springSecurityFilterChain 的Servlet過濾器。 負責保護應(yīng)用程序URL,驗證提交的用戶名和密碼,重定向到登錄表單等。
上述Java配置為我們的應(yīng)用程序執(zhí)行以下操作。
要求對每個URL進行身份驗證 創(chuàng)建登錄表單 允許用戶使用基于表單的身份驗證進行身份驗證 允許注銷 防止CSRF攻擊 安全標題集成等
現(xiàn)在,我們將向戰(zhàn)爭注冊 springSecurityFilterChain 。要進行注冊,Spring Security提供了我們需要擴展的基類AbstractSecurityWebApplicationInitializer。
對于Spring MVC應(yīng)用程序,SecurityWebApplicationInitializer如下所示。
SecurityWebApplicationInitializer.java
package com.nhooo; import org.springframework.security.web.context.*; public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer { }
此代碼將為我們應(yīng)用程序中的每個URL注冊springSecurityFilterChain。
現(xiàn)在,將WebSecurityConfig加載到我們現(xiàn)有的ApplicationInitializer中并添加到getRootConfigClasses()方法。
MvcWebApplicationInitializer.java
package com.nhooo; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; public class MvcWebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<?>[] getRootConfigClasses() { return new Class[] { WebSecurityConfig.class }; } @Override protected Class<?>[] getServletConfigClasses() { // TOdo Auto-generated method stub return null; } @Override protected String[] getServletMappings() { return new String[] { "/" }; } }
WebSecurityConfigurerAdapter 類提供了一個configure(HttpSecurity http)方法,該方法包含以下默認配置。默認定義如下所示。
protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .anyRequest().authenticated() .and() .formLogin() .and() .httpBasic(); }
它類似于給定的XML。
<http> <intercept-url pattern="/**" access="authenticated"/> <form-login /> <http-basic /> </http>
此方法執(zhí)行以下操作。
它確保用戶提出的每個請求都要求對用戶進行身份驗證 它允許用戶使用基于表單的登錄進行身份驗證 它允許用戶使用HTTP Basic身份驗證進行身份驗證
創(chuàng)建一個控制器來處理用戶請求。
HomeController.java
package com.nhooo.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class HomeController { @RequestMapping(value="/", method=RequestMethod.GET) public String index() { return "index"; } }
我們有一個視圖(.jsp)頁面 index.jsp ,其中包含以下源代碼。
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Home Page</title> </head> <body> Welcome to home page! </body> </html>
我們的完整項目如下所示。
輸出:
我們在控制器中只有一個動作,只有真正的用戶才能訪問它。因此,當我們運行該應(yīng)用程序時,它會提示您輸入登錄憑據(jù)。輸出在下面給出。
這是 Spring Security提供的默認登錄頁面頁面,我們沒有創(chuàng)建它。盡管我們可以創(chuàng)建自己的登錄頁面并使用應(yīng)用程序進行配置。我們將在下一個主題中進行此操作。
現(xiàn)在,提供登錄憑據(jù)以進入應(yīng)用程序資源。 Spring Security驗證用戶憑證并確保用戶真實性。
讓我們看看會發(fā)生什么?如果我們輸入了錯誤的憑據(jù)。
單擊登錄按鈕后,則會引發(fā) Bad Credentials (錯誤憑據(jù))錯誤。
現(xiàn)在,使用 正確的憑據(jù)登錄。/strong>
這次憑據(jù)被匹配并顯示了主頁(index.jsp)。