当前位置: 首页 > news >正文

系列五、Spring Security中的认证 授权(前后端分离)

一、Spring Security中的认证 & 授权(前后端分离)

1.1、MyWebSecurityConfigurerAdapter7002

/*** @Author : 一叶浮萍归大海* @Date: 2024/1/11 21:50* @Description: Spring Security配置类*/
@Configuration
public class MyWebSecurityConfigurerAdapter7002 extends WebSecurityConfigurerAdapter {@Resourceprivate MyAuthenticationSuccessHandler7002 successHandler;@Resourceprivate MyAuthenticationFailureHandler7002 failureHandler;@Resourceprivate MyLogoutSuccessHandler logoutSuccessHandler;@Resourceprivate MyAuthenticationEntryPoint authenticationEntryPoint;/*** 密码加密器* @return*/@BeanPasswordEncoder passwordEncoder() {return NoOpPasswordEncoder.getInstance();}/*** 配置基于内存的用户* @param auth* @throws Exception*/@Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {auth.inMemoryAuthentication().withUser("admin").password("123456").roles("admin").and().withUser("root").password("123456").roles("root");}@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().antMatchers("/helloWorld").permitAll().anyRequest().authenticated().and()/*** 登录成功 & 登录失败回调*/.formLogin().loginPage("/login").successHandler(successHandler).failureHandler(failureHandler).and()/*** 注销登录回调*/.logout().logoutUrl("/logout").logoutSuccessHandler(logoutSuccessHandler).permitAll().and().csrf().disable()/*** 未认证回调*/.exceptionHandling().authenticationEntryPoint(authenticationEntryPoint);}}

1.2、MyAuthenticationSuccessHandler7002

/*** @Author : 一叶浮萍归大海* @Date: 2024/1/12 09:55* @Description: 认证(登录)成功处理器*/
@Component
public class MyAuthenticationSuccessHandler7002 implements AuthenticationSuccessHandler {@Overridepublic void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {response.setContentType("application/json;charset=utf-8");PrintWriter out = response.getWriter();R r = R.ok().data(authentication.getPrincipal());out.write(new ObjectMapper().writeValueAsString(r));out.flush();out.close();}
}

1.3、MyAuthenticationFailureHandler7002

/*** @Author : 一叶浮萍归大海* @Date: 2023/1/12 10:05* @Description: 认证(登录)失败处理器*/
@Component
public class MyAuthenticationFailureHandler7002 implements AuthenticationFailureHandler {@Overridepublic void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {response.setContentType("application/json;charset=utf-8");PrintWriter out = response.getWriter();R r = R.error();if (exception instanceof LockedException) {r.data(SpringSecurityConstants.LOCKED_ERROR_MESSAGE);} else if (exception instanceof CredentialsExpiredException) {r.data(SpringSecurityConstants.CREDENTIALS_EXPIRED_ERROR_MESSAGE);} else if (exception instanceof AccountExpiredException) {r.data(SpringSecurityConstants.ACCOUNT_EXPIRED_ERROR_MESSAGE);} else if (exception instanceof DisabledException) {r.data(SpringSecurityConstants.DISABLED_ERROR_MESSAGE);} else if (exception instanceof BadCredentialsException) {r.data(SpringSecurityConstants.BAD_CREDENTIALS_ERROR_MESSAGE);} else if (exception instanceof AuthenticationServiceException) {r.data(SpringSecurityConstants.VERIFY_CODE_ERROR_MESSAGE);} else {r.data(SpringSecurityConstants.LOGIN_ERROR_COMMON_MESSAGE);}out.write(new ObjectMapper().writeValueAsString(r));out.flush();out.close();}
}

1.4、MyLogoutSuccessHandler7002

/*** @Author : 一叶浮萍归大海* @Date: 2024/01/12 11:26* @Description: 注销登录处理器*/
@Component
public class MyLogoutSuccessHandler7002 implements LogoutSuccessHandler {@Overridepublic void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {response.setContentType("application/json;charset=utf-8");PrintWriter out = response.getWriter();R r = R.ok().data(SpringSecurityConstants.LOGOUT_SUCCESS_MESSAGE);out.write(new ObjectMapper().writeValueAsString(r));out.flush();out.close();}}

