應用程序的兩個主要區域:認證和受權(這兩個主要區域是Spring Security的兩個目標)web
認證(Authentication):spring
受權(Authorization):瀏覽器
爲了抵達須要受權的點,主體身份已經有認證過程的創建安全
Spring Security中的類:session
@EnableWebSecurity: 開啓WebSecurity模式框架
1.引入spring-boot-starter-security依賴 2.編寫SpringSecurity配置類 2.1 定製請求的受權規則 2.2 開啓自動配置的登陸功能(/login來到登陸頁;重慶向到/login?error表示登陸失敗) 2.3 開啓自動配置的註銷功能(訪問/logout請求,表示用戶註銷並清空session;註銷成功返回/login?logout) 2.4 開啓自動配置的記住密碼功能(http.rememberMe();)-登陸成功之後,將Cookie發送給瀏覽器保存,能夠實現記住密碼功能;點擊註銷會刪除Cookie,就沒有記住密碼功能 默認post形式的/login表明處理登陸 2.5定義認證規則 @EnableSecurity public class MySecurityConfig extends WebSecurityConfigureAdapter{ @Override protected void configure(HttpSecurity http) throws Exception{ // 定製請求的受權規則 http.authorizeRequest().antMatches("/").permitAll() .antMatches("/level/**").hasRole("VIP"); // 開啓自動配置的登陸功能,若是沒有權限就會跳轉到登陸頁面 http.formLogin().loginPage("/"); // 跳轉到自定義登陸頁 http.logout().logoutSuccessUrl("/"); // 註銷成功返回首頁 http.rememberMe().rememberMeParameter("remember"); // 開啓自動配置的記住密碼功能 } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception{ auth.inMemoryAuthentication().withUser("username").password("password").roles("role1","role2") .and() .withUser("username").password("password").roles("role1","role2") } 3.控制請求的訪問權限