权限即不同用户可以使用不同功能

实现前置:

在上一次登录与校验中,我们将authentication存入到SecurityContextHolder中,后续我们需要从FilterSecurityInterceptor取出并进行验证

基于注解实现

开启相关配置(放在config里)

@EnableGlobalMethodSecurity(prePostEnabled = true) //适用于springboot2
@EnableMethodSecurity(prePostEnabled = true) //适用于springboot3

对应主要注解

@PreAuthorize("hasAuthority('ROLE_USER')")

需要修改的有两部分:

1.在我们自定义的UserDetailService中我们重写了loadUserByUsername:返回的LoginUser还包括了权限。在上一次,我们仅仅向里封装了一个User信息。除此以外,我们还需要一个权限信息,因此基于上次的User信息,我们对LoginUser增加了成员变量

@Data
@NoArgsConstructor
public class LoginUser implements UserDetails {
private User user;
private List<String> permissions;
// 这里不进行序列化/反序列化的原因是SimpleGrantedAuthority没实现序列化,而且我们已经有permissions,可直接通过get方法获取authorities
// 其实他可以不做成员变量,但是这样可以节省系统开销:不必多次创建authorities
@JSONField(serialize = false)
private List<SimpleGrantedAuthority> authorities;

public LoginUser(User user, List<String> permissions) {
this.user = user;
this.permissions = permissions;
}

@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
if(this.authorities!=null){
return this.authorities;
}
//如何选定的simplegrantedauthority:
// 通过查看grantedAuhority源码,通过ctrl+alt点击这个接口,我们发现他有三个实现类,我们从中选择其一
// 本方法即把string转换为相关的权限类型以便于preAuthorize(hasAuthority(''))使用
this.authorities=this.permissions.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());
return this.authorities;
}

@Override
public String getPassword() {
return user.getPassword();
}

@Override
public String getUsername() {
return user.getUserName();
}

@Override
public boolean isAccountNonExpired() {
return true;
}

@Override
public boolean isAccountNonLocked() {
return true;
}

@Override
public boolean isCredentialsNonExpired() {
return true;
}

@Override
public boolean isEnabled() {
return true;
}
}

2.在拦截器中写入对应的权限信息

 SecurityContextHolder.getContext().
setAuthentication(new UsernamePasswordAuthenticationToken(loginUser,null,loginUser.getAuthorities()));

异常:异常一般有两种:登录不上去,权限不够

1.登录失败(身份入口)新增异常处理

@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {
@Override
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
ResponseResult result = new ResponseResult(HttpStatus.UNAUTHORIZED.value(),"用户认证失败!请重新登录");
String json= JSON.toJSONString(result);
WebUtil.renderString(response, json);
}
}

2.权限不足新增异常处理

@Component
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {

@Override
public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
ResponseResult result = new ResponseResult(HttpStatus.FORBIDDEN.value(),"用户权限不足!");
String json= JSON.toJSONString(result);
WebUtil.renderString(response, json);
}
}

WebUtil

