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

网站子目录是什么意思济南建设银行网点

网站子目录是什么意思,济南建设银行网点,网站备案增加域名解析,郑州电力高等专科学校招生网文章目录 SpringSecurity 返回json一、登录成功处理器1.1 统一响应类HttpResult1.2 登录成功处理器1.3 配置登录成功处理器1.4 登录 二、登录失败处理器2.1 登录失败处理器2.2 配置登录失败处理器2.3 登录 三、退出成功处理器3.1 退出成功处理器3.2 配置退出成功处理器3.3 退出…

文章目录

  • SpringSecurity 返回json
  • 一、登录成功处理器
    • 1.1 统一响应类HttpResult
    • 1.2 登录成功处理器
    • 1.3 配置登录成功处理器
    • 1.4 登录
  • 二、登录失败处理器
    • 2.1 登录失败处理器
    • 2.2 配置登录失败处理器
    • 2.3 登录
  • 三、退出成功处理器
    • 3.1 退出成功处理器
    • 3.2 配置退出成功处理器
    • 3.3 退出
  • 四、访问拒绝(无权限)处理器
    • 4.1 访问拒绝处理器
    • 4.2 配置访问拒绝处理器
    • 4.3 被拒绝
  • 五、自定义处理器

SpringSecurity 返回json

承接:1.SpringSecurity -快速入门、加密、基础授权-CSDN博客

一、登录成功处理器

前后端分离成为企业应用开发中的主流,前后端分离通过json进行交互,登录成功和失败后不用页面跳转,而是一段json提示

1.1 统一响应类HttpResult

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class HttpResult {private Integer code;private String msg;private Object data;public HttpResult(Integer code, String msg) {this.code = code;this.msg = msg;}
}

1.2 登录成功处理器

/*** 认证成功就会调用该接口里的方法*/
@Component
public class AppAuthenticationSuccessHandle implements AuthenticationSuccessHandler {//  JSON序列化器,进行序列化和反序列化@Resourceprivate ObjectMapper objectMapper;;@Overridepublic void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
//      定义返回对象httpResultHttpResult httpResult = HttpResult.builder().code(200).msg("登陆成功").build();String strResponse = objectMapper.writeValueAsString(httpResult);//      响应字符集response.setCharacterEncoding("UTF-8");
//      响应内容类型JSON,字符集utf-8response.setContentType("application/json;charset=utf-8");
//      响应给前端PrintWriter writer = response.getWriter();writer.println(strResponse);writer.flush();}
}

1.3 配置登录成功处理器

