序列化和反序列化、单点登入

博客介绍了序列化相关内容,需导入pom.xml。其实现原理为登录时将数据转为json格式的user信息,设置固定cookieName和uuid值及有效期,通过filter使cookie在最后一次访问后30分钟过期。跨域时根据cookie的uuid从redis获取序列化user对象,还提及xml、json转换单点登录。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

序列化:     

导入pom.xml

    <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>2.6.0</version>
    </dependency>

    <dependency>
      <groupId>org.codehaus.jackson</groupId>
      <artifactId>jackson-mapper-asl</artifactId>
      <version>1.9.12</version>
    </dependency>
//序列化、反序列化工具类
import org.codehaus.jackson.map.ObjectMapper;
public class JsonUtil {

    private static Logger log = LoggerFactory.getLogger(JsonUtil.class);
    private static ObjectMapper objectMapper = new ObjectMapper();

    static {
        //设置所有字段都全部序列化(NON_NULL显示非空长度数据属性、NON_DEFAULT显示不是默认值的部分数据、NON_EMPTY是NON_NULL去掉空串的值显示)
        objectMapper.setSerializationInclusion(JsonSerialize.Inclusion.ALWAYS);
        //取消date类型默认转换成timestamps,怕生成的时间戳是从1970年的秒,不方便
        objectMapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS,false);
        //设置日期格式yyyy-MM-dd HH:mm:ss
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
         //转json时javaBean对象属性都是空情况,置为false就不会抛出异常
        objectMapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS ,false);
        //反序列化存在json字符串的属性,javaBean属性没有,设置false是不转换该属性,设置为true如果后期json数据有其他数据,就不会抛出异常
        objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES ,false);
    }

    public static <T> String objToJson (T obj){
        if(obj == null){
            return null;
        }
        try {
            return obj instanceof String ? (String)obj : objectMapper.writeValueAsString(obj);
        } catch (IOException e) {
            log.error("parse object to json",e);
            return null;
        }
    }
    //格式化objec转换的json
    public static <T> String objToJsonPretty (T obj){
        if(obj == null){
            return null;
        }
        try {
            return obj instanceof String ? (String)obj : objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
        } catch (IOException e) {
            log.error("parse object to json",e);
            return null;
        }
    }
   //json 转 object
    public static <T> T jsonToObject(String json, TypeReference<T> type){
        if(StringUtils.isEmpty(json) || type ==null){
            return  null;
        }
        try {
            return (T)(type.getType().equals(String.class) ? json : objectMapper.readValue(json,type));
        } catch (IOException e) {
            log.error("string to object error",e);
            return null;
        }
    }
    //json 转 object
    public static <T> T jsonToObject(String json,Class<?> collectionClass,Class<?> ...element){
        JavaType javaType = objectMapper.getTypeFactory().constructParametricType(collectionClass, element);

        try {
            return objectMapper.readValue(json,javaType);
        } catch (IOException e) {
            log.error("string to object error",e);
            return null;
        }
    }
}
-------------------------------------------------------------------
public class CookieUtils {

    private static Logger log = LoggerFactory.getLogger(CookieUtils.class);
    private static final String doMain = ".xiaolijun.com";
    private static final String cookieName = ".xiaolijun.logging";
    public static final int expireTime = 60 * 30;

    //写cookie
    public static void writeLogInToken(HttpServletResponse httpResponse, String token) {

        Cookie cookie = new Cookie(CookieUtils.cookieName, token);
        cookie.setDomain(doMain);
        cookie.setPath("/");
        cookie.setMaxAge(60 * 60 * 24 * 7);
        log.info("wirte is cookieName:{} ,token:{}", token);
        httpResponse.addCookie(cookie);
    }

    //获取cookie数据
    public static String readLogInToken(HttpServletRequest httpServletRequest) {

        Cookie[] cookies = httpServletRequest.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (StringUtils.equals(cookie.getName(), cookieName)) {
                    return cookie.getValue();
                }
            }
        }
        return null;
    }

    //销毁cookie
    public static void delCookie(HttpServletRequest httpServletRequest) {

        Cookie[] cookies = httpServletRequest.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (StringUtils.equals(cookie.getName(), cookieName)) {
                    cookie.setDomain(CookieUtils.doMain);
                    cookie.setPath("/");
                    cookie.setMaxAge(0);
                }
            }
        }

    }
}

实现原理:
1、登入了就会把数据转成json的user信息,然后将cookie固定的cookieName ,值设置uuid字符串;并设置有效期,通过filter过滤器设置最后一次访问后30分钟过期。
2、然后跨域的话根据cookie获取的token票据也就是uuid获取redis中的序列化user对象。

xml、json转换单点登入

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值