Commit d66c6978 authored by 万成波's avatar 万成波

后台企微登录

parent a8a2a392
......@@ -2,7 +2,6 @@ package com.tangguo.web.controller.system;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.tangguo.common.annotation.Log;
import com.tangguo.common.constant.Constants;
import com.tangguo.common.core.domain.AjaxResult;
import com.tangguo.common.core.domain.entity.SysMenu;
......@@ -11,10 +10,8 @@ import com.tangguo.common.core.domain.entity.SysUser;
import com.tangguo.common.core.domain.model.LoginBody;
import com.tangguo.common.core.domain.model.LoginUser;
import com.tangguo.common.core.domain.model.WxcpCodeLogin;
import com.tangguo.common.enums.BusinessType;
import com.tangguo.common.exception.ServiceException;
import com.tangguo.common.utils.SecurityUtils;
import com.tangguo.common.utils.StringUtils;
import com.tangguo.framework.config.ServerConfig;
import com.tangguo.framework.web.service.SysLoginService;
import com.tangguo.framework.web.service.SysPermissionService;
......@@ -59,7 +56,10 @@ public class SysLoginController {
private TokenService tokenService;
@Autowired
private WxCpService wxCpService;
private WxCpService mobileWxCpService;
@Autowired
private WxCpService pcWxCpService;
@Autowired
private ServerConfig serverConfig;
......@@ -67,8 +67,12 @@ public class SysLoginController {
@Autowired
private ISysUserService userService;
@Value("${wx.cp.redirect-url}")
private String redirectUrl;
@Value("${wx.cp.pc-redirect-url}")
private String pcRedirectUrl;
@Value("${wx.cp.mobile-redirect-url}")
private String mobileRedirectUrl;
......@@ -88,6 +92,47 @@ public class SysLoginController {
return ajax;
}
/**
* 企微用户Code登录
*
* @param bo 登录参数
* @return 登录结果
*/
@PostMapping("/pc/code/login")
public AjaxResult pcCodeLogin(@RequestBody WxcpCodeLogin bo) {
LoginUser loginUser = SecurityUtils.getLoginUserNotEx();
log.info("=> 登录请求参数:{}", bo);
log.info("=> 当前登录用户:{}", loginUser);
// 企微登录认证链接
String authCode = bo.getCode();
WxCpOAuth2Service oauth2Service = this.pcWxCpService.getOauth2Service();
if (Objects.isNull(loginUser) && StrUtil.isBlank(authCode)) {
String oauth2Url = oauth2Service.buildAuthorizationUrl(this.pcRedirectUrl, null);
log.info("=> 认证失败,返回Oauth2登录链接:{}", oauth2Url);
return AjaxResult.error(401, "身份认证失败", oauth2Url);
}
// 查询企微用户信息
String token = null;
if (Objects.isNull(loginUser) && StrUtil.isNotBlank(authCode)) {
try {
WxCpOauth2UserInfo userInfo = oauth2Service.getUserInfo(authCode);
log.info("=> 查询企微用户信息,返回结果:{},{}", authCode, JSON.toJSONString(userInfo));
UserDetails userDetails = this.userDetailsServiceImpl.loadUserByUsername2(userInfo.getUserId());
token = this.tokenService.createToken((LoginUser) userDetails);
} catch (Exception e) {
log.error("=> 查询用户信息失败:", e);
throw new ServiceException("登录失败,查询用户信息失败。");
}
}
log.info("=> 登录结果:{}", token);
return AjaxResult.success("登录成功", token);
}
/**
* 获取用户信息
*
......@@ -129,16 +174,16 @@ public class SysLoginController {
* @return 登录结果
*/
@PostMapping("/bbs/mobile/user/code/login")
public AjaxResult codeLogin(@RequestBody WxcpCodeLogin bo) {
public AjaxResult mobileCodeLogin(@RequestBody WxcpCodeLogin bo) {
LoginUser loginUser = SecurityUtils.getLoginUserNotEx();
log.info("=> 登录请求参数:{}", bo);
log.info("=> 当前登录用户:{}", loginUser);
// 企微登录认证链接
String authCode = bo.getCode();
WxCpOAuth2Service oauth2Service = this.wxCpService.getOauth2Service();
WxCpOAuth2Service oauth2Service = this.mobileWxCpService.getOauth2Service();
if (Objects.isNull(loginUser) && StrUtil.isBlank(authCode)) {
String oauth2Url = oauth2Service.buildAuthorizationUrl(this.redirectUrl, null);
String oauth2Url = oauth2Service.buildAuthorizationUrl(this.mobileRedirectUrl, null);
log.info("=> 认证失败,返回Oauth2登录链接:{}", oauth2Url);
return AjaxResult.error(401, "身份认证失败", oauth2Url);
}
......
......@@ -95,8 +95,13 @@ spring:
# 企业微信配置
wx:
cp:
redirect-url: https://wecom.jift.edu.cn/bbsh5/pages/login/login
pc-redirect-url: https://wecom.jift.edu.cn/bbs/pc
mobile-redirect-url: https://wecom.jift.edu.cn/bbs/h5/pages/login/login
corp-id: wxd2a84aa7529d3801
app-config:
agent-id: 1000218
secret: UubIP6xbLBzw3DwcIyOARYf1e4cm5GNJKNFZTlVfgyo
app-configs:
- name: '移动端应用配置'
agent-id: 1000218
secret: UubIP6xbLBzw3DwcIyOARYf1e4cm5GNJKNFZTlVfgyo
- name: '管理端应用配置'
agent-id: 1000219
secret: OEMM3DAd-2FK-9Ggiu3xsD7Sg4SlPuV7os1hAWrTIwk
......@@ -119,14 +119,3 @@ xss:
excludes: /system/notice
# 匹配链接
urlPatterns: /system/*,/monitor/*,/tool/*
mobile:
auth:
res-token-name: token
req-token-name: Authorization
issuer: Mobile-Auth
algorithm-id: HS512
sign-key: SignKey2025@.
effective-time: 7d
path-patterns: /bbs/mobile/**
......@@ -23,13 +23,13 @@ public class ApplicationTest {
private JmsTemplate jmsTemplate;
@Resource
private WxCpService wxCpService;
private WxCpService mobileWxCpService;
@Test
public void test() {
WxCpOAuth2Service oauth2Service = this.wxCpService.getOauth2Service();
WxCpOAuth2Service oauth2Service = this.mobileWxCpService.getOauth2Service();
System.out.println(oauth2Service.buildAuthorizationUrl("https://test.tangguo.ren/bbs/h5/pages/login/login/", ""));
}
......
{
"name" : "社区",
"appid" : "__UNI__E3457E1",
"appid" : "__UNI__69A40B9",
"description" : "",
"versionName" : "1.0.0",
"versionCode" : "100",
......@@ -72,7 +72,7 @@
"h5" : {
"router" : {
"mode" : "history",
"base" : "/bbsh5/"
"base" : "/bbs/h5/"
}
}
}
......@@ -110,7 +110,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
// 过滤请求
.authorizeRequests()
// 对于登录login 注册register 验证码captchaImage 允许匿名访问
.antMatchers("/login", "/bbs/mobile/user/code/login", "/register", "/captchaImage").permitAll()
.antMatchers("/login", "/bbs/mobile/user/code/login", "/pc/code/login", "/register", "/captchaImage").permitAll()
// 静态资源,可匿名访问
.antMatchers(HttpMethod.GET, "/", "/*.html", "/**/*.html", "/**/*.css", "/**/*.js", "/profile/**").permitAll()
.antMatchers("/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs", "/druid/**").permitAll()
......
......@@ -33,10 +33,10 @@ public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint, S
private static final long serialVersionUID = -8970718410437077606L;
@Autowired
private WxCpService wxCpService;
private WxCpService mobileWxCpService;
@Value("${wx.cp.redirect-url}")
private String redirectUrl;
@Value("${wx.cp.mobile-redirect-url}")
private String mobileRedirectUrl;
@Override
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e)
......@@ -47,8 +47,8 @@ public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint, S
log.info("=> 身份认证失败,Token:{}", request.getHeader("Authorization"));
if (uri.startsWith("/bbs/mobile")) {
WxCpOAuth2Service oauth2Service = this.wxCpService.getOauth2Service();
String oauth2Url = oauth2Service.buildAuthorizationUrl(this.redirectUrl, null);
WxCpOAuth2Service oauth2Service = this.mobileWxCpService.getOauth2Service();
String oauth2Url = oauth2Service.buildAuthorizationUrl(this.mobileRedirectUrl, null);
ServletUtils.renderString(response, JSON.toJSONString(AjaxResult.error(401, "身份认证失败", oauth2Url)));
} else {
String msg = StringUtils.format("请求访问:{},认证失败,无法访问系统资源", uri);
......
......@@ -65,7 +65,7 @@ public class SysLoginService {
private UserDetailsServiceImpl userDetailsServiceImpl;
@Autowired
private WxCpService wxCpService;
private WxCpService mobileWxCpService;
/**
......
......@@ -9,6 +9,7 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.List;
import java.util.Objects;
......@@ -29,10 +30,11 @@ public class WxCpConfiguration {
/**
* 配置 WxCpService 实例
*/
@Bean
public WxCpService wxCpService() {
WxCpProperties.AppConfig appConfig = this.properties.getAppConfig();
WxCpDefaultConfigImpl configStorage = new WxCpDefaultConfigImpl();
@Bean("mobileWxCpService")
public WxCpService mobileWxCpService() {
List<WxCpProperties.AppConfig> appConfigs = this.properties.getAppConfigs();
WxCpProperties.AppConfig appConfig = appConfigs.get(0);
WxCpDefaultConfigImpl configStorage = new WxCpDefaultConfigImpl();
configStorage.setCorpId(this.properties.getCorpId());
configStorage.setAgentId(appConfig.getAgentId());
configStorage.setCorpSecret(appConfig.getSecret());
......@@ -43,4 +45,23 @@ public class WxCpConfiguration {
return service;
}
/**
* 配置 WxCpService 实例
*/
@Bean("pcWxCpService")
public WxCpService pcWxCpService() {
List<WxCpProperties.AppConfig> appConfigs = this.properties.getAppConfigs();
WxCpProperties.AppConfig appConfig = appConfigs.get(1);
WxCpDefaultConfigImpl configStorage = new WxCpDefaultConfigImpl();
configStorage.setCorpId(this.properties.getCorpId());
configStorage.setAgentId(appConfig.getAgentId());
configStorage.setCorpSecret(appConfig.getSecret());
configStorage.setToken(appConfig.getToken());
configStorage.setAesKey(appConfig.getAesKey());
WxCpService service = new WxCpServiceImpl();
service.setWxCpConfigStorage(configStorage);
return service;
}
}
......@@ -3,6 +3,8 @@ package com.tangguo.framework.wxcp;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.List;
/**
* 企业微信配置类
*
......@@ -21,12 +23,14 @@ public class WxCpProperties {
/**
* 多应用配置
*/
private AppConfig appConfig;
private List<AppConfig> appConfigs;
@Data
public static class AppConfig {
private String name;
/**
* 设置企业微信应用的AgentId
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment