后端生成二维码
This commit is contained in:
+19
-19
@@ -1,19 +1,19 @@
|
||||
/*
|
||||
* Copyright [2022] [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.dromara.maxkey.persistence.repository;
|
||||
|
||||
@@ -23,38 +23,38 @@ import java.util.List;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.apache.commons.lang3.ObjectUtils;
|
||||
import org.dromara.maxkey.entity.Institutions;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
|
||||
import com.alibaba.nacos.common.utils.CollectionUtils;
|
||||
import com.github.benmanes.caffeine.cache.Cache;
|
||||
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||
|
||||
public class InstitutionsRepository {
|
||||
static final Logger _logger = LoggerFactory.getLogger(InstitutionsRepository.class);
|
||||
|
||||
private static final String SELECT_STATEMENT =
|
||||
|
||||
private static final String SELECT_STATEMENT =
|
||||
"select * from mxk_institutions where id = ? or domain = ? or consoledomain = ?" ;
|
||||
|
||||
|
||||
private static final String DEFAULT_INSTID = "1";
|
||||
|
||||
protected static final Cache<String, Institutions> institutionsStore =
|
||||
protected static final Cache<String, Institutions> institutionsStore =
|
||||
Caffeine.newBuilder()
|
||||
.expireAfterWrite(60, TimeUnit.MINUTES)
|
||||
.build();
|
||||
|
||||
|
||||
//id domain mapping
|
||||
protected static final ConcurrentHashMap<String,String> mapper = new ConcurrentHashMap<>();
|
||||
|
||||
|
||||
protected JdbcTemplate jdbcTemplate;
|
||||
|
||||
|
||||
public InstitutionsRepository(JdbcTemplate jdbcTemplate) {
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
}
|
||||
|
||||
|
||||
public Institutions get(String instIdOrDomain) {
|
||||
_logger.trace(" instId {}" , instIdOrDomain);
|
||||
Institutions inst = getByInstIdOrDomain(instIdOrDomain);
|
||||
@@ -64,15 +64,15 @@ public class InstitutionsRepository {
|
||||
}
|
||||
return inst;
|
||||
}
|
||||
|
||||
|
||||
private Institutions getByInstIdOrDomain(String instIdOrDomain) {
|
||||
_logger.trace(" instId {}" , instIdOrDomain);
|
||||
Institutions inst = institutionsStore.getIfPresent(mapper.get(instIdOrDomain)==null ? DEFAULT_INSTID : mapper.get(instIdOrDomain) );
|
||||
if(inst == null) {
|
||||
List<Institutions> institutions =
|
||||
List<Institutions> institutions =
|
||||
jdbcTemplate.query(SELECT_STATEMENT,new InstitutionsRowMapper(),instIdOrDomain,instIdOrDomain,instIdOrDomain);
|
||||
|
||||
if (CollectionUtils.isNotEmpty(institutions)) {
|
||||
|
||||
if (ObjectUtils.isNotEmpty(institutions)) {
|
||||
inst = institutions.get(0);
|
||||
}
|
||||
if(inst != null ) {
|
||||
@@ -81,10 +81,10 @@ public class InstitutionsRepository {
|
||||
mapper.put(inst.getId(), inst.getDomain());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return inst;
|
||||
}
|
||||
|
||||
|
||||
public class InstitutionsRowMapper implements RowMapper<Institutions> {
|
||||
@Override
|
||||
public Institutions mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||
|
||||
+41
-42
@@ -1,19 +1,19 @@
|
||||
/*
|
||||
* 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.dromara.maxkey.persistence.repository;
|
||||
|
||||
@@ -24,13 +24,12 @@ import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.apache.commons.lang3.ObjectUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.dromara.maxkey.constants.ConstsRoles;
|
||||
import org.dromara.maxkey.constants.ConstsStatus;
|
||||
import org.dromara.maxkey.entity.idm.Groups;
|
||||
import org.dromara.maxkey.entity.idm.UserInfo;
|
||||
import org.dromara.maxkey.util.StrUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
@@ -57,28 +56,28 @@ public class LoginRepository {
|
||||
private static final String GROUPS_SELECT_STATEMENT = "select distinct g.id,g.groupcode,g.groupname from mxk_userinfo u,mxk_groups g,mxk_group_member gm where u.id = ? and u.id=gm.memberid and gm.groupid=g.id ";
|
||||
|
||||
private static final String DEFAULT_USERINFO_SELECT_STATEMENT = "select * from mxk_userinfo where username = ? ";
|
||||
|
||||
|
||||
private static final String DEFAULT_USERINFO_SELECT_STATEMENT_USERNAME_MOBILE = "select * from mxk_userinfo where (username = ? or mobile = ?)";
|
||||
|
||||
|
||||
private static final String DEFAULT_USERINFO_SELECT_STATEMENT_USERNAME_MOBILE_EMAIL = "select * from mxk_userinfo where (username = ? or mobile = ? or email = ?) ";
|
||||
|
||||
|
||||
private static final String DEFAULT_MYAPPS_SELECT_STATEMENT = "select distinct app.id,app.appname from mxk_apps app,mxk_access gp,mxk_groups g where app.id=gp.appid and app.status = 1 and gp.groupid=g.id and g.id in(%s)";
|
||||
|
||||
|
||||
protected JdbcTemplate jdbcTemplate;
|
||||
|
||||
|
||||
/**
|
||||
* 1 (USERNAME) 2 (USERNAME | MOBILE) 3 (USERNAME | MOBILE | EMAIL)
|
||||
*/
|
||||
public static int LOGIN_ATTRIBUTE_TYPE = 2;
|
||||
|
||||
|
||||
public LoginRepository(){
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
public LoginRepository(JdbcTemplate jdbcTemplate){
|
||||
this.jdbcTemplate=jdbcTemplate;
|
||||
}
|
||||
|
||||
|
||||
public UserInfo find(String username, String password) {
|
||||
List<UserInfo> listUserInfo = null ;
|
||||
if( LOGIN_ATTRIBUTE_TYPE == 1) {
|
||||
@@ -89,37 +88,37 @@ public class LoginRepository {
|
||||
listUserInfo = findByUsernameOrMobileOrEmail(username,password);
|
||||
}
|
||||
_logger.debug("load UserInfo : {}" , listUserInfo);
|
||||
return (CollectionUtils.isNotEmpty(listUserInfo))? listUserInfo.get(0) : null;
|
||||
return (ObjectUtils.isNotEmpty(listUserInfo))? listUserInfo.get(0) : null;
|
||||
}
|
||||
|
||||
|
||||
public List<UserInfo> findByUsername(String username, String password) {
|
||||
return jdbcTemplate.query(
|
||||
DEFAULT_USERINFO_SELECT_STATEMENT,
|
||||
DEFAULT_USERINFO_SELECT_STATEMENT,
|
||||
new UserInfoRowMapper(),
|
||||
username
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
public List<UserInfo> findByUsernameOrMobile(String username, String password) {
|
||||
return jdbcTemplate.query(
|
||||
DEFAULT_USERINFO_SELECT_STATEMENT_USERNAME_MOBILE,
|
||||
DEFAULT_USERINFO_SELECT_STATEMENT_USERNAME_MOBILE,
|
||||
new UserInfoRowMapper(),
|
||||
username,username
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
public List<UserInfo> findByUsernameOrMobileOrEmail(String username, String password) {
|
||||
return jdbcTemplate.query(
|
||||
DEFAULT_USERINFO_SELECT_STATEMENT_USERNAME_MOBILE_EMAIL,
|
||||
DEFAULT_USERINFO_SELECT_STATEMENT_USERNAME_MOBILE_EMAIL,
|
||||
new UserInfoRowMapper(),
|
||||
username,username,username
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 閿佸畾鐢ㄦ埛锛歩slock锛�1 鐢ㄦ埛瑙i攣 2 鐢ㄦ埛閿佸畾
|
||||
*
|
||||
*
|
||||
* @param userInfo
|
||||
*/
|
||||
public void updateLock(UserInfo userInfo) {
|
||||
@@ -137,7 +136,7 @@ public class LoginRepository {
|
||||
|
||||
/**
|
||||
* 閿佸畾鐢ㄦ埛锛歩slock锛�1 鐢ㄦ埛瑙i攣 2 鐢ㄦ埛閿佸畾
|
||||
*
|
||||
*
|
||||
* @param userInfo
|
||||
*/
|
||||
public void updateUnlock(UserInfo userInfo) {
|
||||
@@ -155,7 +154,7 @@ public class LoginRepository {
|
||||
|
||||
/**
|
||||
* reset BadPasswordCount And Lockout
|
||||
*
|
||||
*
|
||||
* @param userInfo
|
||||
*/
|
||||
public void updateLockout(UserInfo userInfo) {
|
||||
@@ -173,7 +172,7 @@ public class LoginRepository {
|
||||
|
||||
/**
|
||||
* if login password is error ,BadPasswordCount++ and set bad date
|
||||
*
|
||||
*
|
||||
* @param userInfo
|
||||
*/
|
||||
public void updateBadPasswordCount(UserInfo userInfo) {
|
||||
@@ -190,15 +189,15 @@ public class LoginRepository {
|
||||
_logger.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public List<GrantedAuthority> queryAuthorizedApps(List<GrantedAuthority> grantedAuthoritys) {
|
||||
String grantedAuthorityString="'ROLE_ALL_USER'";
|
||||
for(GrantedAuthority grantedAuthority : grantedAuthoritys) {
|
||||
grantedAuthorityString += ",'"+ grantedAuthority.getAuthority()+"'";
|
||||
}
|
||||
|
||||
|
||||
ArrayList<GrantedAuthority> listAuthorizedApps = (ArrayList<GrantedAuthority>) jdbcTemplate.query(
|
||||
String.format(DEFAULT_MYAPPS_SELECT_STATEMENT, grantedAuthorityString),
|
||||
String.format(DEFAULT_MYAPPS_SELECT_STATEMENT, grantedAuthorityString),
|
||||
new RowMapper<GrantedAuthority>() {
|
||||
public GrantedAuthority mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||
return new SimpleGrantedAuthority(rs.getString("id"));
|
||||
@@ -208,7 +207,7 @@ public class LoginRepository {
|
||||
_logger.debug("list Authorized Apps {}" , listAuthorizedApps);
|
||||
return listAuthorizedApps;
|
||||
}
|
||||
|
||||
|
||||
public List<Groups> queryGroups(UserInfo userInfo) {
|
||||
List<Groups> listRoles = jdbcTemplate.query(GROUPS_SELECT_STATEMENT, new RowMapper<Groups>() {
|
||||
public Groups mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||
@@ -222,7 +221,7 @@ public class LoginRepository {
|
||||
|
||||
/**
|
||||
* grant Authority by userinfo
|
||||
*
|
||||
*
|
||||
* @param userInfo
|
||||
* @return ArrayList<GrantedAuthority>
|
||||
*/
|
||||
@@ -237,7 +236,7 @@ public class LoginRepository {
|
||||
grantedAuthority.add(ConstsRoles.ROLE_ORDINARY_USER);
|
||||
for (Groups group : listGroups) {
|
||||
grantedAuthority.add(new SimpleGrantedAuthority(group.getId()));
|
||||
if(group.getGroupCode().startsWith("ROLE_")
|
||||
if(group.getGroupCode().startsWith("ROLE_")
|
||||
&& !grantedAuthority.contains(new SimpleGrantedAuthority(group.getGroupCode()))) {
|
||||
grantedAuthority.add(new SimpleGrantedAuthority(group.getGroupCode()));
|
||||
}
|
||||
@@ -246,19 +245,19 @@ public class LoginRepository {
|
||||
|
||||
return grantedAuthority;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public void updateLastLogin(UserInfo userInfo) {
|
||||
jdbcTemplate.update(LOGIN_USERINFO_UPDATE_STATEMENT,
|
||||
new Object[] {
|
||||
userInfo.getLastLoginTime(),
|
||||
userInfo.getLastLoginIp(),
|
||||
userInfo.getLoginCount() + 1,
|
||||
userInfo.getId()
|
||||
new Object[] {
|
||||
userInfo.getLastLoginTime(),
|
||||
userInfo.getLastLoginIp(),
|
||||
userInfo.getLoginCount() + 1,
|
||||
userInfo.getId()
|
||||
},
|
||||
new int[] { Types.TIMESTAMP, Types.VARCHAR, Types.INTEGER, Types.VARCHAR });
|
||||
}
|
||||
|
||||
|
||||
public class UserInfoRowMapper implements RowMapper<UserInfo> {
|
||||
@Override
|
||||
public UserInfo mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||
@@ -372,7 +371,7 @@ public class LoginRepository {
|
||||
if (userInfo.getTheme() == null || userInfo.getTheme().equalsIgnoreCase("")) {
|
||||
userInfo.setTheme("default");
|
||||
}
|
||||
|
||||
|
||||
return userInfo;
|
||||
}
|
||||
}
|
||||
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
package org.dromara.maxkey.persistence.service;
|
||||
|
||||
import org.dromara.maxkey.persistence.cache.MomentaryService;
|
||||
import org.dromara.maxkey.util.IdGenerator;
|
||||
import org.dromara.maxkey.util.ObjectTransformer;
|
||||
import org.dromara.mybatis.jpa.id.IdentifierGenerator;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
/**
|
||||
* @description:
|
||||
* @author: orangeBabu
|
||||
* @time: 15/8/2024 AM9:49
|
||||
*/
|
||||
|
||||
@Repository
|
||||
public class ScanCodeService {
|
||||
private static final Logger _logger = LoggerFactory.getLogger(ScanCodeService.class);
|
||||
|
||||
static final String SCANCODE_TICKET = "login:scancode:%s";
|
||||
|
||||
static final String SCANCODE_CONFIRM = "login:scancode:confirm:%s";
|
||||
|
||||
public static class STATE{
|
||||
public static final String SCANED = "scaned";
|
||||
public static final String CONFIRMED = "confirmed";
|
||||
public static final String CANCELED = "canceled";
|
||||
|
||||
public static final String CANCEL = "cancel";
|
||||
public static final String CONFIRM = "confirm";
|
||||
}
|
||||
|
||||
int validitySeconds = 60 * 3; //default 3 minutes.
|
||||
|
||||
int cancelValiditySeconds = 60 * 1; //default 1 minutes.
|
||||
|
||||
|
||||
@Autowired
|
||||
IdGenerator idGenerator;
|
||||
|
||||
@Autowired
|
||||
MomentaryService momentaryService;
|
||||
|
||||
private String getKey(Long ticket) {
|
||||
return SCANCODE_TICKET.formatted(ticket);
|
||||
}
|
||||
|
||||
private String getConfirmKey(Long sessionId) {
|
||||
return SCANCODE_CONFIRM.formatted(sessionId);
|
||||
}
|
||||
|
||||
public Long createTicket() {
|
||||
Long ticket = 0L;
|
||||
ticket = Long.parseLong(idGenerator.generate());
|
||||
momentaryService.put(getKey(ticket), "ORCode", Duration.ofSeconds(validitySeconds));
|
||||
_logger.info("Ticket {} , Duration {}", ticket , Duration.ofSeconds(validitySeconds));
|
||||
return ticket;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user