应用程序的两个主要区域:认证和受权(这两个主要区域是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.控制请求的访问权限