spring boot 에서 쓴 위 챗 애플 릿 로그 인 인증

13377 단어 Demo자바
컨트롤 러 층:
package com.searchmarket.controller;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: SearchMarket
 * @description:       
 * @Author:    
 * @create: 2020/02/20 - 22:43
 **/
@RequestMapping("wx")
@RestController
public class WeiXinController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @PostMapping("login")
    public Map<String, Object> wxLogin(@RequestParam("code")String code){
        Map<String, Object> result = new HashMap<>();
        result.put("status",200);

        String appid = "wx111111111111111f0e6";
        String secret = "11111111111111111111111111111";
        String url =    "https://api.weixin.qq.com/sns/jscode2session?appid=" + appid +
                        "&secret="  + secret +
                        "&js_code=" + code   +
                        "&grant_type=authorization_code";
        String jsonData = this.restTemplate.getForObject(url,String.class);
        if (StringUtils.contains(jsonData,"errcode")){
            //    
            result.put("status",500);
            result.put("msg","    ");
            return  result;
        }
        String md5Key = DigestUtils.md5Hex(jsonData + "SM_WX_LOGIN");

        String redisKey = "SM_LOGIN_" + md5Key;

        //  :WX_LOGIN——
        this.redisTemplate.opsForValue().set(redisKey,jsonData, Duration.ofDays(30));
        result.put("ticket","SM_" + md5Key);

        return result;
    }
}

Config 기본 설정 클래스
package com.searchmarket.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

/**
 * @program: SearchMarket
 * @description: redis   
 * @Author:    
 * @create: 2020/02/20 - 22:54
 **/
@Configuration
public class SM_Config {
    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

좋은 웹페이지 즐겨찾기