修改互联网单点登录
This commit is contained in:
@@ -3,6 +3,7 @@ package com.ruoyi.web.controller.system;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import com.ruoyi.common.core.domain.entity.tymh.wwToken.WwTokenResult;
|
||||
import com.ruoyi.common.core.domain.entity.tymh.wwToken.WwUserLogin;
|
||||
import com.ruoyi.common.core.domain.model.RegisterBody;
|
||||
import com.ruoyi.common.utils.StringUtils;
|
||||
@@ -21,6 +22,8 @@ import com.ruoyi.framework.web.service.SysLoginService;
|
||||
import com.ruoyi.framework.web.service.SysPermissionService;
|
||||
import com.ruoyi.system.service.ISysMenuService;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* 登录验证
|
||||
*
|
||||
@@ -203,4 +206,59 @@ public class SysLoginController
|
||||
.put("token", token)
|
||||
.put("accessUrl", "");
|
||||
}
|
||||
|
||||
/**
|
||||
*互联网获取token
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/getWwTjmHlwToken")
|
||||
public AjaxResult getWwTjmHlwToken(HttpServletRequest request){
|
||||
String token = request.getHeader("Authorization");
|
||||
if (token != null && token.startsWith("Bearer ")) {
|
||||
token = token.substring(7);
|
||||
}
|
||||
|
||||
String cookieValue = null;
|
||||
javax.servlet.http.Cookie[] cookies = request.getCookies();
|
||||
if (cookies != null) {
|
||||
for (javax.servlet.http.Cookie cookie : cookies) {
|
||||
if ("portal_auth".equals(cookie.getName())) {
|
||||
cookieValue = cookie.getValue();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
boolean isTokenValid = validateToken(token);
|
||||
boolean isCookieValid = validateCookie(cookieValue);
|
||||
if (isTokenValid && isCookieValid) {
|
||||
WwTokenResult wwTokenResult=new WwTokenResult();
|
||||
wwTokenResult.setAccessToken(token);
|
||||
wwTokenResult.setSessionCookie(cookieValue);
|
||||
String localToken = oauthLoginHlwService.getWwTjmHlwToken(wwTokenResult);
|
||||
return AjaxResult.success("门户登录成功")
|
||||
.put("token", localToken)
|
||||
.put("accessUrl", "");
|
||||
} else {
|
||||
return AjaxResult.error("登录失效,请重新登录");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证token
|
||||
* @param token
|
||||
* @return
|
||||
*/
|
||||
private boolean validateToken(String token) {
|
||||
return token != null && !token.isEmpty() && token.matches("^[A-Za-z0-9_-]{36}$");
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证cookie
|
||||
* @param cookieValue
|
||||
* @return
|
||||
*/
|
||||
private boolean validateCookie(String cookieValue) {
|
||||
return cookieValue != null && !cookieValue.isEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -111,7 +111,8 @@ public class SecurityConfig
|
||||
.authorizeHttpRequests((requests) -> {
|
||||
permitAllUrl.getUrls().forEach(url -> requests.antMatchers(url).permitAll());
|
||||
// 对于登录login 注册register 验证码captchaImage 允许匿名访问
|
||||
requests.antMatchers("/login", "/register", "/captchaImage","/app/login","/websocket/**","/speech-recognition","/speech-synthesis","/cms/company/listPage","/cms/appUser/noTmlist","/getTjmhToken","/getWwTjmhToken").permitAll()
|
||||
requests.antMatchers("/login", "/register", "/captchaImage","/app/login","/websocket/**","/speech-recognition","/speech-synthesis",
|
||||
"/cms/company/listPage","/cms/appUser/noTmlist","/getTjmhToken","/getWwTjmhToken","/getWwTjmHlwToken").permitAll()
|
||||
// 静态资源,可匿名访问
|
||||
.antMatchers(HttpMethod.GET, "/", "/*.html", "/**/*.html", "/**/*.css", "/**/*.js", "/profile/**").permitAll()
|
||||
// 移动端公用查询,可匿名访问
|
||||
|
||||
@@ -217,4 +217,28 @@ public class OauthLoginHlwService {
|
||||
sysUser.setLoginDate(new Date());
|
||||
sysUserService.updateUserProfile(sysUser);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取互联网token
|
||||
* @param wwTokenResult
|
||||
* @return
|
||||
*/
|
||||
public String getWwTjmHlwToken(WwTokenResult wwTokenResult){
|
||||
try {
|
||||
//获取门户userInfo
|
||||
WwTyInfo portalUser = oauthClient.wwGetUserInfo(wwTokenResult);
|
||||
//匹配/创建本地用户
|
||||
String localUsername = getOrCreateLocalUser(portalUser);
|
||||
//执行原来的登录流程
|
||||
Authentication authentication = authenticateLocalUser(localUsername);
|
||||
AsyncManager.me().execute(AsyncFactory.recordLogininfor(localUsername, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
|
||||
LoginUser loginUser = (LoginUser) authentication.getPrincipal();
|
||||
|
||||
storePortalToken(loginUser.getUserId(), wwTokenResult.getAccessToken());
|
||||
recordLoginInfo(loginUser.getUserId());
|
||||
return tokenService.createToken(loginUser);
|
||||
} catch (Exception e) {
|
||||
throw new ServiceException("OAuth 登录失败:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user