标签:enc ota ted ebs login session pass 自动 定义
(1)、添加starter依赖
1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-security</artifactId> 4 </dependency>
(2)、使用配置类定义授权与定义规则
1 package cn.coreqi.config; 2 3 import org.springframework.context.annotation.Configuration; 4 import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 5 import org.springframework.security.config.annotation.web.builders.HttpSecurity; 6 import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; 7 import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 8 9 //@Configuration 10 @EnableWebSecurity 11 public class SecurityConfig extends WebSecurityConfigurerAdapter { 12 13 //定义授权规则 14 @Override 15 protected void configure(HttpSecurity http) throws Exception { 16 //定制请求授权规则 17 http.authorizeRequests().antMatchers("/").permitAll() 18 .antMatchers("/vip1/**").hasRole("VIP1") 19 .antMatchers("/vip2/**").hasRole("VIP2") 20 .antMatchers("/vip3/**").hasRole("VIP3"); 21 //开启登陆功能(自动配置) 22 //如果没有登陆就会来到/login(自动生成)登陆页面 23 //如果登陆失败就会重定向到/login?error 24 //默认post形式的/login代表处理登陆 25 http.formLogin().loginPage("/userLogin"); 26 //开启自动配置的注销功能 27 //访问/logout表示用户注销,清空session 28 //注销成功会返回/login?logout页面 29 //logoutSuccessUrl()设置注销成功后跳转的页面地址 30 http.logout().logoutSuccessUrl("/"); 31 //开启记住我功能 32 //登陆成功以后,将cookie发给浏览器保存,以后访问页面带上这个cookie,只要通过检查就可以免登陆 33 //点击注销会删除cookie 34 http.rememberMe(); 35 } 36 37 //定义认证规则 38 @Override 39 protected void configure(AuthenticationManagerBuilder auth) throws Exception { 40 //jdbcAuthentication() 在JDBC中查找用户 41 //inMemoryAuthentication() 在内存中查找用户 42 43 auth.inMemoryAuthentication().withUser("fanqi").password("admin").roles("VIP1","VIP2","VIP3") 44 .and() 45 .withUser("zhangsan").password("123456").roles("VIP1"); 46 } 47 }
标签:enc ota ted ebs login session pass 自动 定义
原文地址:https://www.cnblogs.com/fanqisoft/p/10357234.html