1.5、MyAuthenticationEntryPoint7002

/*** @Author : 一叶浮萍归大海* @Date: 2024/01/12 11:27* @Description: 未认证处理方案(用户未登录就访问资源)*/
@Component
public class MyAuthenticationEntryPoint7002 implements AuthenticationEntryPoint {@Overridepublic void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {response.setContentType("application/json;charset=utf-8");PrintWriter out = response.getWriter();R r = R.error(ResponseEnum.HTTP_UNAUTHORIZED_ERROR.getCode(),ResponseEnum.HTTP_UNAUTHORIZED_ERROR.getMessage());out.write(new ObjectMapper().writeValueAsString(r));out.flush();out.close();}}

1.6、SpringSecurityConstants7002

/*** @Author 一叶浮萍归大海* @Description Spring Security认证 & 授权常量类* @Date 2024/01/12 10:06*/
public class SpringSecurityConstants {public static final String LOGOUT_SUCCESS_MESSAGE = "注销成功!";public static final String LOCKED_ERROR_MESSAGE = "账户被锁定,请联系管理员!";public static final String CREDENTIALS_EXPIRED_ERROR_MESSAGE = "密码过期,请联系管理员!";public static final String ACCOUNT_EXPIRED_ERROR_MESSAGE = "账户过期,请联系管理员!";public static final String DISABLED_ERROR_MESSAGE = "账户被禁用,请联系管理员!";public static final String BAD_CREDENTIALS_ERROR_MESSAGE = "用户名或者密码错误,请重新输入!";public static final String VERIFY_CODE_ERROR_MESSAGE = "验证码错误!";public static final String LOGIN_ERROR_COMMON_MESSAGE = "登录失败,请联系管理员!";
}

1.7、R

/*** @Author 一叶浮萍归大海* @Description* @Date 2023/01/12 10:06*/
@Data
public class R<T> {private Integer code;private String message;private T data;/*** 构造函数私有化*/private R(){}/*** 返回成功结果* @return*/public static R ok(){R r = new R();r.setCode(ResponseEnum.SUCCESS.getCode());r.setMessage(ResponseEnum.SUCCESS.getMessage());return r;}/*** 返回失败结果* @return*/public static R error(){R r = new R();r.setCode(ResponseEnum.ERROR.getCode());r.setMessage(ResponseEnum.ERROR.getMessage());return r;}public static R error(int code, String msg) {R r = new R();r.setCode(code);r.setMessage(msg);return r;}/*** 设置特定的结果* @param responseEnum* @return*/public static R setResult(ResponseEnum responseEnum){R r = new R();r.setCode(responseEnum.getCode());r.setMessage(responseEnum.getMessage());return r;}public R data(T entity) {this.setData(entity);return this;}/*** 设置特定的响应消息* @param message* @return*/public R message(String message){this.setMessage(message);return this;}/*** 设置特定的响应码* @param code* @return*/public R code(Integer code){this.setCode(code);return this;}
}

1.8、ResponseEnum

/*** @Author 一叶浮萍归大海* @Description* @Date 2023/5/30 15:55*/
@Getter
@ToString
@AllArgsConstructor
public enum ResponseEnum {/*** 响应状态码 & 响应信息映射*/SUCCESS(200, "成功!"),ERROR(201, "失败!"),SERVER_INTERNAL_ERROR(500, "服务器内部错误,请联系管理员!"),PARAMETER_VALIDATE_FAILED_ERROR(10001, "参数校验失败,请联系管理员!"),BUSINESS_ERROR(10002, "业务异常,请联系管理员"),// =================== Spring Cloud Alibaba Sentinel统一异常处理 ===================SENTINEL_FLOW_EXCEPTION(20001,"接口被限流,请联系管理员!"),SENTINEL_DEGRADE_EXCEPTION(20002,"接口被降级,请联系管理员!"),SENTINEL_PARAM_FLOW_EXCEPTION(20003,"热点参数限流,请联系管理员!"),SENTINEL_SYSTEM_BLOCK_EXCEPTION(20004,"触发系统保护规则,请联系管理员!"),SENTINEL_AUTHORITY_EXCEPTION(20005,"授权规则不通过,请联系管理员!"),// =================== Spring Security统一异常处理 ===================HTTP_UNAUTHORIZED_ERROR(401, "尚未登录,请登录!"),HTTP_FORBIDDEN_ERROR(403, "权限不足,请联系管理员!"),;/*** 响应状态码*/private Integer code;/*** 响应信息*/private String message;}

相关文章:

