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;
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;
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();
}
}