feat:添加微信认证登陆

This commit is contained in:
haoxr 2020-10-08 23:42:55 +08:00
parent 3122e21a17
commit 51ae6be207
24 changed files with 63 additions and 223 deletions

View File

@ -13,8 +13,8 @@
<packaging>pom</packaging>
<modules>
<module>ylmall-oms-api</module>
<module>ylmall-oms-biz</module>
<module>mall-oms-api</module>
<module>mall-oms-biz</module>
</modules>
<properties>

View File

@ -13,8 +13,8 @@
<packaging>pom</packaging>
<modules>
<module>ylmall-pms-api</module>
<module>ylmall-pms-biz</module>
<module>mall-pms-api</module>
<module>mall-pms-biz</module>
</modules>
<properties>

View File

@ -13,8 +13,8 @@
<packaging>pom</packaging>
<modules>
<module>ylmall-sms-api</module>
<module>ylmall-sms-biz</module>
<module>mall-sms-api</module>
<module>mall-sms-biz</module>
</modules>
<properties>

View File

@ -13,10 +13,10 @@
<module>youlai-common</module>
<module>youlai-admin</module>
<module>youlai-auth</module>
<module>ylmall-sms</module>
<module>ylmall-ums</module>
<module>ylmall-pms</module>
<module>ylmall-oms</module>
<module>mall-sms</module>
<module>mall-ums</module>
<module>mall-pms</module>
<module>mall-oms</module>
</modules>
<parent>

View File

