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();
//防止网站攻击工具:get易受攻击且是csrf攻击,改为post
//新版本不用关闭
//http.csrf().disable(); //springboot默认开启csrf是正确的,会防止跨站请求攻击,
// 低版本的springboot需要关闭csrf,登陆失败存在的原因 或者设置为post方式注销
//开启注销功能,注销跳到首页
http.logout().logoutSuccessUrl("/");
}
}
退出失败问题:
- get请求导致,设置为post方式注销
- springboot默认开启csrf导致
- springboot版本问题,新版本没问题
org.thymeleaf.extras
thymeleaf-extras-springsecurity5
授权是给用户角色来授权
sec:authorize=“”授权,通过这个用户是否有这个角色,来判断是否通过授权
sec:authentication=“name” 认证信息
首页
