CRM项目基于Spring-Security处理登录请求和权限验证并工具类封装R对象回传前端------CRM项目

java">package ***.alatus.config.handler;

import ***.alatus.result.R;
import ***.alatus.util.JSONUtils;
import ***.alatus.util.ResponseUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.a***ess.A***essDeniedException;
import org.springframework.security.web.a***ess.A***essDeniedHandler;
import org.springframework.stereotype.***ponent;

import java.io.IOException;

@***ponent
public class MyA***essDeniedHandler implements A***essDeniedHandler {

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, A***essDeniedException a***essDeniedException) throws IOException, ServletException {
        //执行到这里,说明登录成功,那我们向前端返回json就行了
        //R result = R.FAIL(a***essDeniedException.getLocalizedMessage());
        R result = R.FAIL("抱歉,没有权限访问");

        //把R对象转成json
        String resultJSON = JSONUtils.toJSON(result);

        //把json写出去,写到浏览器
        ResponseUtils.write(response, resultJSON);
    }
}
package ***.alatus.config.handler;

import ***.alatus.result.R;
import ***.alatus.util.JSONUtils;
import ***.alatus.util.ResponseUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.a***ess.A***essDeniedException;
import org.springframework.security.web.a***ess.A***essDeniedHandler;
import org.springframework.stereotype.***ponent;

import java.io.IOException;

@***ponent
public class MyA***essDeniedHandler implements A***essDeniedHandler {

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, A***essDeniedException a***essDeniedException) throws IOException, ServletException {
        //执行到这里,说明登录成功,那我们向前端返回json就行了
        //R result = R.FAIL(a***essDeniedException.getLocalizedMessage());
        R result = R.FAIL("抱歉,没有权限访问");

        //把R对象转成json
        String resultJSON = JSONUtils.toJSON(result);

        //把json写出去,写到浏览器
        ResponseUtils.write(response, resultJSON);
    }
}
package ***.alatus.config.handler;

import ***.alatus.result.R;
import ***.alatus.util.JSONUtils;
import ***.alatus.util.ResponseUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.***ponent;

import java.io.IOException;

@***ponent
public class MyAuthenticationFailureHandler implements AuthenticationFailureHandler {

    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        //执行到这里,说明登录成功,那我们向前端返回json就行了
        R result = R.FAIL(exception.getLocalizedMessage());

        //把R对象转成json
        String resultJSON = JSONUtils.toJSON(result);

        //把json写出去,写到浏览器
        ResponseUtils.write(response, resultJSON);
    }
}
package ***.alatus.config.handler;

import ***.alatus.result.R;
import ***.alatus.util.JSONUtils;
import ***.alatus.util.ResponseUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.***ponent;

import java.io.IOException;

@***ponent
public class MyAuthenticationFailureHandler implements AuthenticationFailureHandler {

    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        //执行到这里,说明登录成功,那我们向前端返回json就行了
        R result = R.FAIL(exception.getLocalizedMessage());

        //把R对象转成json
        String resultJSON = JSONUtils.toJSON(result);

        //把json写出去,写到浏览器
        ResponseUtils.write(response, resultJSON);
    }
}
package ***.alatus.config.handler;

import ***.alatus.model.TUser;
import ***.alatus.result.R;
import ***.alatus.util.JSONUtils;
import ***.alatus.util.ResponseUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSu***essHandler;
import org.springframework.stereotype.***ponent;

import java.io.IOException;

@***ponent
public class MyAuthenticationSu***essHandler implements AuthenticationSu***essHandler {
    @Override
    public void onAuthenticationSu***ess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        //由于禁用了session,我们在登录成功后,需要在服务器保持用户的登录状态,前端下次来访问服务器端的时候,服务器端要知道这个人登录了
        TUser tUser = (TUser) authentication.getPrincipal();
//        登录成功的统一结果
        R result = R.OK(tUser);
//        把R对象转为JSON
        String resultJSON = JSONUtils.toJSON(result);
//        把R以JSON传回前端
        ResponseUtils.write(response,resultJSON);
    }
}
package ***.alatus.config.handler;

import ***.alatus.model.TUser;
import ***.alatus.result.R;
import ***.alatus.util.JSONUtils;
import ***.alatus.util.ResponseUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSu***essHandler;
import org.springframework.stereotype.***ponent;

import java.io.IOException;

@***ponent
public class MyAuthenticationSu***essHandler implements AuthenticationSu***essHandler {
    @Override
    public void onAuthenticationSu***ess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        //由于禁用了session,我们在登录成功后,需要在服务器保持用户的登录状态,前端下次来访问服务器端的时候,服务器端要知道这个人登录了
        TUser tUser = (TUser) authentication.getPrincipal();
//        登录成功的统一结果
        R result = R.OK(tUser);
//        把R对象转为JSON
        String resultJSON = JSONUtils.toJSON(result);
//        把R以JSON传回前端
        ResponseUtils.write(response,resultJSON);
    }
}
package ***.alatus.config.handler;

import ***.alatus.model.TUser;
import ***.alatus.result.CodeEnum;
import ***.alatus.result.R;
import ***.alatus.util.JSONUtils;
import ***.alatus.util.ResponseUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSu***essHandler;
import org.springframework.stereotype.***ponent;

import java.io.IOException;

@***ponent
public class MyLogoutSu***essHandler implements LogoutSu***essHandler {


    @Override
    public void onLogoutSu***ess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        //用户退出登录,那么把redis中的jwt删除
        TUser tUser = (TUser) authentication.getPrincipal();

        //执行到这里,说明退出成功,那我们向前端返回json就行了
        R result = R.OK(CodeEnum.OK);

        //把R对象转成json
        String resultJSON = JSONUtils.toJSON(result);

        //把json写出去,写到浏览器
        ResponseUtils.write(response, resultJSON);
    }
}
package ***.alatus.config.handler;

import ***.alatus.model.TUser;
import ***.alatus.result.CodeEnum;
import ***.alatus.result.R;
import ***.alatus.util.JSONUtils;
import ***.alatus.util.ResponseUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSu***essHandler;
import org.springframework.stereotype.***ponent;

import java.io.IOException;

@***ponent
public class MyLogoutSu***essHandler implements LogoutSu***essHandler {


    @Override
    public void onLogoutSu***ess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        //用户退出登录,那么把redis中的jwt删除
        TUser tUser = (TUser) authentication.getPrincipal();

        //执行到这里,说明退出成功,那我们向前端返回json就行了
        R result = R.OK(CodeEnum.OK);

        //把R对象转成json
        String resultJSON = JSONUtils.toJSON(result);

        //把json写出去,写到浏览器
        ResponseUtils.write(response, resultJSON);
    }
}
package ***.alatus.config.handler;

import ***.alatus.model.TUser;
import ***.alatus.result.CodeEnum;
import ***.alatus.result.R;
import ***.alatus.util.JSONUtils;
import ***.alatus.util.ResponseUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSu***essHandler;
import org.springframework.stereotype.***ponent;

import java.io.IOException;

@***ponent
public class MyLogoutSu***essHandler implements LogoutSu***essHandler {


    @Override
    public void onLogoutSu***ess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        //用户退出登录,那么把redis中的jwt删除
        TUser tUser = (TUser) authentication.getPrincipal();

        //执行到这里,说明退出成功,那我们向前端返回json就行了
        R result = R.OK(CodeEnum.OK);

        //把R对象转成json
        String resultJSON = JSONUtils.toJSON(result);

        //把json写出去,写到浏览器
        ResponseUtils.write(response, resultJSON);
    }
}
package ***.alatus.config.handler;

import ***.alatus.model.TUser;
import ***.alatus.result.CodeEnum;
import ***.alatus.result.R;
import ***.alatus.util.JSONUtils;
import ***.alatus.util.ResponseUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSu***essHandler;
import org.springframework.stereotype.***ponent;

import java.io.IOException;

@***ponent
public class MyLogoutSu***essHandler implements LogoutSu***essHandler {


    @Override
    public void onLogoutSu***ess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        //用户退出登录,那么把redis中的jwt删除
        TUser tUser = (TUser) authentication.getPrincipal();

        //执行到这里,说明退出成功,那我们向前端返回json就行了
        R result = R.OK(CodeEnum.OK);

        //把R对象转成json
        String resultJSON = JSONUtils.toJSON(result);

        //把json写出去,写到浏览器
        ResponseUtils.write(response, resultJSON);
    }
}
package ***.alatus.result;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public enum CodeEnum {
    OK(200,"成功"),
    FAIL(500,"失败");
//    结果码
    private int code;
//    结果信息
    private String msg;
}
package ***.alatus.result;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public enum CodeEnum {
    OK(200,"成功"),
    FAIL(500,"失败");
//    结果码
    private int code;
//    结果信息
    private String msg;
}
package ***.alatus.result;

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

//    封装web层向前端返回的结果
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Data
public class R {
//    返回的结果码
    private int code;
//    表示返回的结果信息,比如用户登陆状态失败了,请求参数格式有误
    private String msg;
//    返回的结果数据,可能是一个对象也可能是List集合
    private Object data;
    public static R OK(int code,String msg){
        return R.builder()
                .code(code)
                .msg(msg)
                .build();
    }
    public static R OK(Object data){
        return R.builder()
                .code(CodeEnum.OK.getCode())
                .msg(CodeEnum.OK.getMsg())
                .data(data)
                .build();
    }
    public static R FAIL(){
        return R.builder()
                .code(CodeEnum.FAIL.getCode())
                .msg(CodeEnum.FAIL.getMsg())
                .build();
    }
    public static R FAIL(String msg){
        return R.builder()
                .code(CodeEnum.FAIL.getCode())
                .msg(msg)
                .build();
    }
}
package ***.alatus.result;

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

