# kuangshen-SpringSecurity **Repository Path**: dotJunz/kuangshen-spring-security ## Basic Information - **Project Name**: kuangshen-SpringSecurity - **Description**: 狂神讲的SpringSecurity - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 2 - **Forks**: 0 - **Created**: 2022-09-14 - **Last Updated**: 2024-04-21 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## SpringSecurity环境搭建 ### 实现界面的路由跳转 ```java @Controller public class RouterController { @RequestMapping({"/","index"}) public String index(){ return "index"; } @RequestMapping("toLogin") public String toLogin(){ return "views/login"; } @RequestMapping("/level1/{id}") public String level1(@PathVariable("id") int id){ return "views/level1/"+id; } @RequestMapping("/level2/{id}") public String level2(@PathVariable("id") int id){ return "views/level2/"+id; } @RequestMapping("/level3/{id}") public String level3(@PathVariable("id") int id){ return "views/level3/"+id; } } ``` ### 效果 首页 ![image-20220914145030395](README.assets/image-20220914145030395.png) 点击Level-1 ![image-20220914145108153](README.assets/image-20220914145108153.png) 点击Level-2 ![image-20220914145149629](README.assets/image-20220914145149629.png) ## 用户认证和授权 ### 授权 设置vip1角色可以访问level1,vip2角色可以访问level2,vip3角色可以访问level3 ### 认证 设置`kuangshen`拥有vip2、vip3的权限 设置`root`拥有vip1、vip2、vip3的权限 设置`guest`拥有vip1的权限 ```java // AOP: 拦截器 @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { // 授权 @Override protected void configure(HttpSecurity http) throws Exception { // 首页所有人可以访问,功能页只有对应有权限的人才能访问 // 请求授权的规则 http.authorizeHttpRequests() .antMatchers("/").permitAll() .antMatchers("/level1/**").hasRole("vip1") .antMatchers("/level2/**").hasRole("vip2") .antMatchers("/level3/**").hasRole("vip3"); // 没有权限默认会到登录界面,需要开启登录的界面 http.formLogin(); } // 认证 @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { //这些数据正常应该从数据库中读 auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()) .withUser("kuangshen").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2","vip3") .and() .withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2","vip3") .and() .withUser("guest").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1"); } } ``` ### 效果 首页都可以访问,再次点击level,跳出登录界面 ![image-20220914145637349](README.assets/image-20220914145637349.png) 登录不同的用户,实现之前设置的不同访问权限 ## 注销及权限控制 ### 注销 点击注销 ![image-20220914153952418](README.assets/image-20220914153952418.png) 回到首页 ![image-20220914154017959](README.assets/image-20220914154017959.png) ### 权限控制 登录不同权限的用户,看到的界面不一样。 登录root用户 ![image-20220914154100329](README.assets/image-20220914154100329.png) ![image-20220914154152817](README.assets/image-20220914154152817.png) 登录kuangshen用户 ![image-20220914154229848](README.assets/image-20220914154229848.png) 登录guest用户 ![image-20220914154259444](README.assets/image-20220914154259444.png) ## 记住我及首页定制 使用定制首页代替默认的 ![image-20220914160842229](README.assets/image-20220914160842229.png) 点击记住我登录,会在cookie中保存remember-me的信息,保存14天。 ![image-20220914160904822](README.assets/image-20220914160904822.png)