sell
提示:
官方文档:微信开放平台open.weixin.qq.com
必须具有公司资质的账号才能用微信登陆App或者网站
开放平台的appId和秘钥和公众平台的不一样,用的接口一样WxMpService
redis集群实现分布式session共享:
登录流程:
1.配置文件中配置openAppId和openAppSecret
2.配置类中设置配置:WxMpService
@Component
public class WechatMpConfig {
@Autowired
private WechatAccountConfig accountConfig;
@Bean
public WxMpService wxMpService() {
WxMpService wxMpService = new WxMpServiceImpl();
wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
return wxMpService;
}
@Bean
public WxMpConfigStorage wxMpConfigStorage() {
WxMpInMemoryConfigStorage wxMpConfigStorage = new WxMpInMemoryConfigStorage();
wxMpConfigStorage.setAppId(accountConfig.getMpAppId());
wxMpConfigStorage.setSecret(accountConfig.getMpAppSecret());
return wxMpConfigStorage;
}
}
3.获取code,通过code获取openid,(见代码)
4.openid去和数据库的数据匹配
5.设置token至redis(需引入依赖,配置redis的host和port)
6.设置token至cookie
7.登陆成功,跳转(redirect),跳转的时候一般用完整地址
登出流程:
1.清除cookie
2.清除redis(过期时间设为0)
AOP实现身份验证:
用户访问url之前验证一下用户是否登录,如果未登录则抛出异常,然后拦截该异常
@Controller
@RequestMapping("/wechat")
@Slf4j
public class WechatController {
@Autowired