功能权限(管理员、普通用户)
访问权限(页面访问)
菜单权限
- 若使用拦截器,过滤器也可以实现身份验证和授权功能但会产生大量的原生代码~冗余
在web开发中,安全第一位!过滤器,拦截器做的好也可以不用springSecurity
网站安全非功能性需求
做网站:安全应该在什么时候考虑?设计之初!
主流的安全框架shiro/SpringSecurity:二者很像~除了类不一样,名字不一样;做认证,授权(vip1,vip2,vip3)
将拦截器、过滤器一路简化:MVC—SPRING—SPRINGBOOT—框架思想
导依赖thymeleaf启动器
org.springframework.boot
spring-boot-starter-security
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-web
org.thymeleaf.extras
thymeleaf-extras-springsecurity5
org.springframework.boot
spring-boot-devtools
runtime
true
org.thymeleaf
thymeleaf-spring5
3.0.15.RELEASE
compile
org.thymeleaf.extras
thymeleaf-extras-java8time
3.0.4.RELEASE
compile
WebSecurityConfigurerAdapter:自定义Security策略
AuthenticationManagerBuilder:自定义认证策略
@EnableWebSecurity:开启WebSecurity模式 、后续的springcloud中@Enablexxx开启某个功能
Spring Security的两个主要目标是“认证”和“授权”(访问控制)。
这个概念是通用的,而不是只在Spring Security中存在。
官网:https://spring.io/projects/spring-security/查看项目中的版本,找到对于的帮助文档。
用例结构,静态资源或者是SpringBoot-27-springSecurity(安全:认证授权)demo的静态资源进行静态资源下载

spring.thymeleaf.cache=false
package com.zk.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author CNCLUKZK
* @create 2022/8/3-22:32
*/
@Controller
public class RouterController {
@RequestMapping({"/","/index","/index.html"})
public String toIndex(){
return "index";
}
@RequestMapping("/toLogin")
public String toLogin(){
return "views/login";
}
@RequestMapping("/level1/{id}")
public String level1(@PathVariable("id") String id){
return "views/level1/"+id;
}
@RequestMapping("/level2/{id}")
public String level2(@PathVariable("id") String id){
return "views/level2/"+id;
}
@RequestMapping("/level3/{id}")
public String level3(@PathVariable("id") String id){
return "views/level3/"+id;
}
}
package com.zk.config;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
/**
* @author CNCLUKZK
* @create 2022/8/3-23:07
*/
//AOP思想,非拦截器
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
//链式编程-- 授权规则,角色和请求路径
@Override
protected void configure(HttpSecurity http) throws Exception {
//首页所有人可以访问,功能页只有对应有权限的人才能访问
//请求授权的页面
http.authorizeRequests()
.antMatchers("/").permitAll() //首页所有人访问
.antMatchers("/level1/**").hasRole("vip1")
.antMatchers("/level2/**").hasRole("vip2")
.antMatchers("/level3/**").hasRole("vip3");
//没有权限默认到登陆页,开启登陆页
http.formLogin();
http.httpBasic();
}
//认证规则,springboot2.1.X可以直接使用,认证用户和角色
//密码编码:PasswordEncoder 缺少加密报错
//spring Secutiry5.0+新增了很多的加密方法
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//正常应该从数据库中读取
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("admin").password(new BCryptPasswordEncoder().encode("111111")).roles("vip1","vip2","vip3")
.and().withUser("zs").password(new BCryptPasswordEncoder().encode("111111")).roles("vip1")
.and().withUser("ls").password(new BCryptPasswordEncoder().encode("111111")).roles("vip2")
.and().withUser("ww").password(new BCryptPasswordEncoder().encode("111111")).roles("vip3");
}
}

注意高版本springSecutiry5在登陆时需要对内存中密码进行加密,可以用springSecutiry提供的加密规则
JDBC Authentication链接数据库进行授权认证
@Autowired
private DataSource dataSource;
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth)throws Exception
/ensure the passwords are encoded properly
UserBuilder users=User.withDefaultPasswordEncoder();
auth.jdbcAuthentication().dataSource(dataSource).withDefaultSchema()
.withUser(users.username("user").password("password").roles("USER"))
.withUser(users.username("admin").password("password").roles("USER","ADMIN"));
}