Merge pull request #6243 from EightMonth/springboot3_sas

sas兼容shiro处理
pull/6344/head v3.6.3_springboot3sas
JEECG 2024-06-20 16:09:29 +08:00 committed by GitHub
commit e616c5d8fe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 138 additions and 69 deletions

View File

@ -12,6 +12,7 @@ import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.AuthenticationException; import org.springframework.security.core.AuthenticationException;
import org.springframework.web.HttpRequestMethodNotSupportedException; import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MaxUploadSizeExceededException; import org.springframework.web.multipart.MaxUploadSizeExceededException;
@ -32,13 +33,14 @@ public class JeecgBootExceptionHandler {
*/ */
@ExceptionHandler(JeecgCaptchaException.class) @ExceptionHandler(JeecgCaptchaException.class)
@ResponseStatus(HttpStatus.OK)
public Result<?> handleJeecgCaptchaException(JeecgCaptchaException e) { public Result<?> handleJeecgCaptchaException(JeecgCaptchaException e) {
log.error(e.getMessage(), e); log.error(e.getMessage(), e);
return Result.error(e.getCode(), e.getMessage()); return Result.error(e.getCode(), e.getMessage());
} }
@ExceptionHandler(AuthenticationException.class) @ExceptionHandler(AuthenticationException.class)
@ResponseStatus(HttpStatus.UNAUTHORIZED) @ResponseStatus(HttpStatus.OK)
public Result<?> handleJeecgCaptchaException(AuthenticationException e) { public Result<?> handleJeecgCaptchaException(AuthenticationException e) {
log.error(e.getMessage(), e); log.error(e.getMessage(), e);
return Result.error(401, e.getMessage()); return Result.error(401, e.getMessage());

View File

@ -244,7 +244,7 @@ public class SecurityConfig {
*/ */
@Bean @Bean
public AuthorizationServerSettings authorizationServerSettings() { public AuthorizationServerSettings authorizationServerSettings() {
return AuthorizationServerSettings.builder().build(); return AuthorizationServerSettings.builder().tokenEndpoint("/sys/login").build();
} }
/** /**

View File

@ -38,6 +38,7 @@ import org.springframework.util.Assert;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import java.security.Principal; import java.security.Principal;
import java.time.Instant;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -89,13 +90,20 @@ public class AppGrantAuthenticationProvider implements AuthenticationProvider {
String captcha = (String) additionalParameter.get("captcha"); String captcha = (String) additionalParameter.get("captcha");
String checkKey = (String) additionalParameter.get("checkKey"); String checkKey = (String) additionalParameter.get("checkKey");
OAuth2ClientAuthenticationToken clientPrincipal = getAuthenticatedClientElseThrowInvalidClient(appGrantAuthenticationToken);
RegisteredClient registeredClient = clientPrincipal.getRegisteredClient();
// 检查登录失败次数 // 检查登录失败次数
if(isLoginFailOvertimes(username)){ if(isLoginFailOvertimes(username)){
throw new JeecgBootException("该用户登录失败次数过多请于10分钟后再次登录"); Map<String, Object> map = new HashMap<>();
map.put("message", "该用户登录失败次数过多请于10分钟后再次登录");
return new OAuth2AccessTokenAuthenticationToken(registeredClient, clientPrincipal, new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER,"fdsafas", Instant.now(), Instant.now().plusNanos(1)), null, map);
} }
if(captcha==null){ if(captcha==null){
throw new JeecgBootException("验证码无效"); Map<String, Object> map = new HashMap<>();
map.put("message", "验证码无效");
return new OAuth2AccessTokenAuthenticationToken(registeredClient, clientPrincipal, new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER,"fdsafas", Instant.now(), Instant.now().plusNanos(1)), null, map);
} }
String lowerCaseCaptcha = captcha.toLowerCase(); String lowerCaseCaptcha = captcha.toLowerCase();
// 加入密钥作为混淆,避免简单的拼接,被外部利用,用户自定义该密钥即可 // 加入密钥作为混淆,避免简单的拼接,被外部利用,用户自定义该密钥即可
@ -104,16 +112,15 @@ public class AppGrantAuthenticationProvider implements AuthenticationProvider {
Object checkCode = redisUtil.get(realKey); Object checkCode = redisUtil.get(realKey);
//当进入登录页时,有一定几率出现验证码错误 #1714 //当进入登录页时,有一定几率出现验证码错误 #1714
if(checkCode==null || !checkCode.toString().equals(lowerCaseCaptcha)) { if(checkCode==null || !checkCode.toString().equals(lowerCaseCaptcha)) {
log.warn("验证码错误key= {} , Ui checkCode= {}, Redis checkCode = {}", checkKey, lowerCaseCaptcha, checkCode); Map<String, Object> map = new HashMap<>();
// 改成特殊的code 便于前端判断 map.put("message", "验证码错误");
throw new JeecgCaptchaException(HttpStatus.PRECONDITION_FAILED.value(), "验证码错误"); return new OAuth2AccessTokenAuthenticationToken(registeredClient, clientPrincipal, new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER,"fdsafas", Instant.now(), Instant.now().plusNanos(1)), null, map);
} }
OAuth2ClientAuthenticationToken clientPrincipal = getAuthenticatedClientElseThrowInvalidClient(appGrantAuthenticationToken);
RegisteredClient registeredClient = clientPrincipal.getRegisteredClient();
if (!registeredClient.getAuthorizationGrantTypes().contains(authorizationGrantType)) { if (!registeredClient.getAuthorizationGrantTypes().contains(authorizationGrantType)) {
throw new JeecgBootException("非法登录"); Map<String, Object> map = new HashMap<>();
map.put("message", "非法登录");
return new OAuth2AccessTokenAuthenticationToken(registeredClient, clientPrincipal, new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER,"fdsafas", Instant.now(), Instant.now().plusNanos(1)), null, map);
} }
// 通过用户名获取用户信息 // 通过用户名获取用户信息
@ -131,7 +138,9 @@ public class AppGrantAuthenticationProvider implements AuthenticationProvider {
password = PasswordUtil.encrypt(username, password, loginUser.getSalt()); password = PasswordUtil.encrypt(username, password, loginUser.getSalt());
if (!password.equals(loginUser.getPassword())) { if (!password.equals(loginUser.getPassword())) {
addLoginFailOvertimes(username); addLoginFailOvertimes(username);
throw new JeecgBootException("用户名或密码不正确"); Map<String, Object> map = new HashMap<>();
map.put("message", "用户名或密码不正确");
return new OAuth2AccessTokenAuthenticationToken(registeredClient, clientPrincipal, new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER,"fdsafas", Instant.now(), Instant.now().plusNanos(1)), null, map);
} }
//由于在上面已验证过用户名、密码现在构建一个已认证的对象UsernamePasswordAuthenticationToken //由于在上面已验证过用户名、密码现在构建一个已认证的对象UsernamePasswordAuthenticationToken
@ -156,9 +165,9 @@ public class AppGrantAuthenticationProvider implements AuthenticationProvider {
OAuth2TokenContext tokenContext = tokenContextBuilder.tokenType(OAuth2TokenType.ACCESS_TOKEN).build(); OAuth2TokenContext tokenContext = tokenContextBuilder.tokenType(OAuth2TokenType.ACCESS_TOKEN).build();
OAuth2Token generatedAccessToken = this.tokenGenerator.generate(tokenContext); OAuth2Token generatedAccessToken = this.tokenGenerator.generate(tokenContext);
if (generatedAccessToken == null) { if (generatedAccessToken == null) {
OAuth2Error error = new OAuth2Error(OAuth2ErrorCodes.SERVER_ERROR, Map<String, Object> map = new HashMap<>();
"无法生成访问token请联系管理系。", ERROR_URI); map.put("message", "无法生成访问token请联系管理系。");
throw new OAuth2AuthenticationException(error); return new OAuth2AccessTokenAuthenticationToken(registeredClient, clientPrincipal, new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER,"fdsafas", Instant.now(), Instant.now().plusNanos(1)), null, map);
} }
OAuth2AccessToken accessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER, OAuth2AccessToken accessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER,
generatedAccessToken.getTokenValue(), generatedAccessToken.getIssuedAt(), generatedAccessToken.getTokenValue(), generatedAccessToken.getIssuedAt(),
@ -180,9 +189,9 @@ public class AppGrantAuthenticationProvider implements AuthenticationProvider {
tokenContext = tokenContextBuilder.tokenType(OAuth2TokenType.REFRESH_TOKEN).build(); tokenContext = tokenContextBuilder.tokenType(OAuth2TokenType.REFRESH_TOKEN).build();
OAuth2Token generatedRefreshToken = this.tokenGenerator.generate(tokenContext); OAuth2Token generatedRefreshToken = this.tokenGenerator.generate(tokenContext);
if (!(generatedRefreshToken instanceof OAuth2RefreshToken)) { if (!(generatedRefreshToken instanceof OAuth2RefreshToken)) {
OAuth2Error error = new OAuth2Error(OAuth2ErrorCodes.SERVER_ERROR, Map<String, Object> map = new HashMap<>();
"无法生成刷新token请联系管理员。", ERROR_URI); map.put("message", "无法生成刷新token请联系管理员。");
throw new OAuth2AuthenticationException(error); return new OAuth2AccessTokenAuthenticationToken(registeredClient, clientPrincipal, new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER,"fdsafas", Instant.now(), Instant.now().plusNanos(1)), null, map);
} }
refreshToken = (OAuth2RefreshToken) generatedRefreshToken; refreshToken = (OAuth2RefreshToken) generatedRefreshToken;
@ -200,7 +209,7 @@ public class AppGrantAuthenticationProvider implements AuthenticationProvider {
baseCommonService.addLog("用户名: " + username + ",登录成功!", CommonConstant.LOG_TYPE_1, null,loginUser); baseCommonService.addLog("用户名: " + username + ",登录成功!", CommonConstant.LOG_TYPE_1, null,loginUser);
JSONObject addition = new JSONObject(new LinkedHashMap<>()); JSONObject addition = new JSONObject(new LinkedHashMap<>());
addition.put("token", accessToken.getTokenValue());
// 设置租户 // 设置租户
JSONObject jsonObject = commonAPI.setLoginTenant(username); JSONObject jsonObject = commonAPI.setLoginTenant(username);
addition.putAll(jsonObject.getInnerMap()); addition.putAll(jsonObject.getInnerMap());
@ -224,8 +233,15 @@ public class AppGrantAuthenticationProvider implements AuthenticationProvider {
addition.put("multi_depart", 2); addition.put("multi_depart", 2);
} }
// 兼容原有shiro登录结果处理
Map<String, Object> map = new HashMap<>();
map.put("result", addition);
map.put("code", 200);
map.put("success", true);
map.put("timestamp", System.currentTimeMillis());
// 返回access_token、refresh_token以及其它信息给到前端 // 返回access_token、refresh_token以及其它信息给到前端
return new OAuth2AccessTokenAuthenticationToken(registeredClient, clientPrincipal, accessToken, refreshToken, addition); return new OAuth2AccessTokenAuthenticationToken(registeredClient, clientPrincipal, accessToken, refreshToken, map);
} }
@Override @Override

View File

@ -39,6 +39,7 @@ import org.springframework.util.Assert;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import java.security.Principal; import java.security.Principal;
import java.time.Instant;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -90,6 +91,9 @@ public class PasswordGrantAuthenticationProvider implements AuthenticationProvid
String captcha = (String) additionalParameter.get("captcha"); String captcha = (String) additionalParameter.get("captcha");
String checkKey = (String) additionalParameter.get("checkKey"); String checkKey = (String) additionalParameter.get("checkKey");
OAuth2ClientAuthenticationToken clientPrincipal = getAuthenticatedClientElseThrowInvalidClient(passwordGrantAuthenticationToken);
RegisteredClient registeredClient = clientPrincipal.getRegisteredClient();
// 检查登录失败次数 // 检查登录失败次数
if(isLoginFailOvertimes(username)){ if(isLoginFailOvertimes(username)){
throw new JeecgBootException("该用户登录失败次数过多请于10分钟后再次登录"); throw new JeecgBootException("该用户登录失败次数过多请于10分钟后再次登录");
@ -105,16 +109,15 @@ public class PasswordGrantAuthenticationProvider implements AuthenticationProvid
Object checkCode = redisUtil.get(realKey); Object checkCode = redisUtil.get(realKey);
//当进入登录页时,有一定几率出现验证码错误 #1714 //当进入登录页时,有一定几率出现验证码错误 #1714
if(checkCode==null || !checkCode.toString().equals(lowerCaseCaptcha)) { if(checkCode==null || !checkCode.toString().equals(lowerCaseCaptcha)) {
log.warn("验证码错误key= {} , Ui checkCode= {}, Redis checkCode = {}", checkKey, lowerCaseCaptcha, checkCode); Map<String, Object> map = new HashMap<>();
// 改成特殊的code 便于前端判断 map.put("message", "验证码错误");
throw new JeecgCaptchaException(HttpStatus.PRECONDITION_FAILED.value(), "验证码错误"); return new OAuth2AccessTokenAuthenticationToken(registeredClient, clientPrincipal, new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER,"fdsafas", Instant.now(), Instant.now().plusNanos(1)), null, map);
} }
OAuth2ClientAuthenticationToken clientPrincipal = getAuthenticatedClientElseThrowInvalidClient(passwordGrantAuthenticationToken);
RegisteredClient registeredClient = clientPrincipal.getRegisteredClient();
if (!registeredClient.getAuthorizationGrantTypes().contains(authorizationGrantType)) { if (!registeredClient.getAuthorizationGrantTypes().contains(authorizationGrantType)) {
throw new JeecgBootException("非法登录"); Map<String, Object> map = new HashMap<>();
map.put("message", "非法登录");
return new OAuth2AccessTokenAuthenticationToken(registeredClient, clientPrincipal, new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER,"fdsafas", Instant.now(), Instant.now().plusNanos(1)), null, map);
} }
// 通过用户名获取用户信息 // 通过用户名获取用户信息
@ -132,7 +135,10 @@ public class PasswordGrantAuthenticationProvider implements AuthenticationProvid
password = PasswordUtil.encrypt(username, password, loginUser.getSalt()); password = PasswordUtil.encrypt(username, password, loginUser.getSalt());
if (!password.equals(loginUser.getPassword())) { if (!password.equals(loginUser.getPassword())) {
addLoginFailOvertimes(username); addLoginFailOvertimes(username);
throw new JeecgBootException("用户名或密码不正确");
Map<String, Object> map = new HashMap<>();
map.put("message", "用户名或密码不正确");
return new OAuth2AccessTokenAuthenticationToken(registeredClient, clientPrincipal, new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER,"fdsafas", Instant.now(), Instant.now().plusNanos(1)), null, map);
} }
//由于在上面已验证过用户名、密码现在构建一个已认证的对象UsernamePasswordAuthenticationToken //由于在上面已验证过用户名、密码现在构建一个已认证的对象UsernamePasswordAuthenticationToken
@ -157,9 +163,9 @@ public class PasswordGrantAuthenticationProvider implements AuthenticationProvid
OAuth2TokenContext tokenContext = tokenContextBuilder.tokenType(OAuth2TokenType.ACCESS_TOKEN).build(); OAuth2TokenContext tokenContext = tokenContextBuilder.tokenType(OAuth2TokenType.ACCESS_TOKEN).build();
OAuth2Token generatedAccessToken = this.tokenGenerator.generate(tokenContext); OAuth2Token generatedAccessToken = this.tokenGenerator.generate(tokenContext);
if (generatedAccessToken == null) { if (generatedAccessToken == null) {
OAuth2Error error = new OAuth2Error(OAuth2ErrorCodes.SERVER_ERROR, Map<String, Object> map = new HashMap<>();
"无法生成访问token请联系管理系。", ERROR_URI); map.put("message", "无法生成访问token请联系管理系。");
throw new OAuth2AuthenticationException(error); return new OAuth2AccessTokenAuthenticationToken(registeredClient, clientPrincipal, new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER,"fdsafas", Instant.now(), Instant.now().plusNanos(1)), null, map);
} }
OAuth2AccessToken accessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER, OAuth2AccessToken accessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER,
generatedAccessToken.getTokenValue(), generatedAccessToken.getIssuedAt(), generatedAccessToken.getTokenValue(), generatedAccessToken.getIssuedAt(),
@ -181,9 +187,9 @@ public class PasswordGrantAuthenticationProvider implements AuthenticationProvid
tokenContext = tokenContextBuilder.tokenType(OAuth2TokenType.REFRESH_TOKEN).build(); tokenContext = tokenContextBuilder.tokenType(OAuth2TokenType.REFRESH_TOKEN).build();
OAuth2Token generatedRefreshToken = this.tokenGenerator.generate(tokenContext); OAuth2Token generatedRefreshToken = this.tokenGenerator.generate(tokenContext);
if (!(generatedRefreshToken instanceof OAuth2RefreshToken)) { if (!(generatedRefreshToken instanceof OAuth2RefreshToken)) {
OAuth2Error error = new OAuth2Error(OAuth2ErrorCodes.SERVER_ERROR, Map<String, Object> map = new HashMap<>();
"无法生成刷新token请联系管理员。", ERROR_URI); map.put("message", "无法生成访问token请联系管理系。");
throw new OAuth2AuthenticationException(error); return new OAuth2AccessTokenAuthenticationToken(registeredClient, clientPrincipal, new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER,"fdsafas", Instant.now(), Instant.now().plusNanos(1)), null, map);
} }
refreshToken = (OAuth2RefreshToken) generatedRefreshToken; refreshToken = (OAuth2RefreshToken) generatedRefreshToken;
@ -201,6 +207,7 @@ public class PasswordGrantAuthenticationProvider implements AuthenticationProvid
baseCommonService.addLog("用户名: " + username + ",登录成功!", CommonConstant.LOG_TYPE_1, null,loginUser); baseCommonService.addLog("用户名: " + username + ",登录成功!", CommonConstant.LOG_TYPE_1, null,loginUser);
JSONObject addition = new JSONObject(new LinkedHashMap<>()); JSONObject addition = new JSONObject(new LinkedHashMap<>());
addition.put("token", accessToken.getTokenValue());
// 设置租户 // 设置租户
JSONObject jsonObject = commonAPI.setLoginTenant(username); JSONObject jsonObject = commonAPI.setLoginTenant(username);
@ -225,8 +232,15 @@ public class PasswordGrantAuthenticationProvider implements AuthenticationProvid
addition.put("multi_depart", 2); addition.put("multi_depart", 2);
} }
// 兼容原有shiro登录结果处理
Map<String, Object> map = new HashMap<>();
map.put("result", addition);
map.put("code", 200);
map.put("success", true);
map.put("timestamp", System.currentTimeMillis());
// 返回access_token、refresh_token以及其它信息给到前端 // 返回access_token、refresh_token以及其它信息给到前端
return new OAuth2AccessTokenAuthenticationToken(registeredClient, clientPrincipal, accessToken, refreshToken, addition); return new OAuth2AccessTokenAuthenticationToken(registeredClient, clientPrincipal, accessToken, refreshToken, map);
} }
@Override @Override

View File

@ -36,6 +36,7 @@ import org.springframework.security.oauth2.server.authorization.token.OAuth2Toke
import org.springframework.util.Assert; import org.springframework.util.Assert;
import java.security.Principal; import java.security.Principal;
import java.time.Instant;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -89,6 +90,9 @@ public class PhoneGrantAuthenticationProvider implements AuthenticationProvider
// 验证码 // 验证码
String captcha = (String) additionalParameter.get("captcha"); String captcha = (String) additionalParameter.get("captcha");
OAuth2ClientAuthenticationToken clientPrincipal = getAuthenticatedClientElseThrowInvalidClient(phoneGrantAuthenticationToken);
RegisteredClient registeredClient = clientPrincipal.getRegisteredClient();
// 通过手机号获取用户信息 // 通过手机号获取用户信息
LoginUser loginUser = commonAPI.getUserByPhone(phone); LoginUser loginUser = commonAPI.getUserByPhone(phone);
// 检查用户可行性 // 检查用户可行性
@ -101,15 +105,17 @@ public class PhoneGrantAuthenticationProvider implements AuthenticationProvider
if (!captcha.equals(code)) { if (!captcha.equals(code)) {
//update-begin-author:taoyan date:2022-11-7 for: issues/4109 平台用户登录失败锁定用户 //update-begin-author:taoyan date:2022-11-7 for: issues/4109 平台用户登录失败锁定用户
addLoginFailOvertimes(phone); addLoginFailOvertimes(phone);
//update-end-author:taoyan date:2022-11-7 for: issues/4109 平台用户登录失败锁定用户
throw new JeecgBootException("手机验证码错误"); Map<String, Object> map = new HashMap<>();
map.put("message", "手机验证码错误");
return new OAuth2AccessTokenAuthenticationToken(registeredClient, clientPrincipal, new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER,"fdsafas", Instant.now(), Instant.now().plusNanos(1)), null, map);
} }
OAuth2ClientAuthenticationToken clientPrincipal = getAuthenticatedClientElseThrowInvalidClient(phoneGrantAuthenticationToken);
RegisteredClient registeredClient = clientPrincipal.getRegisteredClient();
if (!registeredClient.getAuthorizationGrantTypes().contains(authorizationGrantType)) { if (!registeredClient.getAuthorizationGrantTypes().contains(authorizationGrantType)) {
throw new JeecgBootException("非法登录"); Map<String, Object> map = new HashMap<>();
map.put("message", "非法登录");
return new OAuth2AccessTokenAuthenticationToken(registeredClient, clientPrincipal, new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER,"fdsafas", Instant.now(), Instant.now().plusNanos(1)), null, map);
} }
//由于在上面已验证过用户名、密码现在构建一个已认证的对象UsernamePasswordAuthenticationToken //由于在上面已验证过用户名、密码现在构建一个已认证的对象UsernamePasswordAuthenticationToken
@ -134,9 +140,9 @@ public class PhoneGrantAuthenticationProvider implements AuthenticationProvider
OAuth2TokenContext tokenContext = tokenContextBuilder.tokenType(OAuth2TokenType.ACCESS_TOKEN).build(); OAuth2TokenContext tokenContext = tokenContextBuilder.tokenType(OAuth2TokenType.ACCESS_TOKEN).build();
OAuth2Token generatedAccessToken = this.tokenGenerator.generate(tokenContext); OAuth2Token generatedAccessToken = this.tokenGenerator.generate(tokenContext);
if (generatedAccessToken == null) { if (generatedAccessToken == null) {
OAuth2Error error = new OAuth2Error(OAuth2ErrorCodes.SERVER_ERROR, Map<String, Object> map = new HashMap<>();
"无法生成访问token请联系管理系。", ERROR_URI); map.put("message", "无法生成刷新token请联系管理员。");
throw new OAuth2AuthenticationException(error); return new OAuth2AccessTokenAuthenticationToken(registeredClient, clientPrincipal, new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER,"fdsafas", Instant.now(), Instant.now().plusNanos(1)), null, map);
} }
OAuth2AccessToken accessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER, OAuth2AccessToken accessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER,
generatedAccessToken.getTokenValue(), generatedAccessToken.getIssuedAt(), generatedAccessToken.getTokenValue(), generatedAccessToken.getIssuedAt(),
@ -158,9 +164,9 @@ public class PhoneGrantAuthenticationProvider implements AuthenticationProvider
tokenContext = tokenContextBuilder.tokenType(OAuth2TokenType.REFRESH_TOKEN).build(); tokenContext = tokenContextBuilder.tokenType(OAuth2TokenType.REFRESH_TOKEN).build();
OAuth2Token generatedRefreshToken = this.tokenGenerator.generate(tokenContext); OAuth2Token generatedRefreshToken = this.tokenGenerator.generate(tokenContext);
if (!(generatedRefreshToken instanceof OAuth2RefreshToken)) { if (!(generatedRefreshToken instanceof OAuth2RefreshToken)) {
OAuth2Error error = new OAuth2Error(OAuth2ErrorCodes.SERVER_ERROR, Map<String, Object> map = new HashMap<>();
"无法生成刷新token请联系管理员。", ERROR_URI); map.put("message", "无法生成刷新token请联系管理员。");
throw new OAuth2AuthenticationException(error); return new OAuth2AccessTokenAuthenticationToken(registeredClient, clientPrincipal, new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER,"fdsafas", Instant.now(), Instant.now().plusNanos(1)), null, map);
} }
refreshToken = (OAuth2RefreshToken) generatedRefreshToken; refreshToken = (OAuth2RefreshToken) generatedRefreshToken;
@ -175,7 +181,7 @@ public class PhoneGrantAuthenticationProvider implements AuthenticationProvider
baseCommonService.addLog("用户名: " + loginUser.getUsername() + ",登录成功!", CommonConstant.LOG_TYPE_1, null,loginUser); baseCommonService.addLog("用户名: " + loginUser.getUsername() + ",登录成功!", CommonConstant.LOG_TYPE_1, null,loginUser);
JSONObject addition = new JSONObject(new LinkedHashMap<>()); JSONObject addition = new JSONObject(new LinkedHashMap<>());
addition.put("token", accessToken.getTokenValue());
// 设置租户 // 设置租户
JSONObject jsonObject = commonAPI.setLoginTenant(loginUser.getUsername()); JSONObject jsonObject = commonAPI.setLoginTenant(loginUser.getUsername());
addition.putAll(jsonObject.getInnerMap()); addition.putAll(jsonObject.getInnerMap());
@ -199,8 +205,15 @@ public class PhoneGrantAuthenticationProvider implements AuthenticationProvider
addition.put("multi_depart", 2); addition.put("multi_depart", 2);
} }
// 兼容原有shiro登录结果处理
Map<String, Object> map = new HashMap<>();
map.put("result", addition);
map.put("code", 200);
map.put("success", true);
map.put("timestamp", System.currentTimeMillis());
// 返回access_token、refresh_token以及其它信息给到前端 // 返回access_token、refresh_token以及其它信息给到前端
return new OAuth2AccessTokenAuthenticationToken(registeredClient, clientPrincipal, accessToken, refreshToken, addition); return new OAuth2AccessTokenAuthenticationToken(registeredClient, clientPrincipal, accessToken, refreshToken, map);
} }
@Override @Override

View File

@ -30,6 +30,7 @@ import org.springframework.stereotype.Component;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import java.security.Principal; import java.security.Principal;
import java.time.Instant;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -80,7 +81,9 @@ public class SelfAuthenticationProvider implements AuthenticationProvider {
RegisteredClient registeredClient = clientPrincipal.getRegisteredClient(); RegisteredClient registeredClient = clientPrincipal.getRegisteredClient();
if (!registeredClient.getAuthorizationGrantTypes().contains(authorizationGrantType)) { if (!registeredClient.getAuthorizationGrantTypes().contains(authorizationGrantType)) {
throw new JeecgBootException("非法登录"); Map<String, Object> map = new HashMap<>();
map.put("message", "非法登录");
return new OAuth2AccessTokenAuthenticationToken(registeredClient, clientPrincipal, new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER,"fdsafas", Instant.now(), Instant.now().plusNanos(1)), null, map);
} }
// 通过用户名获取用户信息 // 通过用户名获取用户信息
@ -109,9 +112,10 @@ public class SelfAuthenticationProvider implements AuthenticationProvider {
OAuth2TokenContext tokenContext = tokenContextBuilder.tokenType(OAuth2TokenType.ACCESS_TOKEN).build(); OAuth2TokenContext tokenContext = tokenContextBuilder.tokenType(OAuth2TokenType.ACCESS_TOKEN).build();
OAuth2Token generatedAccessToken = this.tokenGenerator.generate(tokenContext); OAuth2Token generatedAccessToken = this.tokenGenerator.generate(tokenContext);
if (generatedAccessToken == null) { if (generatedAccessToken == null) {
OAuth2Error error = new OAuth2Error(OAuth2ErrorCodes.SERVER_ERROR, Map<String, Object> map = new HashMap<>();
"无法生成访问token请联系管理系。", ERROR_URI); map.put("message", "无法生成刷新token请联系管理员。");
throw new OAuth2AuthenticationException(error); return new OAuth2AccessTokenAuthenticationToken(registeredClient, clientPrincipal, new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER,"fdsafas", Instant.now(), Instant.now().plusNanos(1)), null, map);
} }
OAuth2AccessToken accessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER, OAuth2AccessToken accessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER,
generatedAccessToken.getTokenValue(), generatedAccessToken.getIssuedAt(), generatedAccessToken.getTokenValue(), generatedAccessToken.getIssuedAt(),
@ -133,9 +137,9 @@ public class SelfAuthenticationProvider implements AuthenticationProvider {
tokenContext = tokenContextBuilder.tokenType(OAuth2TokenType.REFRESH_TOKEN).build(); tokenContext = tokenContextBuilder.tokenType(OAuth2TokenType.REFRESH_TOKEN).build();
OAuth2Token generatedRefreshToken = this.tokenGenerator.generate(tokenContext); OAuth2Token generatedRefreshToken = this.tokenGenerator.generate(tokenContext);
if (!(generatedRefreshToken instanceof OAuth2RefreshToken)) { if (!(generatedRefreshToken instanceof OAuth2RefreshToken)) {
OAuth2Error error = new OAuth2Error(OAuth2ErrorCodes.SERVER_ERROR, Map<String, Object> map = new HashMap<>();
"无法生成刷新token请联系管理员。", ERROR_URI); map.put("message", "无法生成刷新token请联系管理员。");
throw new OAuth2AuthenticationException(error); return new OAuth2AccessTokenAuthenticationToken(registeredClient, clientPrincipal, new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER,"fdsafas", Instant.now(), Instant.now().plusNanos(1)), null, map);
} }
refreshToken = (OAuth2RefreshToken) generatedRefreshToken; refreshToken = (OAuth2RefreshToken) generatedRefreshToken;
@ -148,7 +152,7 @@ public class SelfAuthenticationProvider implements AuthenticationProvider {
authorizationService.save(authorization); authorizationService.save(authorization);
JSONObject addition = new JSONObject(new LinkedHashMap<>()); JSONObject addition = new JSONObject(new LinkedHashMap<>());
addition.put("token", accessToken.getTokenValue());
// 设置租户 // 设置租户
JSONObject jsonObject = commonAPI.setLoginTenant(username); JSONObject jsonObject = commonAPI.setLoginTenant(username);
addition.putAll(jsonObject.getInnerMap()); addition.putAll(jsonObject.getInnerMap());
@ -172,8 +176,15 @@ public class SelfAuthenticationProvider implements AuthenticationProvider {
addition.put("multi_depart", 2); addition.put("multi_depart", 2);
} }
// 兼容原有shiro登录结果处理
Map<String, Object> map = new HashMap<>();
map.put("result", addition);
map.put("code", 200);
map.put("success", true);
map.put("timestamp", System.currentTimeMillis());
// 返回access_token、refresh_token以及其它信息给到前端 // 返回access_token、refresh_token以及其它信息给到前端
return new OAuth2AccessTokenAuthenticationToken(registeredClient, clientPrincipal, accessToken, refreshToken, addition); return new OAuth2AccessTokenAuthenticationToken(registeredClient, clientPrincipal, accessToken, refreshToken, map);
} }
@Override @Override

View File

@ -34,6 +34,7 @@ import org.springframework.security.oauth2.server.authorization.token.OAuth2Toke
import org.springframework.util.Assert; import org.springframework.util.Assert;
import java.security.Principal; import java.security.Principal;
import java.time.Instant;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -95,7 +96,10 @@ public class SocialGrantAuthenticationProvider implements AuthenticationProvider
RegisteredClient registeredClient = clientPrincipal.getRegisteredClient(); RegisteredClient registeredClient = clientPrincipal.getRegisteredClient();
if (!registeredClient.getAuthorizationGrantTypes().contains(authorizationGrantType)) { if (!registeredClient.getAuthorizationGrantTypes().contains(authorizationGrantType)) {
throw new JeecgBootException("非法登录"); Map<String, Object> map = new HashMap<>();
map.put("message", "非法登录");
return new OAuth2AccessTokenAuthenticationToken(registeredClient, clientPrincipal, new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER,"fdsafas", Instant.now(), Instant.now().plusNanos(1)), null, map);
} }
//由于在上面已验证过用户名、密码现在构建一个已认证的对象UsernamePasswordAuthenticationToken //由于在上面已验证过用户名、密码现在构建一个已认证的对象UsernamePasswordAuthenticationToken
@ -120,9 +124,10 @@ public class SocialGrantAuthenticationProvider implements AuthenticationProvider
OAuth2TokenContext tokenContext = tokenContextBuilder.tokenType(OAuth2TokenType.ACCESS_TOKEN).build(); OAuth2TokenContext tokenContext = tokenContextBuilder.tokenType(OAuth2TokenType.ACCESS_TOKEN).build();
OAuth2Token generatedAccessToken = this.tokenGenerator.generate(tokenContext); OAuth2Token generatedAccessToken = this.tokenGenerator.generate(tokenContext);
if (generatedAccessToken == null) { if (generatedAccessToken == null) {
OAuth2Error error = new OAuth2Error(OAuth2ErrorCodes.SERVER_ERROR, Map<String, Object> map = new HashMap<>();
"无法生成访问token请联系管理系。", ERROR_URI); map.put("message", "无法生成访问token请联系管理系。");
throw new OAuth2AuthenticationException(error); return new OAuth2AccessTokenAuthenticationToken(registeredClient, clientPrincipal, new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER,"fdsafas", Instant.now(), Instant.now().plusNanos(1)), null, map);
} }
OAuth2AccessToken accessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER, OAuth2AccessToken accessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER,
generatedAccessToken.getTokenValue(), generatedAccessToken.getIssuedAt(), generatedAccessToken.getTokenValue(), generatedAccessToken.getIssuedAt(),
@ -144,9 +149,9 @@ public class SocialGrantAuthenticationProvider implements AuthenticationProvider
tokenContext = tokenContextBuilder.tokenType(OAuth2TokenType.REFRESH_TOKEN).build(); tokenContext = tokenContextBuilder.tokenType(OAuth2TokenType.REFRESH_TOKEN).build();
OAuth2Token generatedRefreshToken = this.tokenGenerator.generate(tokenContext); OAuth2Token generatedRefreshToken = this.tokenGenerator.generate(tokenContext);
if (!(generatedRefreshToken instanceof OAuth2RefreshToken)) { if (!(generatedRefreshToken instanceof OAuth2RefreshToken)) {
OAuth2Error error = new OAuth2Error(OAuth2ErrorCodes.SERVER_ERROR, Map<String, Object> map = new HashMap<>();
"无法生成刷新token请联系管理员。", ERROR_URI); map.put("message", "无法生成刷新token请联系管理员。");
throw new OAuth2AuthenticationException(error); return new OAuth2AccessTokenAuthenticationToken(registeredClient, clientPrincipal, new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER,"fdsafas", Instant.now(), Instant.now().plusNanos(1)), null, map);
} }
refreshToken = (OAuth2RefreshToken) generatedRefreshToken; refreshToken = (OAuth2RefreshToken) generatedRefreshToken;
@ -161,7 +166,7 @@ public class SocialGrantAuthenticationProvider implements AuthenticationProvider
baseCommonService.addLog("用户名: " + loginUser.getUsername() + ",登录成功!", CommonConstant.LOG_TYPE_1, null,loginUser); baseCommonService.addLog("用户名: " + loginUser.getUsername() + ",登录成功!", CommonConstant.LOG_TYPE_1, null,loginUser);
JSONObject addition = new JSONObject(new LinkedHashMap<>()); JSONObject addition = new JSONObject(new LinkedHashMap<>());
addition.put("token", accessToken.getTokenValue());
// 设置租户 // 设置租户
JSONObject jsonObject = commonAPI.setLoginTenant(loginUser.getUsername()); JSONObject jsonObject = commonAPI.setLoginTenant(loginUser.getUsername());
addition.putAll(jsonObject.getInnerMap()); addition.putAll(jsonObject.getInnerMap());
@ -185,8 +190,16 @@ public class SocialGrantAuthenticationProvider implements AuthenticationProvider
addition.put("multi_depart", 2); addition.put("multi_depart", 2);
} }
// 兼容原有shiro登录结果处理
Map<String, Object> map = new HashMap<>();
map.put("result", addition);
map.put("code", 200);
map.put("success", true);
map.put("timestamp", System.currentTimeMillis());
// 返回access_token、refresh_token以及其它信息给到前端 // 返回access_token、refresh_token以及其它信息给到前端
return new OAuth2AccessTokenAuthenticationToken(registeredClient, clientPrincipal, accessToken, refreshToken, addition); return new OAuth2AccessTokenAuthenticationToken(registeredClient, clientPrincipal, accessToken, refreshToken, map);
} }
@Override @Override

View File

@ -89,8 +89,8 @@ public class LoginController {
* @return * @return
*/ */
@Deprecated @Deprecated
@Operation(summary = "登录接口") // @Operation(summary = "登录接口")
@RequestMapping(value = "/login", method = RequestMethod.POST) // @RequestMapping(value = "/login", method = RequestMethod.POST)
public Result<JSONObject> login(@RequestBody SysLoginModel sysLoginModel){ public Result<JSONObject> login(@RequestBody SysLoginModel sysLoginModel){
Result<JSONObject> result = new Result<JSONObject>(); Result<JSONObject> result = new Result<JSONObject>();
String username = sysLoginModel.getUsername(); String username = sysLoginModel.getUsername();