//    封装web层向前端返回的结果
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Data
public class R {
//    返回的结果码
    private int code;
//    表示返回的结果信息,比如用户登陆状态失败了,请求参数格式有误
    private String msg;
//    返回的结果数据,可能是一个对象也可能是List集合
    private Object data;
    public static R OK(int code,String msg){
        return R.builder()
                .code(code)
                .msg(msg)
                .build();
    }
    public static R OK(Object data){
        return R.builder()
                .code(CodeEnum.OK.getCode())
                .msg(CodeEnum.OK.getMsg())
                .data(data)
                .build();
    }
    public static R FAIL(){
        return R.builder()
                .code(CodeEnum.FAIL.getCode())
                .msg(CodeEnum.FAIL.getMsg())
                .build();
    }
    public static R FAIL(String msg){
        return R.builder()
                .code(CodeEnum.FAIL.getCode())
                .msg(msg)
                .build();
    }
}
package ***.alatus.service;

import org.springframework.security.core.userdetails.UserDetailsService;

public interface UserService extends UserDetailsService {

}
package ***.alatus.service;

import org.springframework.security.core.userdetails.UserDetailsService;

public interface UserService extends UserDetailsService {

}
package ***.alatus.service.impl;

import ***.alatus.mapper.TUserMapper;
import ***.alatus.model.TUser;
import jakarta.annotation.Resource;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements ***.alatus.service.UserService {
    @Resource
    private TUserMapper tUserMapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        TUser tUser = tUserMapper.selectByLoginAct(username);
        if(tUser == null){
            throw new UsernameNotFoundException("登陆账号不存在");
        }
        return tUser;
    }
}
package ***.alatus.service.impl;

import ***.alatus.mapper.TUserMapper;
import ***.alatus.model.TUser;
import jakarta.annotation.Resource;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements ***.alatus.service.UserService {
    @Resource
    private TUserMapper tUserMapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        TUser tUser = tUserMapper.selectByLoginAct(username);
        if(tUser == null){
            throw new UsernameNotFoundException("登陆账号不存在");
        }
        return tUser;
    }
}
package ***.alatus.util;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.util.ObjectUtils;

import java.io.IOException;
import java.io.PrintWriter;

public class ResponseUtils {

    public static void write(HttpServletResponse response, String resultJSON) {
        response.setContentType("application/json;charset=utf-8");
        PrintWriter writer = null;
        try {
            writer = response.getWriter();

            writer.write(resultJSON);

            writer.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            //spring提供的一个对象工具类
            if (!ObjectUtils.isEmpty(writer)) {
                writer.close();
            }
        }
    }
}
package ***.alatus.util;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.util.ObjectUtils;

import java.io.IOException;
import java.io.PrintWriter;

public class ResponseUtils {

    public static void write(HttpServletResponse response, String resultJSON) {
        response.setContentType("application/json;charset=utf-8");
        PrintWriter writer = null;
        try {
            writer = response.getWriter();

            writer.write(resultJSON);

            writer.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            //spring提供的一个对象工具类
            if (!ObjectUtils.isEmpty(writer)) {
                writer.close();
            }
        }
    }
}
package ***.alatus.util;

import ***.fasterxml.jackson.core.JsonProcessingException;
import ***.fasterxml.jackson.databind.ObjectMapper;

public class JSONUtils {

    //对象映射工具类, 可以实现 java对象 <----> json对象 的相互转化
    private static final ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 把Java对象转json
     *
     * @param object
     * @return
     */
    public static String toJSON(Object object) {
        try {
            return objectMapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 把json转Java对象
     *
     * @param json
     * @param clazz
     * @return
     * @param <T>
     */
    public static <T> T toBean(String json, Class<T> clazz) {
        try {
            return objectMapper.readValue(json, clazz);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}
package ***.alatus.util;

import ***.fasterxml.jackson.core.JsonProcessingException;
import ***.fasterxml.jackson.databind.ObjectMapper;

public class JSONUtils {

    //对象映射工具类, 可以实现 java对象 <----> json对象 的相互转化
    private static final ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 把Java对象转json
     *
     * @param object
     * @return
     */
    public static String toJSON(Object object) {
        try {
            return objectMapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 把json转Java对象
     *
     * @param json
     * @param clazz
     * @return
     * @param <T>
     */
    public static <T> T toBean(String json, Class<T> clazz) {
        try {
            return objectMapper.readValue(json, clazz);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}
转载请说明出处内容投诉
CSS教程_站长资源网 » CRM项目基于Spring-Security处理登录请求和权限验证并工具类封装R对象回传前端------CRM项目

发表评论

欢迎 访客 发表评论

一个令你着迷的主题!

查看演示 官网购买