@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {@Resourceprivate AppAuthenticationSuccessHandle appAuthenticationSuccessHandle;@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests()//授权http请求.anyRequest() //任何请求.authenticated();//都需要认证http.formLogin().successHandler(appAuthenticationSuccessHandle) //认证成功处理器.permitAll();//允许表单登录}}

1.4 登录

image-20231016223324743

登录成功后如下所示

image-20231016223344428

二、登录失败处理器

2.1 登录失败处理器

/*** 认证失败就会调用下面的方法*/
@Component
public class AppAuthenticationFailHandle implements AuthenticationFailureHandler {//  JSON序列化器,进行序列化和反序列化@Resourceprivate ObjectMapper objectMapper;;@Overridepublic void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {//      定义返回对象httpResultHttpResult httpResult = HttpResult.builder().code(401).msg("登录失败").build();String strResponse = objectMapper.writeValueAsString(httpResult);//      响应字符集response.setCharacterEncoding("UTF-8");
//      响应内容类型JSON,字符集utf-8response.setContentType("application/json;charset=utf-8");
//      响应给前端PrintWriter writer = response.getWriter();writer.println(strResponse);writer.flush();}
}

2.2 配置登录失败处理器

@Resource
private AppAuthenticationFailHandle appAuthenticationFailHandle;@Override
protected void configure(HttpSecurity http) throws Exception {http.authorizeRequests()//授权http请求.anyRequest() //任何请求.authenticated();//都需要认证http.formLogin().successHandler(appAuthenticationSuccessHandle) //认证成功处理器.failureHandler(appAuthenticationFailHandle) // 认证失败处理器.permitAll();//允许表单登录
}

2.3 登录

输入一个错误的密码

image-20231016224805298

如下图所示

image-20231016224824503

三、退出成功处理器

3.1 退出成功处理器

/*** 退出成功处理器*/
@Component
public class AppLogoutSuccessHandle implements LogoutSuccessHandler{//  JSON序列化器,进行序列化和反序列化@Resourceprivate ObjectMapper objectMapper;;@Overridepublic void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
//      定义返回对象httpResultHttpResult httpResult = HttpResult.builder().code(200).msg("退出成功").build();String strResponse = objectMapper.writeValueAsString(httpResult);//      响应字符集response.setCharacterEncoding("UTF-8");
//      响应内容类型JSON,字符集utf-8response.setContentType("application/json;charset=utf-8");
//      响应给前端PrintWriter writer = response.getWriter();writer.println(strResponse);writer.flush();}
}

3.2 配置退出成功处理器

@Resource
private AppLogoutSuccessHandle appLogoutSuccessHandle;@Override
protected void configure(HttpSecurity http) throws Exception {http.authorizeRequests()//授权http请求.anyRequest() //任何请求.authenticated();//都需要认证http.formLogin().successHandler(appAuthenticationSuccessHandle) //认证成功处理器.failureHandler(appAuthenticationFailHandle) // 认证失败处理器.permitAll();//允许表单登录http.logout().logoutSuccessHandler(appLogoutSuccessHandle);//登录成功处理器
}

3.3 退出

image-20231016231114408

四、访问拒绝(无权限)处理器

4.1 访问拒绝处理器

@Component
public class AppAccessDenyHandle implements AccessDeniedHandler {//  JSON序列化器,进行序列化和反序列化@Resourceprivate ObjectMapper objectMapper;;@Overridepublic void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {//      定义返回对象httpResultHttpResult httpResult = HttpResult.builder().code(403).msg("您没有权限访问该资源!!").build();String strResponse = objectMapper.writeValueAsString(httpResult);//      响应字符集response.setCharacterEncoding("UTF-8");
//      响应内容类型JSON,字符集utf-8response.setContentType("application/json;charset=utf-8");
//      响应给前端PrintWriter writer = response.getWriter();writer.println(strResponse);writer.flush();}
}

4.2 配置访问拒绝处理器

@Resource
private AppAccessDenyHandle appAccessDenyHandle;@Override
protected void configure(HttpSecurity http) throws Exception {http.authorizeRequests()//授权http请求.anyRequest() //任何请求.authenticated();//都需要认证http.formLogin().successHandler(appAuthenticationSuccessHandle) //认证成功处理器.failureHandler(appAuthenticationFailHandle) // 认证失败处理器.permitAll();//允许表单登录http.logout().logoutSuccessHandler(appLogoutSuccessHandle);//登录成功处理器;http.exceptionHandling()//异常处理.accessDeniedHandler(appAccessDenyHandle);//访问被拒绝处理器
}

4.3 被拒绝

image-20231016231313240

五、自定义处理器

SpringSecurity - 认证与授权、自定义失败处理、跨域问题、认证成功/失败处理器_我爱布朗熊的博客-CSDN博客

http://www.yayakq.cn/news/549508/

相关文章:

  • 湖南seo优化价格网站怎么优化seo
  • 定制v教程免费网站关键词seo排名
  • 深圳建设高端网站江苏省建设厅官网
  • 筛网怎么做网站百度收录查询接口
  • 广南网站建设做商业网站去哪里弄好
  • 优享揭阳网站建设asp网站的缺点
  • 网站系统维护要多久极简 单页面网站模板
  • 中国城乡建设厅网站鄂州网站建设哪家专业
  • 苏州企业网站制作电话wordpress做商城网站吗
  • 北京在线建站模板企业管理系统排名
  • 哪里的赣州网站建设上海网站设计开发公司
  • 制作公司网站要多少钱网络营销网站类型
  • 深圳鹏洲建设工程有限公司网站wordpress判断分类
  • 有网站做淘宝客临淄招聘信息网
  • 如何建自己网站做淘宝客又拍云WordPress 插件
  • 国外网站专题红黄产品推广渠道有哪些
  • 有哪些好的做兼职网站网络推广方案写作七步法
  • 网站内容与目录结构中小企业网络搭建
  • 建设一个网站需要注意哪些内容阜宁做网站的公司
  • 网站模板素材下载wordpress显示pdf
  • 网站对联广告图片公司怎么注册企业邮箱
  • 重庆网站制作权威乐云践新国内大宗商品交易平台有哪些
  • 企业网站优化设计应该把什么放在首位页面锚wordpress
  • 网站设计要考虑的因素公司管理系统是系统软件吗
  • 网站形式什么网站看电影是免费的
  • 服务哪家好网站制作网页做的很美的网站
  • 电子商务网站开发要学什么西安哪家公司做网站
  • 网站建设次年续费合同丹徒网站建设代理商
  • 企业网站设计网络公司织梦网站后台管理
  • 南京凯盛建设集团官方网站网站前台架构