public class WebUtil {
/**
* 将字符串渲染到客户端
*
* @param response 渲染对象
* @param string 待渲染的字符串
* @return null
*/
public static String renderString(HttpServletResponse response, String string) {
try {
response.setStatus(200);
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
response.getWriter().print(string);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}

修改securityConfig

// 添加异常处理器
http
.exceptionHandling()
.authenticationEntryPoint(authenticationEntryPoint)
.accessDeniedHandler(accessDeniedHandler);
 

springsecurity:权限与异常处理的更多相关文章

  1. SpringSecurity权限管理系统实战—二、日志、接口文档等实现

    系列目录 SpringSecurity权限管理系统实战-一.项目简介和开发环境准备 SpringSecurity权限管理系统实战-二.日志.接口文档等实现 SpringSecurity权限管理系统实战 ...

  2. SpringSecurity权限管理系统实战—七、处理一些问题

    目录 SpringSecurity权限管理系统实战-一.项目简介和开发环境准备 SpringSecurity权限管理系统实战-二.日志.接口文档等实现 SpringSecurity权限管理系统实战-三 ...

  3. SpringSecurity权限管理系统实战—一、项目简介和开发环境准备

    目录 SpringSecurity权限管理系统实战-一.项目简介和开发环境准备 SpringSecurity权限管理系统实战-二.日志.接口文档等实现 SpringSecurity权限管理系统实战-三 ...

  4. SpringSecurity权限管理系统实战—四、整合SpringSecurity(上)

    目录 SpringSecurity权限管理系统实战-一.项目简介和开发环境准备 SpringSecurity权限管理系统实战-二.日志.接口文档等实现 SpringSecurity权限管理系统实战-三 ...

  5. SpringSecurity权限管理系统实战—六、SpringSecurity整合jwt

    目录 SpringSecurity权限管理系统实战-一.项目简介和开发环境准备 SpringSecurity权限管理系统实战-二.日志.接口文档等实现 SpringSecurity权限管理系统实战-三 ...

  6. SpringSecurity权限管理系统实战—八、AOP 记录用户、异常日志

    目录 SpringSecurity权限管理系统实战-一.项目简介和开发环境准备 SpringSecurity权限管理系统实战-二.日志.接口文档等实现 SpringSecurity权限管理系统实战-三 ...

  7. SpringSecurity权限管理系统实战—九、数据权限的配置

    目录 SpringSecurity权限管理系统实战-一.项目简介和开发环境准备 SpringSecurity权限管理系统实战-二.日志.接口文档等实现 SpringSecurity权限管理系统实战-三 ...

  8. spring-security权限控制详解

    在本例中,主要讲解spring-boot与spring-security的集成,实现方式为: 将用户.权限.资源(url)采用数据库存储 自定义过滤器,代替原有的 FilterSecurityInte ...

  9. SpringSecurity权限管理系统实战—五、整合SpringSecurity(下)

    系列目录 前言 上篇文章SpringSecurity整合了一半,这次把另一半整完,所以本篇的序号接着上一篇. 七.自定义用户信息 前面我们登录都是用的指定的用户名和密码或者是springsecurit ...

  10. sonar + jacoco + mockMvc 模拟session 用户登录 配合SpringSecurity 权限 快速测试代码覆盖率.

    遇到mock 测试简直就是神器,特别是要做代码覆盖率,直接测试controller就好了,缺点,虽然可以回滚事务,但是依赖数据库数据,解决,根据SpringBoot ,再建立一个专门跑单元测试的数据库 ...

随机推荐

  1. WPF 实现触摸滑动功能

    自定义ScrollViewer的Touch事件--触摸上下移动ScrollViewer滚动到指定位置   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...

  2. js 设置复选框选中后不勾选

    今天在解决bug的时候,明明觉得这是个小问题,但是在我去调试的时候,发现使用setAttribute一直没得效果,虽然input框已经设置了checked属性,但是无法勾选,于是在网上找了一大堆,发现 ...

  3. Vue前端的搭建(与后端JavaEE的连接)

    目录 前端平台搭建(Vue2.6,App:HBulderX) 创建Vue2.6项目 下载相应插件方便开发 路由配置 对连接后端进行一些配置(main.js文件) 导入ElementUI组件 组件 | ...

  4. react表单处理 受控组件

    将state与表单项中的value值绑定在一起,有state的值来控制表单元素的值,称为受控组件. 绑定步骤: 在state中添加一个状态,作为表单元素的value值 给表单元素绑定change事件, ...

  5. win10系统(专业版)实现双网卡链路聚合

    win10系统(专业版)实现双网卡链路聚合 参考: https://learn.microsoft.com/zh-cn/powershell/module/netswitchteam/new-nets ...

  6. vite+vue3+ts+elementPlus前端框架搭建 [一]

    记录下搭建vite + vue3 + ts + elementPlus项目的过程及遇到的问题. 建议使用pnpm安装依赖,npm切换到pnpm 链接地址:[https://www.pnpm.cn/in ...

  7. uniapp 使用z-paging 分页组件 写在头部插槽内的单选按钮无法点击

    这个问题是因为组件层级太低 <z-paging ref="paging" v-model="dataList" @query="queryLis ...

  8. linux下安装oracle 11g(静默安装)

    关闭selinux 关闭防火墙 检查安装依赖包 yum -y install binutils compat-libcap1 vsftpd gcc gcc-c++ glibc-devel glibc ...

  9. 基于人类反馈的强化学习,Reinforcement Learning from Human Feedback (RLHF)

    基于人类反馈的强化学习, RLHF,转载参考链接 RLHF 是一项涉及多个模型和不同训练阶段的复杂概念,可以按三个步骤分解: 预训练一个语言模型 (LM) : 聚合问答数据并训练一个奖励模型 (Rew ...

  10. Linux Centos7搭建RabbitMQ

    下载依赖 yum -y install epel-release yum -y update 安装Erlang yum -y install erlang socat 测试安装成功 erl -vers ...