springsecurity前后端分离处理器

springsecurity前后端分离处理器

1.创建ResponseResult类

package com.woniu.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class ResponseResult<T> {
    private T data;

    private Integer code;//200=成功 500=失败

    private String msg;//提示

    public static final ResponseResult<Void> SECCUSS=new ResponseResult(ResultEnum.SECCUSS.getCode(),ResultEnum.SECCUSS.getMsg());
    public static final ResponseResult<Void> FAIL=new ResponseResult(ResultEnum.FAIL.getCode(),ResultEnum.FAIL.getMsg());
    public static final ResponseResult<Void> NOLOGIN=new ResponseResult(ResultEnum.NOLOGIN.getCode(),ResultEnum.NOLOGIN.getMsg());
    public static final ResponseResult<Void> NOAUTH=new ResponseResult(ResultEnum.NOAUTH.getCode(),ResultEnum.NOAUTH.getMsg());
    public static final ResponseResult<Void> LOGOUT=new ResponseResult(ResultEnum.LOGOUT.getCode(),ResultEnum.LOGOUT.getMsg());

    public ResponseResult(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }
}

2. 创建ResultEnum枚举类

package com.woniu.entity;


public enum ResultEnum {

    SECCUSS(200,"登录成功!"),
    FAIL(2000,"登录失败!"),
    NOLOGIN(2001,"未登录,请先登录!"),
    NOAUTH(2002,"无此操作权限,请联系管理员!"),
    LOGOUT(2003,"退出成功!")
    ;

    private Integer code;
    private String msg;

    @Override
    public String toString() {
        return "ResultEnum{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                '}';
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    ResultEnum(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    ResultEnum() {
    }
}

3.

package com.woniu.handler;

import com.alibaba.fastjson.JSON;
import com.woniu.entity.ResponseResult;
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.io.PrintWriter;

/**
 * 前后端分离的项目情况下,登录成功后返回的不再是一个页面,而是一个json
 * 处理用户登录成功后返回给前端的数据:比如用户名等信息
 */
public class LoginSuccessHandler implements AuthenticationSuccessHandler
{
    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {

        httpServletResponse.setContentType("application/json;charset=utf-8");
        PrintWriter pw = httpServletResponse.getWriter();

        String json = JSON.toJSONString(ResponseResult.SECCUSS);

        pw.print(json);
        pw.flush();
        pw.close();
    }
}

4.

package com.woniu.handler;

import com.alibaba.fastjson.JSON;
import com.woniu.entity.ResponseResult;
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.io.PrintWriter;

/**
 * 登录失败后返回给前端的提示信息
 */
public class LoginFailHandler implements AuthenticationFailureHandler
{
    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        httpServletResponse.setContentType("application/json;charset=utf-8");
        PrintWriter pw = httpServletResponse.getWriter();

        String json = JSON.toJSONString(ResponseResult.FAIL);

        pw.print(json);
        pw.flush();
        pw.close();
    }
}

5.

package com.woniu.handler;

import com.alibaba.fastjson.JSON;
import com.woniu.entity.ResponseResult;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

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

/**
 * 前后端分离
 * 用户未登录直接访问系统资源
 * 会被该类拦截
 */
public class MyAuthenticationEntryPoint implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        httpServletResponse.setContentType("application/json;charset=utf-8");
        PrintWriter pw = httpServletResponse.getWriter();

        String json = JSON.toJSONString(ResponseResult.NOLOGIN);

        pw.print(json);
        pw.flush();
        pw.close();
    }
}

6.

package com.woniu.handler;

import com.alibaba.fastjson.JSON;
import com.woniu.entity.ResponseResult;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

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

/**
 * 虽然知道用户名密码
 * 拦截没有权限访问该资源的操作
 */
public class MyAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException, ServletException {
        httpServletResponse.setContentType("application/json;charset=utf-8");
        PrintWriter pw = httpServletResponse.getWriter();

        String json = JSON.toJSONString(ResponseResult.NOAUTH);

        pw.print(json);
        pw.flush();
        pw.close();
    }
}

7.

package com.woniu.handler;

import com.alibaba.fastjson.JSON;
import com.woniu.entity.ResponseResult;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

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

/**
 * 用户退出
 */
public class MyLogoutSuccessHandler implements LogoutSuccessHandler {
    @Override
    public void onLogoutSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
        httpServletResponse.setContentType("application/json;charset=utf-8");
        PrintWriter pw = httpServletResponse.getWriter();

        String json = JSON.toJSONString(ResponseResult.LOGOUT);

        pw.print(json);
        pw.flush();
        pw.close();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值