Spring Security两个主要目标:认证(Authentication)、授权(Authorization)
1.导入依赖
org.springframework.boot spring-boot-starter-security
2.配置一个Security的配置类
@EnableWebSecurity//开启security模式
public class SecurityConfig extends WebSecurityConfigurerAdapter {
//链式编程
//授权
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/").permitAll()//权限请求,首页都可以允许访问
.antMatchers("/leven1/**").hasRole("vip1");//用户角色为vip1才允许访问
//读源码发现,登录默认登录页面/login,认证失败走login?error
//自己的页面代替SpringSecurity的login页面, .loginPage("/tologin")
// 跳转真实的页面加上 .loginProcessingUrl("/login")
http.formLogin().loginPage("/tologin");
//开启注销功能,并跳到主页
http.logout().logoutSuccessUrl("/");
http.csrf().disable();//关闭csrf功能:跨站请求伪造,默认只能通过post方式提交logout请求 登出失败的原因
http.rememberMe().rememberMeParameter("remember");//记住我功能, .rememberMeParameter("remember")自定义标签
}
//认证
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//在内存中读,也可以在数据库中读
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())//加密方式
.withUser("aa").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2")
.and()
.withUser("ww").password(new BCryptPasswordEncoder().encode("456")).roles("vip4");
}
}
一般在thymeleaf中搭配:
1.引入启动类
org.thymeleaf.extras
thymeleaf-extras-springsecurity5
3.0.4.RELEASE
2.常用标签
当前用户没有被认证/认证失败当前用户认证成功当前用户有vip1的权限的情况下Java相关栏目本月热门文章
- 1【Linux驱动开发】设备树详解(二)设备树语法详解
- 2别跟客户扯细节
- 3Springboot+RabbitMQ+ACK机制(生产方确认(全局、局部)、消费方确认)、知识盲区
- 4【Java】对象处理流(ObjectOutputStream和ObjectInputStream)
- 5【分页】常见两种SpringBoot项目中分页技巧
- 6一文带你搞懂OAuth2.0
- 7我要写整个中文互联网界最牛逼的JVM系列教程 | 「JVM与Java体系架构」章节:虚拟机与Java虚拟机介绍
- 8【Spring Cloud】新闻头条微服务项目:FreeMarker模板引擎实现文章静态页面生成
- 9JavaSE - 封装、static成员和内部类
- 10树莓派mjpg-streamer实现监控及拍照功能调试
- 11用c++写一个蓝屏代码
- 12从JDK8源码中看ArrayList和LinkedList的区别
- 13idea 1、报错java: 找不到符号 符号: 变量 log 2、转换成Maven项目
- 14在openwrt使用C语言增加ubus接口(包含C uci操作)
- 15Spring 解决循环依赖
- 16SpringMVC——基于MVC架构的Spring框架
- 17Andy‘s First Dictionary C++ STL set应用
- 18动态内存管理
- 19我的创作纪念日
- 20Docker自定义镜像-Dockerfile



