详解springboot springsecuroty中的注销和权限控制问题

网友投稿 475 2022-08-22


详解springboot springsecuroty中的注销和权限控制问题

目录1账户注销1.1在SecurityConfig中加入开启注销功能的代码1.2在index.html添加注销的按钮1.3启动项目测试2权限控制2.1导入springsecurity和thymeleaf的整合依赖2.2springboot版本降级2.3引入约束2.4修改页面代码2.5重启程序测试

上篇文章给大家介绍了springboot对接第三方微信授权及获取用户的头像和昵称等等

1 账户注销

1.1 在SecurityConfig中加入开启注销功能的代码

src/main/java/com/lv/config/SecurityConfig.java

package com.lv.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.WebSecurityConfigurerAdapter;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

//AOP : 拦截器!

@EnableWebSecurity

public class SecurityConfig extends WebSecurityConfigurerAdapter {

//授权

@Override

public 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.logout().logoutSuccessUrl("/");

//防止跨站工具, get,post

http.csrf().disable();//关闭csrf功能,注销失败可能的原因

}

//认证,springboot 2.1.x 可以直接使用

//密码编码:PasswordEncoder

//在Spring Security 5.0+ 新增了很多加密方法~

protected void configure(AuthenticationManagerBuilder auth) throws Exception {

//这些数据正常应该从数据库中读

auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())

.withUser("lv").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2","vip3")

.and()

.withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2","vip3")

.withUser("guest").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1");

}

1.2 在index.html 添加注销的按钮

src/main/resources/templates/index.html

登录

注销

1.3 启动项目测试

访问登录页面,登录 guest 账户,该账户可以访问 level1的页面

登录成功后,点击 level1的链接,成功跳转到 level 页面,然后点击注销按钮

弹回到首页,再次点击点击 level1 页面

跳转到了登录页面

说明账户注销成功

2 权限控制

2.1 导入springsecurity和thymeleaf的整合依赖

pom.xml

org.thymeleaf.extras

thymeleaf-extras-springsecurity4

3.0.2.RELEASE

2.2 springboot版本降级

pom.xml

org.springframework.boot

spring-boot-starter-parent

2.0.9.RELEASE

必须将springboot的版本降到2.0.9以下,否则 sec:authorize="isAuthenticated()" 不会生效.版本降低后,需要手动导入junit依赖,否则测试类会报错

org.junit.jupiter

junit-jupiter

RELEASE

test

2.3 引入约束

在index.html的头文件中添加springsecurity和thymeleaf的整合约束

src/main/resources/templates/index.html

xmlns:sec="http://thymeleaf.org/thymeleaf-extras-springsecurity4">

2.4 修改页面代码

主要修改两部分,一部分是登录状态下显示用户名,和注销按钮,未登录显示登录按钮 通过 sec:authorize="isAuthenticated()" 实现.另一部分是根据登录用户的权限显示不同的页面菜单,通过 sec:authorize="hasRole('vip1')" 实现.

src/main/resources/templates/index.html

xmlns:sec="http://thymeleaf.org/thymeleaf-extras-springsecurity4">

首页

登录

用户名:

注销


http://

2.5 重启程序测试

未登录页面

登录 lv 用户的页面

登录 geust 用户的页面

登录 root 用户的页面

页面显示都不同,权限控制成功实现


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:python垃圾箱-垃圾回收(python 垃圾回收机制什么时候回收)
下一篇:python编程进阶学习笔记(python编程基础与案例集锦)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~