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

后台企微登录

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