feat(auth): 添加微信登录功能- 新增微信登录相关配置和接口
- 实现微信登录逻辑,包括获取 openid 和 session_key- 更新安全配置,允许微信登录请求通过 - 添加必要的依赖和数据传输对象
This commit is contained in:
parent
ff6c20fe00
commit
1e4d0abd3a
5
pom.xml
5
pom.xml
|
|
@ -118,6 +118,11 @@
|
|||
<artifactId>spring-security-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>fastjson</artifactId>
|
||||
<version>1.2.83</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<dependencyManagement>
|
||||
|
|
|
|||
|
|
@ -38,7 +38,8 @@ public class SecurityConfig {
|
|||
"/swagger-ui/**",
|
||||
"/v3/api-docs/**",
|
||||
"/api/public/**",
|
||||
"/api/admin/login"
|
||||
"/api/admin/login",
|
||||
"/api/client/wx/login"
|
||||
).permitAll()
|
||||
.antMatchers("/api/admin/**").authenticated()
|
||||
.antMatchers("/api/client/**").authenticated()
|
||||
|
|
|
|||
|
|
@ -0,0 +1,15 @@
|
|||
package com.example.admin_server.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "wx")
|
||||
@Data
|
||||
public class WxConfig {
|
||||
private String appid;
|
||||
private String secret;
|
||||
private String jscode2sessionUrl;
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,64 @@
|
|||
package com.example.admin_server.controller.client;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.example.admin_server.common.Result;
|
||||
import com.example.admin_server.config.WxConfig;
|
||||
import com.example.admin_server.model.dto.WxLoginDTO;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
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;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/client/wx")
|
||||
@RequiredArgsConstructor
|
||||
@Api(tags = {"微信登录"})
|
||||
public class WxLoginController {
|
||||
|
||||
private final WxConfig wxConfig;
|
||||
|
||||
@PostMapping("/login")
|
||||
@ApiOperation(value = "微信登录")
|
||||
public Result<?> wxLogin(@RequestBody WxLoginDTO request) {
|
||||
String code = request.getCode();
|
||||
if (code == null || code.isEmpty()) {
|
||||
return Result.fail("code 参数不能为空");
|
||||
}
|
||||
|
||||
// 构造请求微信的 URL
|
||||
String url = wxConfig.getJscode2sessionUrl()
|
||||
+ "?appid=" + wxConfig.getAppid()
|
||||
+ "&secret=" + wxConfig.getSecret()
|
||||
+ "&js_code=" + code
|
||||
+ "&grant_type=authorization_code";
|
||||
|
||||
// 请求微信服务器
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
String response = restTemplate.getForObject(url, String.class);
|
||||
|
||||
// 解析微信响应
|
||||
JSONObject jsonObject = JSONObject.parseObject(response);
|
||||
String openid = jsonObject.getString("openid");
|
||||
String sessionKey = jsonObject.getString("session_key");
|
||||
|
||||
if (openid == null) {
|
||||
String errMsg = jsonObject.getString("errmsg");
|
||||
return Result.fail("微信登录失败: " + errMsg);
|
||||
}
|
||||
|
||||
// TODO: 你可以在这里用 openid 查数据库,创建或更新用户,生成 JWT 等
|
||||
|
||||
Map<String, Object> resultData = new HashMap<>();
|
||||
resultData.put("openid", openid);
|
||||
resultData.put("token", "mock-token-" + openid); // 后期你可以用 JWT 替换这里
|
||||
|
||||
return Result.ok("登录成功", resultData);
|
||||
}
|
||||
}
|
||||
|
|
@ -29,6 +29,7 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter {
|
|||
WHITELIST.add("/api/admin/login");
|
||||
WHITELIST.add("/api/client/login");
|
||||
WHITELIST.add("/api/employee/login");
|
||||
WHITELIST.add("/api/client/wx/login");
|
||||
// 也可以放其它公开接口
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,12 @@
|
|||
package com.example.admin_server.model.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
|
||||
@Data
|
||||
public class WxLoginDTO {
|
||||
|
||||
@NotBlank
|
||||
private String code;
|
||||
}
|
||||
|
|
@ -21,27 +21,11 @@ app:
|
|||
jwt:
|
||||
secret: 's9TfkPeAKJOlDw4ox3r6VhAMG7KfI0RK'
|
||||
expiration: 86400000
|
||||
|
||||
springdoc:
|
||||
swagger-ui:
|
||||
path: /swagger-ui.html
|
||||
tags-sorter: alpha
|
||||
operations-sorter: alpha
|
||||
api-docs:
|
||||
path: /v3/api-docs
|
||||
group-configs:
|
||||
- group: '管理端'
|
||||
paths-to-match: '/api/admin/**'
|
||||
packages-to-scan: com.example.admin_server.controller.admin
|
||||
- group: '客户端'
|
||||
paths-to-match: '/api/client/**'
|
||||
packages-to-scan: com.example.admin_server.controller.client
|
||||
- group: '员工端'
|
||||
paths-to-match: '/api/employee/**'
|
||||
packages-to-scan: com.example.admin_server.controller.employee
|
||||
wx:
|
||||
appid: wxf204145820676bb2
|
||||
secret: 642108612e162db4edb9cf01d7f6d474
|
||||
jscode2session-url: https://api.weixin.qq.com/sns/jscode2session
|
||||
|
||||
knife4j:
|
||||
enable: true
|
||||
setting:
|
||||
language: zh_cn
|
||||
|
||||
|
|
|
|||
|
|
@ -21,27 +21,11 @@ app:
|
|||
jwt:
|
||||
secret: '2n9g8b5TfsYLBZGFwE5ImpeQH5u0djEl'
|
||||
expiration: 86400000
|
||||
|
||||
springdoc:
|
||||
swagger-ui:
|
||||
path: /swagger-ui.html
|
||||
tags-sorter: alpha
|
||||
operations-sorter: alpha
|
||||
api-docs:
|
||||
path: /v3/api-docs
|
||||
group-configs:
|
||||
- group: '管理端'
|
||||
paths-to-match: '/api/admin/**'
|
||||
packages-to-scan: com.example.admin_server.controller.admin
|
||||
- group: '客户端'
|
||||
paths-to-match: '/api/client/**'
|
||||
packages-to-scan: com.example.admin_server.controller.client
|
||||
- group: '员工端'
|
||||
paths-to-match: '/api/employee/**'
|
||||
packages-to-scan: com.example.admin_server.controller.employee
|
||||
wx:
|
||||
appid: your_app_id
|
||||
secret: your_app_secret
|
||||
jscode2session-url: https://api.weixin.qq.com/sns/jscode2session
|
||||
|
||||
knife4j:
|
||||
enable: true
|
||||
setting:
|
||||
language: zh_cn
|
||||
|
||||
|
|
|
|||
|
|
@ -21,27 +21,11 @@ app:
|
|||
jwt:
|
||||
secret: 'ylZS9cQ40nBvcZxJKu9zCIDt096BpZbm'
|
||||
expiration: 86400000
|
||||
|
||||
springdoc:
|
||||
swagger-ui:
|
||||
path: /swagger-ui.html
|
||||
tags-sorter: alpha
|
||||
operations-sorter: alpha
|
||||
api-docs:
|
||||
path: /v3/api-docs
|
||||
group-configs:
|
||||
- group: '管理端'
|
||||
paths-to-match: '/api/admin/**'
|
||||
packages-to-scan: com.example.admin_server.controller.admin
|
||||
- group: '客户端'
|
||||
paths-to-match: '/api/client/**'
|
||||
packages-to-scan: com.example.admin_server.controller.client
|
||||
- group: '员工端'
|
||||
paths-to-match: '/api/employee/**'
|
||||
packages-to-scan: com.example.admin_server.controller.employee
|
||||
wx:
|
||||
appid: your_app_id
|
||||
secret: your_app_secret
|
||||
jscode2session-url: https://api.weixin.qq.com/sns/jscode2session
|
||||
|
||||
knife4j:
|
||||
enable: true
|
||||
setting:
|
||||
language: zh_cn
|
||||
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ spring:
|
|||
time-zone: GMT+8
|
||||
# 指定当前激活环境
|
||||
profiles:
|
||||
active: test
|
||||
active: dev
|
||||
datasource:
|
||||
url: jdbc:mysql://localhost:3306/admin_server?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai
|
||||
username: admin_server
|
||||
|
|
@ -24,28 +24,12 @@ app:
|
|||
jwt:
|
||||
secret: 'mwsK9Ol9Ni2IyTvcdgFDVBxatw8QWx2o'
|
||||
expiration: 86400000
|
||||
wx:
|
||||
appid: your_app_id
|
||||
secret: your_app_secret
|
||||
jscode2session-url: https://api.weixin.qq.com/sns/jscode2session
|
||||
|
||||
|
||||
springdoc:
|
||||
swagger-ui:
|
||||
path: /swagger-ui.html
|
||||
tags-sorter: alpha
|
||||
operations-sorter: alpha
|
||||
api-docs:
|
||||
path: /v3/api-docs
|
||||
group-configs:
|
||||
- group: '管理端'
|
||||
paths-to-match: '/api/admin/**'
|
||||
packages-to-scan: com.example.admin_server.controller.admin
|
||||
- group: '客户端'
|
||||
paths-to-match: '/api/client/**'
|
||||
packages-to-scan: com.example.admin_server.controller.client
|
||||
- group: '员工端'
|
||||
paths-to-match: '/api/employee/**'
|
||||
packages-to-scan: com.example.admin_server.controller.employee
|
||||
|
||||
knife4j:
|
||||
enable: true
|
||||
setting:
|
||||
language: zh_cn
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue