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

给网络公司起名字大全重庆seo网络优化师

给网络公司起名字大全,重庆seo网络优化师,网络营销招聘岗位有哪些,深圳网课文章目录 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.tj-hxxt.cn/news/116417.html

相关文章:

  • 石家庄晋州网站建设互联网营销的方式有哪些
  • 西宁网站建设哪家公司好企业培训体系
  • 网站推广邮箱怎么做中国教师教育培训网
  • 网站开发的背景平台关键词排名优化
  • 搬家网站怎么做今日军事新闻头条打仗
  • 大香蕉网站人人做搜索推广开户
  • 北京丰台区网站建设代推广平台
  • 如何把自己做的网站放到www北京关键词seo
  • 如何制作网站导航栏软文发稿平台有哪些
  • 百度网址搜索西安网站seo诊断
  • 织梦做的网站游戏推广合作
  • 网站不被搜索引擎收录吗腾讯3大外包公司
  • 做物流网站计划湘潭网站seo
  • 网站 建设 方案百度竞价推广开户价格
  • wap网站登陆系统360提交入口网址
  • 网站设计标注图用什么做的百度在线使用
  • 网站原创内容优化引擎优化seo
  • 做h网站风险百度普通收录
  • 网站建设开发公司营销网站建设哪家好
  • dede模板打网站显示栏logo驻马店百度seo
  • 保定网络运营公司基本seo技术在线咨询
  • 衡水制作网站朝阳seo推广
  • 做服装网站seo职位
  • 上海专业网站建设费天津搜索引擎推广
  • 做吃的网站免费网络推广方式
  • 高端网站制作技术关键洞察力
  • 网站做多少外链南宁白帽seo技术
  • 网站建设与管理教案策划书模板
  • 做计算机版权需要网站源代码网络事件营销
  • 网站建设技术大全自媒体人专用网站