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

广告网站建设网百度游戏中心app

广告网站建设网,百度游戏中心app,和网站设计人员谈价要注意什么,网站建设及维护机文章目录 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.hengruixuexiao.com/news/21416.html

相关文章:

  • wordpress如何做页面模板下载seo专员招聘
  • 网站建设学多久电商平台怎么注册
  • 怎样用自己的电脑做网站百度公司电话热线电话
  • 如何免费建立网站友情链接英文翻译
  • 网站开发最适合的浏览器佛山旺道seo
  • 校园网站开发的需求分析搜索引擎优化包括哪些方面
  • 单页网站与传统网站的区别链接交换
  • 网站网站开发的公司电话搜狗站长平台验证网站
  • 邢台建网站公司站长工具查询入口
  • 个人网页模板html代码简述搜索引擎优化
  • 360营销推广搜索引擎营销简称seo
  • 做病毒和木马的培训网站全专业优化公司
  • 全国最大装修网站排名环球网疫情最新动态
  • 河北网站开发永久免费自助建站平台
  • 电子商务网站建设重点seo推广官网
  • pc网站做移动端适配重庆搜索引擎seo
  • 兰州网站建设平台分析东莞疫情最新通知
  • 长沙做网站价格宁波网站建设
  • 网站设计公司长沙公司网站测试
  • 网页百度优化大师有用吗
  • 萍乡公司做网站关键词名词解释
  • 吴中区两学一做网站一套完整的运营方案
  • 苏州市网站建设网址解析ip地址
  • 做外贸生意的网站重庆森林为什么不能看
  • 竹中建设官方网站短视频营销
  • wordpress建站环境搭建博客网站注册
  • 电子商城网站系统平台外宣推广技巧
  • 网站反向代理怎么做搜索引擎网站大全
  • 给别人做网站自己建个网站要多少钱
  • 东城手机网站建设淮北网站建设