第三章:springboot-springsecurity登录成功与失败的处理

前言:还记得上一章中WebSecurityConfigurerAdapter 配置注释掉的
//认证成功的返回处理下章再讲
// .successHandler()
//认证失败的返回处理
// .failureHandler()

在项目开发中一般是前后分离的,前后端统一达成一定规格的请求返回的数据规格。
成功认证处理:

package com.wyb.seurity;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;

/**
 * 成功返回处理
 */
public class WebAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest,
                                        HttpServletResponse httpServletResponse,
                                        Authentication authentication) throws IOException, ServletException {
        httpServletResponse.setContentType("application/json;charset=UTF-8");

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("msg","登录成功");
        hashMap.put("authentication",authentication);
        System.out.println(authentication.getDetails());
        //spring提供的状态码
        httpServletResponse.setStatus(HttpStatus.OK.value());
        String s = new ObjectMapper().writeValueAsString(hashMap);
        httpServletResponse.getWriter().println(s);
    }
}

认证成功的返回:
上期密码配置

spring:
  application:
    name: wyb-admin-security-resourceapi
  security:
    user:
      name: user
      password: 123456
      roles: admin,super

在这里插入图片描述

失败的返回处理:

package com.wyb.seurity;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;

/**
 * 认证失败
 */
public class WebAuthenticationFailureHandler implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        httpServletResponse.setContentType("application/json;charset=UTF-8");

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("msg","登录失败");
        hashMap.put("e",e.getMessage());
        //spring提供的状态码
        httpServletResponse.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
        String s = new ObjectMapper().writeValueAsString(hashMap);
        httpServletResponse.getWriter().println(s);
    }
}

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值