Spring Security

学习思路:了解什么是Spring Security ~~> 看官网简介 ~~>简单快速阅读官方文档(看自己想要的)

 

spring boot:

  • 导入maven依赖
  • 配置相关文件
  • 测试代码编写

市面上较为知名的安全框架:

  • Shiro (用的十分多,功能十分强大)
  • Spring Security (用的也很多,可以与spring无缝结合,十分方便)

Spring Security官方文档地址:https://docs.spring.io/spring-security/site/docs/5.3.0.RELEASE/reference/html5/#ns-method-security

 

原理:

 

 

配置:

 1 package com.coding.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 import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
 9 
10 @Configuration
11 @EnableWebSecurity
12 public class SecurityConfig extends WebSecurityConfigurerAdapter {
13     @Override
14     protected void configure(HttpSecurity http) throws Exception {
15 
16         // 不同用户访问内容不同! 这里,过滤器,登录注销规则,安全配置,OAuth2配置
17         // 我们平时只需要配置一些基本的规则即可!
18 
19         // 首页是允许所有人访问的!
20         // 定制授权规则: 那些请求,哪些人可以访问!
21         http.authorizeRequests()
22                 .antMatchers("/").permitAll() //全部人可访问首页
23                 .antMatchers("/level1/**").hasRole("vip1") // 访问/level1/**下的需要vip1权限
24                 .antMatchers("/level2/**").hasRole("vip2")
25                 .antMatchers("/level3/**").hasRole("vip3");
26 
27         // 登录  /login跳转到登录页  /login?error 登录失败
28         http.formLogin()
29                 .usernameParameter("username")
30                 .passwordParameter("password")
31                 .loginPage("/toLogin")
32                 .loginProcessingUrl("/login");// 登陆表单提交请求!
33 
34         // 注销,开启默认的注销功能
35         http.logout().logoutSuccessUrl("/"); // 用户登出跳转首页
36 
37         // 自定义的登录页需要配置 rememberMe 的参数名,就可以绑定到我们前端的!
38         // 记住我功能
39         http.rememberMe().rememberMeParameter("remember");
40     }
41 
42     // 定义用户认证的规则(密码,权限...)
43     @Override
44     protected void configure(AuthenticationManagerBuilder auth) throws Exception {
45         auth.inMemoryAuthentication()
46                 .passwordEncoder(new BCryptPasswordEncoder()) // 加密使用的方法
47                 .withUser("coding") // 用户名
48                 .password(new BCryptPasswordEncoder().encode("123456"))
49                 .roles("vip1","vip2") // 用户权限
50                 .and() // 多个用户配置用此方法连接
51                 .withUser("heng")
52                 .password(new BCryptPasswordEncoder().encode("123456"))
53                 .roles("vip1","vip2","vip3")
54                 .and()
55                 .withUser("zhangsan")
56                 .password(new BCryptPasswordEncoder().encode("123456"))
57                 .roles("vip1");
58 
59     }
60 }

 

前端略...

 

posted @ 2020-03-28 15:11  执笔人生  阅读(325)  评论(0编辑  收藏  举报