123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- /*
- * Copyright [2020] [MaxKey of copyright http://www.maxkey.top]
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
- package org.maxkey.persistence.service;
- import org.apache.mybatis.jpa.persistence.JpaBaseService;
- import org.maxkey.constants.ConstantsStatus;
- import org.maxkey.crypto.ReciprocalUtils;
- import org.maxkey.crypto.password.PasswordReciprocal;
- import org.maxkey.domain.ChangePassword;
- import org.maxkey.domain.UserInfo;
- import org.maxkey.identity.kafka.KafkaIdentityAction;
- import org.maxkey.identity.kafka.KafkaIdentityTopic;
- import org.maxkey.identity.kafka.KafkaProvisioningService;
- import org.maxkey.persistence.db.PasswordPolicyValidator;
- import org.maxkey.persistence.mapper.UserInfoMapper;
- import org.maxkey.util.DateUtils;
- import org.maxkey.util.StringUtils;
- import org.maxkey.web.WebContext;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.security.crypto.password.PasswordEncoder;
- import org.springframework.stereotype.Service;
- /**
- * @author Crystal.Sea
- *
- */
- @Service
- public class UserInfoService extends JpaBaseService<UserInfo> {
- final static Logger _logger = LoggerFactory.getLogger(UserInfoService.class);
-
- @Autowired
- private PasswordEncoder passwordEncoder;
-
- @Autowired
- PasswordPolicyValidator passwordPolicyValidator;
-
- @Autowired
- KafkaProvisioningService kafkaProvisioningService;
-
- public UserInfoService() {
- super(UserInfoMapper.class);
- }
- /* (non-Javadoc)
- * @see com.connsec.db.service.BaseService#getMapper()
- */
- @Override
- public UserInfoMapper getMapper() {
- // TODO Auto-generated method stub
- return (UserInfoMapper)super.getMapper();
- }
-
- public boolean insert(UserInfo userInfo) {
- userInfo = passwordEncoder(userInfo);
- if (super.insert(userInfo)) {
- kafkaProvisioningService.send(
- KafkaIdentityTopic.USERINFO_TOPIC,
- userInfo,
- KafkaIdentityAction.CREATE_ACTION);
- return true;
- }
- return false;
- }
-
- public boolean update(UserInfo userInfo) {
- userInfo = passwordEncoder(userInfo);
- if (super.update(userInfo)) {
- kafkaProvisioningService.send(
- KafkaIdentityTopic.USERINFO_TOPIC,
- userInfo,
- KafkaIdentityAction.UPDATE_ACTION);
-
- changePasswordProvisioning(userInfo);
- return true;
- }
- return false;
- }
-
- public boolean delete(UserInfo userInfo) {
- if( super.delete(userInfo)){
- kafkaProvisioningService.send(
- KafkaIdentityTopic.USERINFO_TOPIC,
- userInfo,
- KafkaIdentityAction.DELETE_ACTION);
- return true;
- }
- return false;
- }
- public boolean updateProtectedApps(UserInfo userinfo) {
- try {
- if(WebContext.getUserInfo() != null) {
- userinfo.setModifiedBy(WebContext.getUserInfo().getId());
- }
- userinfo.setModifiedDate(DateUtils.getCurrentDateTimeAsString());
- return getMapper().updateProtectedApps(userinfo) > 0;
- } catch (Exception e) {
- e.printStackTrace();
- }
- return false;
- }
- public UserInfo loadByUsername(String username) {
- return getMapper().loadByUsername(username);
- }
-
- public UserInfo loadByAppIdAndUsername(String appId,String username){
- try {
- UserInfo userinfo = new UserInfo();
- userinfo.setUsername(username);
- return getMapper().loadByAppIdAndUsername(userinfo) ;
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
-
- public void logisticDeleteAllByCid(String cid){
- try {
- getMapper().logisticDeleteAllByCid(cid);
- } catch(Exception e) {
- e.printStackTrace();
- }
- }
-
- public UserInfo passwordEncoder(UserInfo userInfo) {
- //密码不为空,则需要进行加密处理
- if(userInfo.getPassword()!=null && !userInfo.getPassword().equals("")) {
- String password = passwordEncoder.encode(userInfo.getPassword());
- userInfo.setDecipherable(ReciprocalUtils.encode(PasswordReciprocal.getInstance().rawPassword(userInfo.getUsername(), userInfo.getPassword())));
- _logger.debug("decipherable : "+userInfo.getDecipherable());
- userInfo.setPassword(password);
- userInfo.setPasswordLastSetTime(DateUtils.getCurrentDateTimeAsString());
-
- userInfo.setModifiedDate(DateUtils.getCurrentDateTimeAsString());
- }
- return userInfo;
- }
-
-
- public boolean changePassword(UserInfo userInfo) {
- try {
-
- passwordPolicyValidator.validator(userInfo);
-
- if(WebContext.getUserInfo() != null) {
- userInfo.setModifiedBy(WebContext.getUserInfo().getId());
-
- }
- userInfo = passwordEncoder(userInfo);
-
- if(getMapper().changePassword(userInfo) > 0){
- changePasswordProvisioning(userInfo);
- return true;
- }
- return false;
- } catch (Exception e) {
- e.printStackTrace();
- }
- return false;
- }
-
- public void changePasswordProvisioning(UserInfo userInfo) {
- if(userInfo.getPassword()!=null && !userInfo.getPassword().equals("")) {
- ChangePassword changePassword=new ChangePassword();
- changePassword.setId(userInfo.getId());
- changePassword.setUid(userInfo.getId());
- changePassword.setUsername(userInfo.getUsername());
- changePassword.setDecipherable(userInfo.getDecipherable());
- changePassword.setPassword(userInfo.getPassword());
- kafkaProvisioningService.send(
- KafkaIdentityTopic.PASSWORD_TOPIC,
- changePassword,
- KafkaIdentityAction.PASSWORD_ACTION);
- }
- }
-
- public boolean changeAppLoginPassword(UserInfo userinfo) {
- try {
- if(WebContext.getUserInfo() != null) {
- userinfo.setModifiedBy(WebContext.getUserInfo().getId());
- }
- userinfo.setModifiedDate(DateUtils.getCurrentDateTimeAsString());
- return getMapper().changeAppLoginPassword(userinfo) > 0;
- } catch (Exception e) {
- e.printStackTrace();
- }
- return false;
- }
-
-
- /**
- * 锁定用户:islock:1 用户解锁 2 用户锁定
- * @param userInfo
- */
- public void locked(UserInfo userInfo) {
- try {
- if(userInfo != null && StringUtils.isNotEmpty(userInfo.getId())) {
- userInfo.setIsLocked(ConstantsStatus.STOP);
- getMapper().locked(userInfo);
- }
- } catch(Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * 用户登录成功后,重置错误密码次数和解锁用户
- * @param userInfo
- */
- public void unlock(UserInfo userInfo) {
- try {
- if(userInfo != null && StringUtils.isNotEmpty(userInfo.getId())) {
- userInfo.setIsLocked(ConstantsStatus.START);
- userInfo.setBadPasswordCount(0);
- getMapper().unlock(userInfo);
- }
- } catch(Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * 更新错误密码次数
- * @param userInfo
- */
- public void updateBadPasswordCount(UserInfo userInfo) {
- try {
- if(userInfo != null && StringUtils.isNotEmpty(userInfo.getId())) {
- int updateBadPWDCount = userInfo.getBadPasswordCount() + 1;
- userInfo.setBadPasswordCount(updateBadPWDCount);
- getMapper().updateBadPWDCount(userInfo);
- }
- } catch(Exception e) {
- e.printStackTrace();
- }
- }
-
- public boolean changeSharedSecret(UserInfo userInfo){
- return getMapper().changeSharedSecret(userInfo)>0;
- }
-
- public boolean changePasswordQuestion(UserInfo userInfo){
- return getMapper().changePasswordQuestion(userInfo)>0;
- }
-
- public boolean changeAuthnType(UserInfo userInfo){
- return getMapper().changeAuthnType(userInfo)>0;
- }
-
- public boolean changeEmail(UserInfo userInfo){
- return getMapper().changeEmail(userInfo)>0;
- }
-
- public boolean changeMobile(UserInfo userInfo){
- return getMapper().changeMobile(userInfo)>0;
- }
-
- public UserInfo queryUserInfoByEmailMobile(String emailMobile) {
- return getMapper().queryUserInfoByEmailMobile(emailMobile);
- }
-
- public int updateProfile(UserInfo userInfo){
-
- return getMapper().updateProfile(userInfo);
- }
- public void setPasswordPolicyValidator(PasswordPolicyValidator passwordPolicyValidator) {
- this.passwordPolicyValidator = passwordPolicyValidator;
- }
- }
|