Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.vcredit.lrh.auth.service.Impl; import com.vcredit.lrh.auth.mapper.LrhAccountMapper; import com.vcredit.lrh.auth.mapper.LrhCustomerMapper; import com.vcredit.lrh.auth.service.SecurityService; import com.vcredit.lrh.auth.service.base.dto.AccountDto; import com.vcredit.lrh.auth.service.base.input.LoginParam; import com.vcredit.lrh.auth.service.base.model.LrhCustomer; import com.vcredit.lrh.commons.constants.LrhErrorConstants; import com.vcredit.lrh.commons.constants.RedisCacheKeys; import com.vcredit.lrh.commons.exceptions.O2OServerException; import com.vcredit.lrh.commons.exceptions.ValidationFailedException; import com.vcredit.lrh.commons.validator.O2OValidator; import com.vcredit.lrh.db.redis.template.O2ORedisTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; import javax.servlet.http.HttpServletRequest; import java.util.HashMap; import java.util.Map; @Service public class SecurityServiceImpl implements SecurityService { @Autowired O2ORedisTemplate redisTemplate; @Value("${spring.application.name}") String applicationName; @Autowired private LrhAccountMapper lrhAccountMapper; @Autowired private LrhCustomerMapper lrhCustomerMapper; /** * ?accessToken?? * @param accessToken * @return */ public AccountDto getUserByAccessToken(String accessToken) { return redisTemplate.get(RedisCacheKeys.ACCOUNT_CACHE_TOKEN + accessToken, AccountDto.class);//?? } /** * ??access token ?Redis * @param accessToken */ public void saveUserInfoWithToken(AccountDto account, String accessToken) { redisTemplate.put(RedisCacheKeys.ACCOUNT_CACHE_TOKEN + accessToken, account);//?access tokenkey String loginName = account.getLoginName(); redisTemplate.put(RedisCacheKeys.ACCESS_TOKEN_CACHE_PREFIX + loginName, accessToken);//tokenlogin name key } /** * ???accessToken */ public void deleteAccountInfoByLoginName(String loginName) { String accessToken = redisTemplate.get(RedisCacheKeys.ACCESS_TOKEN_CACHE_PREFIX + loginName);//redis?token if (!StringUtils.isEmpty(accessToken)) { redisTemplate.delete(RedisCacheKeys.ACCOUNT_CACHE_TOKEN + accessToken);//? redisTemplate.delete(RedisCacheKeys.ACCESS_TOKEN_CACHE_PREFIX + loginName);//accessToken } } /** * ?accessToken?accessToken */ public void deteteAccountInfoByAccessToken(String accessToken) { AccountDto accountDto = getUserByAccessToken(accessToken); if (null != accountDto) { redisTemplate.delete(RedisCacheKeys.ACCOUNT_CACHE_TOKEN + accessToken);//? redisTemplate.delete(RedisCacheKeys.ACCESS_TOKEN_CACHE_PREFIX + accountDto.getLoginName());//accessToken } } /** * ?? * * @return */ public Map<String, Object> authenticate(HttpServletRequest request, LoginParam loginParam) throws Exception { String accessToken = request.getSession().getId().toUpperCase(); AccountDto accountDto = null; String loginName = loginParam.getLoginName(); String password = loginParam.getPassword(); //? O2OValidator.validateMobile(loginName); O2OValidator.validatePassword(password); //?? accountDto = lrhAccountMapper.getAcountByLoginName(loginName); if (null == accountDto) { throw new ValidationFailedException(LrhErrorConstants.AUTH_LOGIN_NAME_NOT_EXIT); } //?? if (!accountDto.getPassword().equals(password)) { throw new ValidationFailedException(LrhErrorConstants.AUTH_PASSWORD_ERROR); } //??accessToken???accessToken?? // ??????????? deleteAccountInfoByLoginName(loginName); //??access token ?Redis accountDto.setPassword("");//??? LrhCustomer customer = lrhCustomerMapper.selectByAccountId(accountDto.getAccountId()); accountDto.setCustomerName(customer.getCustomerName()); accountDto.setIntroducerInviteCode(customer.getIntroducerInviteCode()); saveUserInfoWithToken(accountDto, accessToken); Map<String, Object> resultMap = new HashMap<>(); resultMap.put("accessToken", accessToken); resultMap.put("account", accountDto); return resultMap; } /** * ???IP10?24???? * @param mobile * @param ip * @throws Exception */ public void filterSmsCodeRequest(String mobile, String ip) throws Exception { // filterIp(ip); filterMobile(mobile); } /** * ???10?24???? * ???10624?10 * @param mobile * @throws Exception */ private void filterMobile(String mobile) throws Exception { // 1, 10 Integer mobileCount = redisTemplate.get(mobile + "_mobile", Integer.class); if (mobileCount != null && mobileCount >= 6) { throw new O2OServerException("????????"); } if (mobileCount == null) { mobileCount = 1; } else { mobileCount++; } redisTemplate.put(mobile + "_mobile", mobileCount, 10 * 60); // 2, 24? Integer totalCount = redisTemplate.get(mobile + "_totalCount", Integer.class); if (totalCount != null && totalCount >= 20) { throw new O2OServerException("????????"); } if (totalCount == null) { totalCount = 1; } else { totalCount++; } redisTemplate.put(mobile + "_totalCount", totalCount, 24 * 60 * 60); } /** * ?IP?10?24???? * ?IP?101024?100 * @param ip * @throws Exception */ private void filterIp(String ip) throws Exception { // 1, 10 Integer ipCount = redisTemplate.get(ip + "_ip", Integer.class); if (ipCount != null && ipCount >= 10) { throw new O2OServerException("????????"); } if (ipCount == null) { ipCount = 1; } else { ipCount++; } redisTemplate.put(ip + "_ip", ipCount, 10 * 60); // 2, 24? Integer totalCount = redisTemplate.get(ip + "_totalCount", Integer.class); if (totalCount != null && totalCount >= 100) { throw new O2OServerException("????????"); } if (totalCount == null) { totalCount = 1; } else { totalCount++; } redisTemplate.put(ip + "_totalCount", totalCount, 24 * 60 * 60); } }