@ -1,73 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>ylmall-sms</artifactId>
<groupId>com.youlai</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>ylmall-sms-biz</artifactId>
<dependencies>
<dependency>
<groupId>com.youlai</groupId>
<artifactId>ylmall-sms-api</artifactId>
<version>${youlai-common.version}</version>
</dependency>
<dependency>
<groupId>com.youlai</groupId>
<artifactId>youlai-common-db</artifactId>
<version>${youlai-common.version}</version>
</dependency>
<dependency>
<groupId>com.youlai</groupId>
<artifactId>youlai-common-knife4j</artifactId>
<version>${youlai-common.version}</version>
</dependency>
<dependency>
<groupId>com.youlai</groupId>
<artifactId>youlai-common-web</artifactId>
<version>${youlai-common.version}</version>
</dependency>
<dependency>
<groupId>com.youlai</groupId>
<artifactId>youlai-common-redis</artifactId>
<version>${youlai-common.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- nacos 依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -1,9 +0,0 @@
package com.ylmall.sms;
@SpringBootApplication
@EnableDiscoveryClient
@MapperScan("com.youlai.admin.mapper")
@EnableSwagger2
public class SmsApplication {
}

View File

@ -1,12 +0,0 @@
server:
port: 8080
spring:
application:
name: youlai-admin
cloud:
nacos:
discovery:
server-addr: http://localhost:8848
config:
file-extension: yaml

View File

@ -1,73 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>ylmall-ums</artifactId>
<groupId>com.youlai</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>ylmall-ums-biz</artifactId>
<dependencies>
<dependency>
<groupId>com.youlai</groupId>
<artifactId>ylmall-sms-api</artifactId>
<version>${youlai-common.version}</version>
</dependency>
<dependency>
<groupId>com.youlai</groupId>
<artifactId>youlai-common-db</artifactId>
<version>${youlai-common.version}</version>
</dependency>
<dependency>
<groupId>com.youlai</groupId>
<artifactId>youlai-common-knife4j</artifactId>
<version>${youlai-common.version}</version>
</dependency>
<dependency>
<groupId>com.youlai</groupId>
<artifactId>youlai-common-web</artifactId>
<version>${youlai-common.version}</version>
</dependency>
<dependency>
<groupId>com.youlai</groupId>
<artifactId>youlai-common-redis</artifactId>
<version>${youlai-common.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- nacos 依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -82,7 +82,6 @@
<artifactId>weixin-java-miniapp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>

View File

@ -19,7 +19,7 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
http
.authorizeRequests().requestMatchers(EndpointRequest.toAnyEndpoint()).permitAll()
.and()
.authorizeRequests().antMatchers("/rsa/publicKey","/oauth/login_by_wx").permitAll().anyRequest().authenticated()
.authorizeRequests().antMatchers("/rsa/publicKey","/wx/auth/login").permitAll().anyRequest().authenticated()
.and()
.csrf().disable();
}

View File

@ -3,33 +3,29 @@ package com.youlai.auth.config;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.WxMaConfig;
import org.springframework.beans.factory.annotation.Autowired;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import lombok.AllArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@AllArgsConstructor
public class WxConfig {
@Autowired
private WxProperties properties;
/*
@Bean
public cn.binarywang.wx.miniapp.config.WxMaConfig wxMaConfig() {
WxMaConfig config = new WxMaConfig();
config.setAppid(properties.getAppId());
config.setSecret(properties.getAppSecret());
public WxMaConfig wxMaConfig() {
WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
config.setAppid(properties.getAppid());
config.setSecret(properties.getSecret());
return config;
}
@Bean
public WxMaService wxMaService(cn.binarywang.wx.miniapp.config.WxMaConfig maConfig) {
public WxMaService wxMaService(WxMaConfig wxMaConfig) {
WxMaService service = new WxMaServiceImpl();
service.setWxMaConfig(maConfig);
service.setWxMaConfig(wxMaConfig);
return service;
}
*/
}

View File

@ -4,7 +4,6 @@ import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import java.util.List;
@Data
@Configuration

View File

@ -1,11 +1,8 @@
package com.youlai.auth.controller;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.youlai.auth.domain.Oauth2Token;
import com.youlai.auth.domain.WxLoginInfo;
import com.youlai.common.core.constant.AuthConstants;
import com.youlai.common.core.result.Result;
import com.youlai.common.core.result.ResultCode;
@ -14,7 +11,6 @@ import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.endpoint.TokenEndpoint;
@ -77,27 +73,4 @@ public class AuthController {
}
private WxMaService wxService;
@PostMapping("/login_by_wx")
public Result loginByWx(@RequestBody WxLoginInfo wxLoginInfo) {
String code = wxLoginInfo.getCode();
WxLoginInfo.UserInfo userInfo = wxLoginInfo.getUserInfo();
if (code == null || userInfo == null) {
return Result.error();
}
String sessionKey;
String openId;
try {
WxMaJscode2SessionResult result = this.wxService.getUserService().getSessionInfo(code);
sessionKey = result.getSessionKey();
openId = result.getOpenid();
} catch (WxErrorException e) {
e.printStackTrace();
}
return Result.success();
}
}

View File

@ -0,0 +1,37 @@
package com.youlai.auth.controller;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import com.youlai.auth.domain.WxLoginInfo;
import com.youlai.common.core.result.Result;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/wx/auth")
public class WxAuthController {
@Autowired
private WxMaService wxService;
@PostMapping("/login")
public Result loginByWx(@RequestBody WxLoginInfo wxLoginInfo) throws WxErrorException {
String code = wxLoginInfo.getCode();
if (code == null) {
return Result.error();
}
WxMaJscode2SessionResult session = wxService.getUserService().getSessionInfo(code);
if(session==null){
return Result.error();
}
String openid = session.getOpenid();
String secretKey = session.getSessionKey();
return Result.success();
}
}

View File

@ -7,6 +7,10 @@ public class WxLoginInfo {
private String code;
private String encryptedData;
private String iv;
private UserInfo userInfo;
@Data

View File

@ -14,6 +14,5 @@ spring:
prefix: ${spring.application.name}
wx:
miniapp:
configs:
appid: wx99a151dc43d2637b
secret: a09605af8ad29ca5d18ff31c19828f37
appid: wx99a151dc43d2637b
secret: a09605af8ad29ca5d18ff31c19828f37