  • 基于elementUI的el-table组件实现按住某一行数据上下滑动选中/选择或取消选中/选择鼠标经过的行
  • 北斗卫星技术在建筑监测领域的革新实践
  • 最新使用宝塔反代openai官方API接口搭建详细教程及502 Bad Gateway错误问题解决
  • MySQL修炼手册7:数据修改基础:INSERT、UPDATE、DELETE语句详解
  • SpringBoot外部配置文件
  • Cesium 模型压平
  • HTTP超文本传输协议
  • 广东省第三届职业技能大赛“网络安全项目”B模块任务书
  • 【计算机网络 谢希仁 第八版笔记】第一章 概述
  • Python——VScode安装
  • Excel地址
  • 使用Qt连接scrcpy-server控制手机
  • vue2使用 element表格展开功能渲染子表格
  • unity C#中使用ref、out区别和使用案例
  • 【干货】深入剖析选择排序算法:原理、步骤与复杂度分析
  • 「前端」从UglifyJSPlugin强制开启css压缩探究webpack插件运行机制
  • 【JavaScript】通过闭包创建具有私有属性的实例对象
  • ES2017异步函数现已正式可用
  • ES6--对象的扩展
  • JavaScript 基础知识 - 入门篇(一)
  • Java应用性能调优
  • js中forEach回调同异步问题
  • leetcode386. Lexicographical Numbers
  • webpack4 一点通
  • webpack入门学习手记(二)
  • Windows Containers 大冒险: 容器网络
  • 阿里云爬虫风险管理产品商业化,为云端流量保驾护航
  • 闭包,sync使用细节
  • 从tcpdump抓包看TCP/IP协议
  • 翻译:Hystrix - How To Use
  • 个人博客开发系列:评论功能之GitHub账号OAuth授权
  • 老板让我十分钟上手nx-admin
  • 聊聊flink的BlobWriter
  • 深度解析利用ES6进行Promise封装总结
  • 使用 Node.js 的 nodemailer 模块发送邮件(支持 QQ、163 等、支持附件)
  • 微服务框架lagom
  • 在electron中实现跨域请求,无需更改服务器端设置
  • #我与Java虚拟机的故事#连载09:面试大厂逃不过的JVM
  • (007)XHTML文档之标题——h1~h6
  • (2015)JS ES6 必知的十个 特性
  • (3)选择元素——(14)接触DOM元素(Accessing DOM elements)
  • (cljs/run-at (JSVM. :browser) 搭建刚好可用的开发环境!)
  • (C语言)二分查找 超详细
  • (poj1.2.1)1970(筛选法模拟)
  • (windows2012共享文件夹和防火墙设置
  • (八)Spring源码解析:Spring MVC
  • (附源码)springboot电竞专题网站 毕业设计 641314
  • (附源码)计算机毕业设计大学生兼职系统
  • (十六)Flask之蓝图
  • (十五)devops持续集成开发——jenkins流水线构建策略配置及触发器的使用
  • (数位dp) 算法竞赛入门到进阶 书本题集
  • (一)认识微服务
  • (原)记一次CentOS7 磁盘空间大小异常的解决过程
  • (终章)[图像识别]13.OpenCV案例 自定义训练集分类器物体检测
  • (转)3D模板阴影原理