修改H5登录授权
This commit is contained in:
@@ -8,6 +8,7 @@ import com.accompany.business.service.exchange.GoldExchangeDiamondService;
|
||||
import com.accompany.business.service.withdraw.WithdrawUserLimitService;
|
||||
import com.accompany.business.vo.exchange.GoldExchangeDiamondVo;
|
||||
import com.accompany.common.annotation.Authorization;
|
||||
import com.accompany.common.annotation.H5Authorization;
|
||||
import com.accompany.common.exception.ApiException;
|
||||
import com.accompany.common.result.BusiResult;
|
||||
import com.accompany.common.status.BusiStatus;
|
||||
@@ -34,18 +35,20 @@ public class GoldExchangeDiamondController extends BaseController {
|
||||
@Autowired
|
||||
private WithdrawUserLimitService withdrawUserLimitService;
|
||||
|
||||
@Authorization
|
||||
@H5Authorization
|
||||
@ApiOperation("获取配置")
|
||||
@GetMapping(value = "/getConfig")
|
||||
@Authorization
|
||||
public BusiResult<GoldExchangeDiamondVo> getConfig(HttpServletRequest request) {
|
||||
Long uid = getUid(request);
|
||||
GoldExchangeDiamondVo configVo = service.buildVo(uid);
|
||||
return new BusiResult<>(configVo);
|
||||
}
|
||||
|
||||
@Authorization
|
||||
@H5Authorization
|
||||
@ApiOperation("兑换")
|
||||
@PostMapping(value = "/exchange")
|
||||
@Authorization
|
||||
public BusiResult<Void> exchange(HttpServletRequest request, Long goldNum, Long diamondNum) {
|
||||
if (null == goldNum || goldNum < 0 || null == diamondNum || diamondNum < 0) {
|
||||
throw new ServiceException(BusiStatus.PARAMERROR);
|
||||
|
@@ -2,6 +2,7 @@ package com.accompany.business.controller.withdraw;
|
||||
|
||||
import com.accompany.business.service.withdraw.WithdrawAccountDtlService;
|
||||
import com.accompany.business.vo.withdraw.WithdrawAccountDtlVo;
|
||||
import com.accompany.common.annotation.H5Authorization;
|
||||
import com.accompany.common.result.BusiResult;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiImplicitParam;
|
||||
@@ -33,6 +34,7 @@ public class WithdrawAccountDtlController {
|
||||
* @param uid
|
||||
* @return
|
||||
*/
|
||||
@H5Authorization
|
||||
@ApiOperation("账户列表")
|
||||
@ApiImplicitParam(name = "uid", value = "用户ID")
|
||||
@GetMapping("getAccounts")
|
||||
|
@@ -4,6 +4,7 @@ import com.accompany.business.dto.withdraw.WithdrawAmountHolder;
|
||||
import com.accompany.business.model.withdraw.WithdrawUserAccount;
|
||||
import com.accompany.business.service.withdraw.WithdrawUserAccountService;
|
||||
import com.accompany.business.vo.withdraw.WithdrawUserAccountVo;
|
||||
import com.accompany.common.annotation.H5Authorization;
|
||||
import com.accompany.common.result.BusiResult;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiImplicitParam;
|
||||
@@ -34,6 +35,7 @@ public class WithdrawUserAccountController {
|
||||
* @param account
|
||||
* @return
|
||||
*/
|
||||
@H5Authorization
|
||||
@ApiOperation("保存")
|
||||
@PostMapping("save")
|
||||
public BusiResult<Void> save(@RequestBody WithdrawUserAccount account) {
|
||||
@@ -47,6 +49,7 @@ public class WithdrawUserAccountController {
|
||||
* @param uid
|
||||
* @return
|
||||
*/
|
||||
@H5Authorization
|
||||
@ApiOperation("列表")
|
||||
@ApiImplicitParam(name = "uid", value = "用户ID")
|
||||
@GetMapping("list")
|
||||
@@ -62,6 +65,7 @@ public class WithdrawUserAccountController {
|
||||
* @param goldNum
|
||||
* @return
|
||||
*/
|
||||
@H5Authorization
|
||||
@ApiOperation("汇率计算")
|
||||
@ApiImplicitParams({
|
||||
@ApiImplicitParam(name = "uid", value = "用户ID", required = true),
|
||||
@@ -81,6 +85,7 @@ public class WithdrawUserAccountController {
|
||||
* @param goldNum
|
||||
* @return
|
||||
*/
|
||||
@H5Authorization
|
||||
@ApiOperation(value = "提现申请", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
|
||||
@ApiImplicitParams({
|
||||
@ApiImplicitParam(name = "uid", value = "用户ID", required = true),
|
||||
|
@@ -2,6 +2,7 @@ package com.accompany.business.controller.withdraw;
|
||||
|
||||
import com.accompany.business.model.withdraw.WithdrawConfig;
|
||||
import com.accompany.business.service.withdraw.WithdrawUserAccountService;
|
||||
import com.accompany.common.annotation.H5Authorization;
|
||||
import com.accompany.common.result.BusiResult;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
@@ -30,6 +31,7 @@ public class WithdrawUserController {
|
||||
* @param uid
|
||||
* @return
|
||||
*/
|
||||
@H5Authorization
|
||||
@ApiOperation("提现配置")
|
||||
@GetMapping("config")
|
||||
public BusiResult<WithdrawConfig> config(@RequestParam("uid") Long uid) {
|
||||
|
@@ -4,6 +4,10 @@ import com.accompany.core.service.SysConfService;
|
||||
import com.accompany.core.service.user.PhoneBlackService;
|
||||
import com.accompany.oauth2.service.MyUserDetailsService;
|
||||
import com.accompany.oauth2.service.MyUserDetailsServiceImpl;
|
||||
import com.accompany.oauth2.service.account.AccountH5LoginService;
|
||||
import com.accompany.oauth2.support.h5.AbstractH5TokenGranter;
|
||||
import com.accompany.oauth2.support.h5.PasswordH5TokenGranter;
|
||||
import com.accompany.oauth2.support.h5.VerifyCodeH5TokenGranter;
|
||||
import com.accompany.oauth2.support.password.PasswordAuthenticationProvider;
|
||||
import com.accompany.oauth2.support.verify.VerifyCodeAuthenticationProvider;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -29,6 +33,9 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
@Autowired
|
||||
private PhoneBlackService phoneBlackService;
|
||||
|
||||
@Autowired
|
||||
private AccountH5LoginService accountH5LoginService;
|
||||
|
||||
@Bean
|
||||
@Override
|
||||
protected UserDetailsService userDetailsService() {
|
||||
@@ -75,4 +82,14 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
public AuthenticationProvider verifyCodeAuthenticationProvider() {
|
||||
return new VerifyCodeAuthenticationProvider(myUserDetailsService(), phoneBlackService);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public AbstractH5TokenGranter passwordH5TokenGranter() {
|
||||
return new PasswordH5TokenGranter(myUserDetailsService(), accountH5LoginService);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public AbstractH5TokenGranter verifyCodeH5TokenGranter() {
|
||||
return new VerifyCodeH5TokenGranter(myUserDetailsService(), phoneBlackService, accountH5LoginService);
|
||||
}
|
||||
}
|
||||
|
@@ -20,4 +20,12 @@ public interface MyUserDetailsService extends UserDetailsService {
|
||||
Boolean logout(String accessToken);
|
||||
|
||||
AccountLoginRecord buildAccountLoginRecord(String ipAddress, Account account, byte loginType, DeviceInfo deviceInfo, String openId);
|
||||
|
||||
/**
|
||||
* 处理密码登录
|
||||
*
|
||||
* @param username 用户登录账号
|
||||
* @return 错误提示
|
||||
*/
|
||||
void handlePwdLogin(String username, String password, UserDetails userDetails);
|
||||
}
|
||||
|
@@ -255,4 +255,35 @@ public class MyUserDetailsServiceImpl implements MyUserDetailsService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理密码登录
|
||||
*
|
||||
* @param username 用户登录账号
|
||||
* @return 错误提示
|
||||
*/
|
||||
@Override
|
||||
public void handlePwdLogin(String username, String password, UserDetails userDetails) {
|
||||
String value = sysConfService.getDefaultSysConfValueById(Constant.SysConfId.PWD_LOGIN_DAY_WRONG_COUNT, "5");
|
||||
Long maxCount = Long.valueOf(value);
|
||||
String cacheKey = RedisKey.user_login_pwd_wrong_day_count.getKey();
|
||||
Boolean exits = jedisService.exits(cacheKey);
|
||||
String countValue = jedisService.hget(cacheKey, username);
|
||||
Long currCount = com.accompany.common.utils.StringUtils.isBlank(countValue) ? 0L : Long.parseLong(countValue);
|
||||
if (currCount >= maxCount) {
|
||||
throw new CustomOAuth2Exception(CustomOAuth2Exception.PWD_WRONG_OVER_LIMIT, OAuthStatus.PWD_WRONG_OVER_LIMIT.getReasonPhrase());
|
||||
}
|
||||
if (!password.equals(userDetails.getPassword())) {
|
||||
currCount = jedisService.hincrBy(cacheKey, username, 1L);
|
||||
if (!exits) {
|
||||
jedisService.expire(cacheKey, 10 * 60);//10分钟后解锁
|
||||
}
|
||||
if (currCount >= maxCount) {
|
||||
throw new CustomOAuth2Exception(CustomOAuth2Exception.PWD_WRONG_OVER_LIMIT, OAuthStatus.PWD_WRONG_OVER_LIMIT.getReasonPhrase());
|
||||
} else {
|
||||
Long remainCount = maxCount - currCount;
|
||||
throw new CustomOAuth2Exception(CustomOAuth2Exception.PASSWORD_ERROR, String.format("密碼錯誤,還剩%d次機會", remainCount));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,5 +1,6 @@
|
||||
package com.accompany.oauth2.service.account;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import com.accompany.common.constant.ApplicationConstant;
|
||||
import com.accompany.common.constant.Constant;
|
||||
import com.accompany.common.device.DeviceInfo;
|
||||
@@ -16,16 +17,21 @@ import com.accompany.core.mybatismapper.AccountH5LoginRecordMapper;
|
||||
import com.accompany.core.service.account.AccountService;
|
||||
import com.accompany.core.service.common.JedisService;
|
||||
import com.accompany.core.util.JwtUtils;
|
||||
import com.accompany.oauth2.support.h5.AbstractH5TokenGranter;
|
||||
import com.accompany.oauth2.token.H5AccessToken;
|
||||
import com.accompany.sms.service.SmsService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.context.request.ServletWebRequest;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Created by yuanyi on 2019/2/21.
|
||||
@@ -33,6 +39,14 @@ import java.util.Map;
|
||||
@Service
|
||||
@Slf4j
|
||||
public class AccountH5LoginService {
|
||||
|
||||
private static final long H5_JWT_TOKEN_EX = 60 * 60 * 1000 * 2L;
|
||||
|
||||
/**
|
||||
* code有效时间为10分钟
|
||||
**/
|
||||
private static final int CODE_EXPIRE_TIME = 60 * 10;
|
||||
|
||||
@Autowired
|
||||
private AccountH5LoginRecordMapper accountH5LoginRecordMapper;
|
||||
@Autowired
|
||||
@@ -50,10 +64,8 @@ public class AccountH5LoginService {
|
||||
@Autowired
|
||||
private AccountManageService accountManageService;
|
||||
|
||||
/**
|
||||
* code有效时间为10分钟
|
||||
**/
|
||||
private static final int CODE_EXPIRE_TIME = 60 * 10;
|
||||
@Autowired
|
||||
private List<AbstractH5TokenGranter> h5tokenGranters;
|
||||
|
||||
public AccountH5LoginRecord buildRecord(Long uid, Byte loginType, String ip, String os, String appVersion, String deviceId) {
|
||||
AccountH5LoginRecord accountH5LoginRecord = new AccountH5LoginRecord();
|
||||
@@ -68,8 +80,8 @@ public class AccountH5LoginService {
|
||||
}
|
||||
|
||||
@Async
|
||||
public Integer insertRecord(AccountH5LoginRecord accountH5LoginRecord) {
|
||||
return accountH5LoginRecordMapper.insert(accountH5LoginRecord);
|
||||
public void insertRecord(AccountH5LoginRecord accountH5LoginRecord) {
|
||||
accountH5LoginRecordMapper.insert(accountH5LoginRecord);
|
||||
}
|
||||
|
||||
//获取授权码
|
||||
@@ -132,11 +144,6 @@ public class AccountH5LoginService {
|
||||
return map;
|
||||
}
|
||||
|
||||
public String createJwtToken(Long uid) {
|
||||
Long mills = 60 * 60 * 1000 * 2L;
|
||||
return jwtUtils.createJWT(mills, uid);
|
||||
}
|
||||
|
||||
public Map<String, String> smsLogin(String mobile, String code) {
|
||||
// 校验验证码
|
||||
if (!smsService.verifySmsCode(mobile, code)) {
|
||||
@@ -157,6 +164,10 @@ public class AccountH5LoginService {
|
||||
return map;
|
||||
}
|
||||
|
||||
public String createJwtToken(Long uid) {
|
||||
return jwtUtils.createJWT(H5_JWT_TOKEN_EX, uid);
|
||||
}
|
||||
|
||||
private void saveH5LoginJwtToken(Long uid, String jwtToken) {
|
||||
jedisService.hset(RedisKey.h5loginjwtoken.getKey(), uid.toString(), jwtToken);
|
||||
}
|
||||
@@ -181,4 +192,34 @@ public class AccountH5LoginService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建h5令牌
|
||||
*
|
||||
* @param uid
|
||||
* @return
|
||||
*/
|
||||
public H5AccessToken createH5AccessToken(Long uid) {
|
||||
String jwtToken = createJwtToken(uid);
|
||||
saveH5LoginJwtToken(uid, jwtToken);
|
||||
H5AccessToken accessToken = new H5AccessToken();
|
||||
accessToken.setAccess_token(jwtToken);
|
||||
accessToken.setExpires_in(H5_JWT_TOKEN_EX);
|
||||
return accessToken;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取token
|
||||
*
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
public H5AccessToken token(ServletWebRequest request) {
|
||||
String grantType = request.getParameter("grantType");
|
||||
for (AbstractH5TokenGranter tokenGranter : h5tokenGranters) {
|
||||
if (tokenGranter.getGrantType().equals(grantType)) {
|
||||
return tokenGranter.getAuthentication(request);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,48 @@
|
||||
package com.accompany.oauth2.support.h5;
|
||||
|
||||
import com.accompany.oauth2.token.H5AccessToken;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.web.context.request.ServletWebRequest;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author: liaozetao
|
||||
* @date: 2023/7/17 10:25
|
||||
* @description:
|
||||
*/
|
||||
public abstract class AbstractH5TokenGranter {
|
||||
|
||||
protected static final String PHONE_AREA_CODE = "phoneAreaCode";
|
||||
|
||||
protected static final String PHONE = "phone";
|
||||
|
||||
protected static final String PASSWORD = "password";
|
||||
|
||||
protected static final String CODE = "code";
|
||||
|
||||
private final String grantType;
|
||||
|
||||
public AbstractH5TokenGranter(String grantType) {
|
||||
this.grantType = grantType;
|
||||
}
|
||||
|
||||
public H5AccessToken getAuthentication(ServletWebRequest request) {
|
||||
Map<String, Object> parameters = new HashMap<>();
|
||||
for (Map.Entry<String, String[]> entry : request.getParameterMap().entrySet()) {
|
||||
String key = entry.getKey();
|
||||
String[] value = entry.getValue();
|
||||
if (value.length > 0) {
|
||||
parameters.put(key, value[0]);
|
||||
}
|
||||
}
|
||||
return authenticate(parameters);
|
||||
}
|
||||
|
||||
public abstract H5AccessToken authenticate(Map<String, Object> parameters);
|
||||
|
||||
public String getGrantType() {
|
||||
return grantType;
|
||||
}
|
||||
}
|
@@ -0,0 +1,68 @@
|
||||
package com.accompany.oauth2.support.h5;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.accompany.common.device.DeviceInfo;
|
||||
import com.accompany.common.utils.DESUtils;
|
||||
import com.accompany.core.util.KeyStore;
|
||||
import com.accompany.core.util.MD5;
|
||||
import com.accompany.oauth2.constant.GrantTypeEnum;
|
||||
import com.accompany.oauth2.constant.LoginTypeEnum;
|
||||
import com.accompany.oauth2.model.AccountDetails;
|
||||
import com.accompany.oauth2.service.MyUserDetailsService;
|
||||
import com.accompany.oauth2.service.account.AccountH5LoginService;
|
||||
import com.accompany.oauth2.token.H5AccessToken;
|
||||
import com.accompany.oauth2.util.RequestContextHolderUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.beanutils.BeanUtils;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author: liaozetao
|
||||
* @date: 2023/7/17 10:37
|
||||
* @description:
|
||||
*/
|
||||
@Slf4j
|
||||
public class PasswordH5TokenGranter extends AbstractH5TokenGranter {
|
||||
|
||||
private final MyUserDetailsService userDetailsService;
|
||||
|
||||
private final AccountH5LoginService accountH5LoginService;
|
||||
|
||||
public PasswordH5TokenGranter(MyUserDetailsService userDetailsService, AccountH5LoginService accountH5LoginService) {
|
||||
super(GrantTypeEnum.PASSWORD.getValue());
|
||||
this.userDetailsService = userDetailsService;
|
||||
this.accountH5LoginService = accountH5LoginService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public H5AccessToken authenticate(Map<String, Object> parameters) {
|
||||
String phoneAreaCode = StrUtil.toString(parameters.get(PHONE_AREA_CODE));
|
||||
String username = StrUtil.toString(parameters.get(PHONE));
|
||||
String password = StrUtil.toString(parameters.get(PASSWORD));
|
||||
String code = StrUtil.toString(parameters.get(CODE));
|
||||
String ipAddress = RequestContextHolderUtils.getRemoteAddr();
|
||||
DeviceInfo deviceInfo = new DeviceInfo();
|
||||
try {
|
||||
BeanUtils.populate(deviceInfo, parameters);
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
UserDetails userDetails;
|
||||
try {
|
||||
userDetails = userDetailsService.loadUserByPhone(username, phoneAreaCode, code, deviceInfo, ipAddress);
|
||||
try {
|
||||
password = MD5.getMD5(DESUtils.DESAndBase64Decrypt(password, KeyStore.DES_ENCRYPT_KEY));
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException("密码非法");
|
||||
}
|
||||
userDetailsService.handlePwdLogin(username, password, userDetails);
|
||||
userDetailsService.login(username, userDetails, LoginTypeEnum.PASSWORD, deviceInfo, code);
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
throw new RuntimeException(e.getMessage());
|
||||
}
|
||||
return accountH5LoginService.createH5AccessToken(((AccountDetails) userDetails).getAccount().getUid());
|
||||
}
|
||||
}
|
@@ -0,0 +1,69 @@
|
||||
package com.accompany.oauth2.support.h5;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.accompany.common.device.DeviceInfo;
|
||||
import com.accompany.common.exception.ApiException;
|
||||
import com.accompany.core.service.user.PhoneBlackService;
|
||||
import com.accompany.oauth2.constant.GrantTypeEnum;
|
||||
import com.accompany.oauth2.constant.LoginTypeEnum;
|
||||
import com.accompany.oauth2.constant.OAuthStatus;
|
||||
import com.accompany.oauth2.exception.CustomOAuth2Exception;
|
||||
import com.accompany.oauth2.model.AccountDetails;
|
||||
import com.accompany.oauth2.service.MyUserDetailsService;
|
||||
import com.accompany.oauth2.service.account.AccountH5LoginService;
|
||||
import com.accompany.oauth2.token.H5AccessToken;
|
||||
import com.accompany.oauth2.util.RequestContextHolderUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.beanutils.BeanUtils;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author: liaozetao
|
||||
* @date: 2023/7/17 10:38
|
||||
* @description:
|
||||
*/
|
||||
@Slf4j
|
||||
public class VerifyCodeH5TokenGranter extends AbstractH5TokenGranter {
|
||||
|
||||
private final MyUserDetailsService userDetailsService;
|
||||
|
||||
private final PhoneBlackService phoneBlackService;
|
||||
|
||||
private final AccountH5LoginService accountH5LoginService;
|
||||
|
||||
public VerifyCodeH5TokenGranter(MyUserDetailsService userDetailsService, PhoneBlackService phoneBlackService, AccountH5LoginService accountH5LoginService) {
|
||||
super(GrantTypeEnum.VERIFY_CODE.getValue());
|
||||
this.userDetailsService = userDetailsService;
|
||||
this.phoneBlackService = phoneBlackService;
|
||||
this.accountH5LoginService = accountH5LoginService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public H5AccessToken authenticate(Map<String, Object> parameters) {
|
||||
String phoneAreaCode = StrUtil.toString(parameters.get(PHONE_AREA_CODE));
|
||||
String phone = StrUtil.toString(parameters.get(PHONE));
|
||||
String code = StrUtil.toString(parameters.get(CODE));
|
||||
DeviceInfo deviceInfo = new DeviceInfo();
|
||||
try {
|
||||
BeanUtils.populate(deviceInfo, parameters);
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
if (phoneBlackService.checkIsNeedIntercept(phone)) {
|
||||
throw new CustomOAuth2Exception(CustomOAuth2Exception.PHONE_BE_INTERCEPTED, OAuthStatus.PHONE_BE_INTERCEPTED.getReasonPhrase());
|
||||
}
|
||||
UserDetails userDetails = null;
|
||||
try {
|
||||
userDetails = userDetailsService.loadUserByPhone(phone, phoneAreaCode, code, deviceInfo, RequestContextHolderUtils.getRemoteAddr());
|
||||
userDetailsService.login(phone, userDetails, LoginTypeEnum.ID, deviceInfo, code);
|
||||
} catch (CustomOAuth2Exception e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
throw new RuntimeException(e.getMessage());
|
||||
}
|
||||
return accountH5LoginService.createH5AccessToken(((AccountDetails) userDetails).getAccount().getUid());
|
||||
}
|
||||
}
|
@@ -109,7 +109,7 @@ public class PasswordAuthenticationProvider implements AuthenticationProvider {
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException("密码非法");
|
||||
}
|
||||
handlePwdLogin(username, password, userDetails);
|
||||
myUserDetailsService.handlePwdLogin(username, password, userDetails);
|
||||
}
|
||||
myUserDetailsService.login(username, userDetails, loginTypeEnum, deviceInfo, ipAddress, "", "", smsCode);
|
||||
} catch (CustomOAuth2Exception ce) {
|
||||
@@ -127,34 +127,4 @@ public class PasswordAuthenticationProvider implements AuthenticationProvider {
|
||||
return UsernamePasswordAuthenticationToken.class.isAssignableFrom(aClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理密码登录
|
||||
*
|
||||
* @param username 用户登录账号
|
||||
* @return 错误提示
|
||||
*/
|
||||
private void handlePwdLogin(String username, String password, UserDetails userDetails) {
|
||||
String value = sysConfService.getDefaultSysConfValueById(Constant.SysConfId.PWD_LOGIN_DAY_WRONG_COUNT, "5");
|
||||
Long maxCount = Long.valueOf(value);
|
||||
String cacheKey = RedisKey.user_login_pwd_wrong_day_count.getKey();
|
||||
Boolean exits = jedisService.exits(cacheKey);
|
||||
String countValue = jedisService.hget(cacheKey, username);
|
||||
Long currCount = StringUtils.isBlank(countValue) ? 0L : Long.parseLong(countValue);
|
||||
if (currCount >= maxCount) {
|
||||
throw new CustomOAuth2Exception(CustomOAuth2Exception.PWD_WRONG_OVER_LIMIT, OAuthStatus.PWD_WRONG_OVER_LIMIT.getReasonPhrase());
|
||||
}
|
||||
if (!password.equals(userDetails.getPassword())) {
|
||||
currCount = jedisService.hincrBy(cacheKey, username, 1L);
|
||||
if (!exits) {
|
||||
jedisService.expire(cacheKey, 10 * 60);//10分钟后解锁
|
||||
}
|
||||
if (currCount >= maxCount) {
|
||||
throw new CustomOAuth2Exception(CustomOAuth2Exception.PWD_WRONG_OVER_LIMIT, OAuthStatus.PWD_WRONG_OVER_LIMIT.getReasonPhrase());
|
||||
} else {
|
||||
Long remainCount = maxCount - currCount;
|
||||
throw new CustomOAuth2Exception(CustomOAuth2Exception.PASSWORD_ERROR, String.format("密碼錯誤,還剩%d次機會", remainCount));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -1,5 +1,6 @@
|
||||
package com.accompany.oauth2.token;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.accompany.oauth2.support.CustomOAuth2AccessTokenJackson2Deserializer;
|
||||
import com.accompany.oauth2.support.CustomOAuth2AccessTokenJackson2Serializer;
|
||||
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
|
||||
@@ -18,8 +19,9 @@ import org.springframework.security.oauth2.common.OAuth2AccessToken;
|
||||
)
|
||||
public class CustomOAuth2AccessToken extends DefaultOAuth2AccessToken {
|
||||
|
||||
public String netEaseToken="";
|
||||
public String accid="";
|
||||
public String netEaseToken = StrUtil.EMPTY;
|
||||
public String accid = StrUtil.EMPTY;
|
||||
|
||||
public long uid;
|
||||
|
||||
public CustomOAuth2AccessToken(OAuth2AccessToken accessToken) {
|
||||
|
@@ -0,0 +1,33 @@
|
||||
package com.accompany.oauth2.token;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author: liaozetao
|
||||
* @date: 2023/7/17 10:33
|
||||
* @description:
|
||||
*/
|
||||
@ApiModel
|
||||
@Data
|
||||
public class H5AccessToken {
|
||||
|
||||
/**
|
||||
* 用户ID
|
||||
*/
|
||||
@ApiModelProperty("用户ID")
|
||||
private Long uid;
|
||||
|
||||
/**
|
||||
* 令牌
|
||||
*/
|
||||
@ApiModelProperty("令牌")
|
||||
private String access_token;
|
||||
|
||||
/**
|
||||
* 失效时间
|
||||
*/
|
||||
@ApiModelProperty("失效时间")
|
||||
private Long expires_in;
|
||||
}
|
@@ -1,7 +1,5 @@
|
||||
package com.accompany.oauth2.controller;
|
||||
|
||||
import com.accompany.oauth2.common.BaseController;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.accompany.common.annotation.Authorization;
|
||||
import com.accompany.common.device.DeviceInfo;
|
||||
import com.accompany.common.exception.ApiException;
|
||||
@@ -10,112 +8,155 @@ import com.accompany.common.result.BusiResult;
|
||||
import com.accompany.common.status.BusiStatus;
|
||||
import com.accompany.common.utils.IPUitls;
|
||||
import com.accompany.common.utils.StringUtils;
|
||||
import com.accompany.oauth2.common.BaseController;
|
||||
import com.accompany.oauth2.exception.CustomOAuth2Exception;
|
||||
import com.accompany.oauth2.service.account.AccountH5LoginService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import com.accompany.oauth2.token.H5AccessToken;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.context.request.ServletWebRequest;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Created by yuanyi on 2019/2/22.
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/oauth/h5")
|
||||
public class H5LoginController extends BaseController {
|
||||
public static final Logger logger = LoggerFactory.getLogger(H5LoginController.class);
|
||||
|
||||
@Autowired
|
||||
private AccountH5LoginService accountH5LoginService;
|
||||
|
||||
/**
|
||||
* app授权登录
|
||||
*
|
||||
* @param request
|
||||
* @param code
|
||||
* @param uid
|
||||
* @return
|
||||
*/
|
||||
@Authorization
|
||||
@RequestMapping(value = "/authorized/login",method = RequestMethod.POST)
|
||||
public BusiResult<Void> loginAuthorizedByH5(HttpServletRequest request, @RequestParam String code, @RequestParam Long uid, @RequestBody DeviceInfo deviceInfo){
|
||||
logger.info("h5 authorized login params is: code={},uid={},deviceInfo={}",code,uid, JSONObject.toJSON(deviceInfo));
|
||||
try{
|
||||
@RequestMapping(value = "/authorized/login", method = RequestMethod.POST)
|
||||
public BusiResult<Void> loginAuthorizedByH5(HttpServletRequest request, @RequestParam String code, @RequestParam Long uid, @RequestBody DeviceInfo deviceInfo) {
|
||||
log.info("h5 authorized login params is: code={},uid={},deviceInfo={}", code, uid, JSONObject.toJSON(deviceInfo));
|
||||
try {
|
||||
String ip = IPUitls.getRealIpAddress(request);
|
||||
this.accountH5LoginService.h5AuthLogin(code,uid,ip,deviceInfo.getOs(),deviceInfo.getAppVersion(),deviceInfo.getDeviceId());
|
||||
this.accountH5LoginService.h5AuthLogin(code, uid, ip, deviceInfo.getOs(), deviceInfo.getAppVersion(), deviceInfo.getDeviceId());
|
||||
return new BusiResult<>(BusiStatus.SUCCESS);
|
||||
}catch (CustomOAuth2Exception e) {
|
||||
} catch (CustomOAuth2Exception e) {
|
||||
return new BusiResult<>(BusiStatus.ACCOUNT_ERROR);
|
||||
}catch (BusinessException e){
|
||||
return new BusiResult<>(BusiStatus.SERVERERROR, e.getMessage(),null);
|
||||
}catch (Exception e){
|
||||
logger.error("h5 authorized login failed,param is: code={},uid={},deviceInfo={}",code,uid, JSONObject.toJSON(deviceInfo),e);
|
||||
} catch (BusinessException e) {
|
||||
return new BusiResult<>(BusiStatus.SERVERERROR, e.getMessage(), null);
|
||||
} catch (Exception e) {
|
||||
log.error("h5 authorized login failed,param is: code={},uid={},deviceInfo={}", code, uid, JSONObject.toJSON(deviceInfo), e);
|
||||
return new BusiResult<>(BusiStatus.BUSIERROR);
|
||||
}
|
||||
}
|
||||
|
||||
// 获取授权码
|
||||
@RequestMapping(value = "/code/get",method = RequestMethod.GET)
|
||||
public BusiResult<String> getAuthCode(){
|
||||
/**
|
||||
* 获取授权码
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@GetMapping(value = "/code/get")
|
||||
public BusiResult<String> getAuthCode() {
|
||||
String code = this.accountH5LoginService.getAuthCode();
|
||||
return new BusiResult<>(BusiStatus.SUCCESS, code);
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/code/check", method = RequestMethod.GET)
|
||||
public BusiResult<String> checkCodeIsUsed(String code){
|
||||
if(StringUtils.isEmpty(code)){
|
||||
/**
|
||||
* @param code
|
||||
* @return
|
||||
*/
|
||||
@GetMapping(value = "/code/check")
|
||||
public BusiResult<String> checkCodeIsUsed(String code) {
|
||||
if (StringUtils.isEmpty(code)) {
|
||||
return new BusiResult<>(BusiStatus.PARAMERROR);
|
||||
}
|
||||
try{
|
||||
try {
|
||||
String token = this.accountH5LoginService.checkCodeIsUsed(code);
|
||||
if(StringUtils.isEmpty(token)){
|
||||
return new BusiResult<>(1001,"no used",null);
|
||||
if (StringUtils.isEmpty(token)) {
|
||||
return new BusiResult<>(1001, "no used", null);
|
||||
}
|
||||
return new BusiResult<>(BusiStatus.SUCCESS, token);
|
||||
}catch (BusinessException e){
|
||||
return new BusiResult<>(500,e.getMessage(),null);
|
||||
}catch (Exception e){
|
||||
} catch (BusinessException e) {
|
||||
return new BusiResult<>(500, e.getMessage(), null);
|
||||
} catch (Exception e) {
|
||||
return new BusiResult<>(BusiStatus.BUSIERROR);
|
||||
}
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/login", method = RequestMethod.POST)
|
||||
public BusiResult<Map<String, String>> loginByToken(String token){
|
||||
try{
|
||||
Map<String,String> map = this.accountH5LoginService.h5Login(token);
|
||||
logger.info("h5 login ...... token={},result={}",token,JSONObject.toJSON(map));
|
||||
return new BusiResult<>(BusiStatus.SUCCESS, map);
|
||||
}catch (ApiException e){
|
||||
return new BusiResult<>(e.getResponseCode(),e.getMessage(),null);
|
||||
}catch (CustomOAuth2Exception e){
|
||||
/**
|
||||
* @param token
|
||||
* @return
|
||||
*/
|
||||
@PostMapping(value = "/login")
|
||||
public BusiResult<Map<String, String>> loginByToken(String token) {
|
||||
try {
|
||||
Map<String, String> map = this.accountH5LoginService.h5Login(token);
|
||||
log.info("h5 login ...... token={},result={}", token, JSONObject.toJSON(map));
|
||||
return new BusiResult<>(BusiStatus.SUCCESS, map);
|
||||
} catch (ApiException e) {
|
||||
return new BusiResult<>(e.getResponseCode(), e.getMessage(), null);
|
||||
} catch (CustomOAuth2Exception e) {
|
||||
return new BusiResult<>(BusiStatus.ACCOUNT_ERROR);
|
||||
}catch (Exception e){
|
||||
logger.error("h5 login failed,token={}",token,e);
|
||||
} catch (Exception e) {
|
||||
log.error("h5 login failed,token={}", token, e);
|
||||
return new BusiResult<>(BusiStatus.BUSIERROR);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 手机验证码H5登录
|
||||
*
|
||||
* @param mobile
|
||||
* @param code
|
||||
* @return
|
||||
*/
|
||||
@RequestMapping(value = "/smsLogin", method = RequestMethod.POST)
|
||||
public BusiResult<Map<String, String>> loginBySms(String mobile, String code){
|
||||
logger.info("h5 smsLogin, mobile:{}, code:{}", mobile, code);
|
||||
@PostMapping("/smsLogin")
|
||||
public BusiResult<Map<String, String>> loginBySms(String mobile, String code) {
|
||||
log.info("h5 smsLogin, mobile:{}, code:{}", mobile, code);
|
||||
return new BusiResult<>(accountH5LoginService.smsLogin(mobile, code));
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册
|
||||
*
|
||||
* @param phoneAreaCode
|
||||
* @param mobile
|
||||
* @param code
|
||||
* @param inviteCode
|
||||
* @param inviteUid
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/register")
|
||||
public BusiResult<Void> registerByPhone(String phoneAreaCode,String mobile, String code, String inviteCode, Long inviteUid, HttpServletRequest request) {
|
||||
logger.info("h5 registerByPhone, mobile:{}, code:{}, inviteCode: {}", mobile, code, inviteCode);
|
||||
public BusiResult<Void> registerByPhone(String phoneAreaCode, String mobile, String code, String inviteCode, Long inviteUid, HttpServletRequest request) {
|
||||
log.info("h5 registerByPhone, mobile:{}, code:{}, inviteCode: {}", mobile, code, inviteCode);
|
||||
DeviceInfo deviceInfo = getDeviceInfo(request);
|
||||
String ipAddress = IPUitls.getRealIpAddress(request);
|
||||
accountH5LoginService.register(mobile, code, inviteCode, inviteUid, deviceInfo, ipAddress,phoneAreaCode);
|
||||
accountH5LoginService.register(mobile, code, inviteCode, inviteUid, deviceInfo, ipAddress, phoneAreaCode);
|
||||
return new BusiResult<>(BusiStatus.SUCCESS);
|
||||
}
|
||||
|
||||
/**
|
||||
* 授权登录
|
||||
*
|
||||
* @param request
|
||||
* @param response
|
||||
* @return
|
||||
*/
|
||||
@ApiOperation("授权登录")
|
||||
@PostMapping("/token")
|
||||
public BusiResult<H5AccessToken> token(HttpServletRequest request, HttpServletResponse response) {
|
||||
return BusiResult.success(accountH5LoginService.token(new ServletWebRequest(request, response)));
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user