新增部分登录实现

This commit is contained in:
aoshiguchen
2022-08-01 23:50:46 +08:00
parent c287889a82
commit b0e3a3454e
15 changed files with 408 additions and 21 deletions
@@ -213,6 +213,7 @@ public class SimpleBeanFactory extends AbstractBeanFactory {
BeanWrapper factoryBean = bean.getFactoryBean();
if (!factoryBean.hasInstance()) {
newInstance(factoryBean);
inject(factoryBean);
}
if (bean.getInstantiationMethod().getParameters().length == 0) {
bean.setInstance(bean.getInstantiationMethod().invoke(factoryBean.getInstance()));
@@ -26,10 +26,7 @@ import fun.asgc.neutrino.core.annotation.Configuration;
import fun.asgc.neutrino.core.annotation.Value;
import fun.asgc.neutrino.core.constant.MetaDataConstant;
import fun.asgc.neutrino.core.exception.ConfigurationParserException;
import fun.asgc.neutrino.core.util.CollectionUtil;
import fun.asgc.neutrino.core.util.FileUtil;
import fun.asgc.neutrino.core.util.ReflectUtil;
import fun.asgc.neutrino.core.util.StringUtil;
import fun.asgc.neutrino.core.util.*;
import org.apache.commons.lang3.StringUtils;
import java.io.FileNotFoundException;
@@ -50,12 +47,20 @@ public abstract class AbstractConfigurationParser implements ConfigurationParser
@Override
public <T> T parse(InputStream in, Class<T> clazz) throws ConfigurationParserException {
return parse(in, clazz, null);
}
public <T> T parse(InputStream in, Class<T> clazz, Object obj) throws ConfigurationParserException {
Map<String, Object> config = parse2Map(in);
return parse(config, clazz);
return parse(config, clazz, obj);
}
@Override
public <T> T parse(Class<T> clazz) throws ConfigurationParserException {
return parse(clazz, null);
}
public <T> T parse(Class<T> clazz, Object obj) throws ConfigurationParserException {
String fileName = defaultFileName();
Configuration configuration = clazz.getAnnotation(Configuration.class);
if (null != configuration && StringUtils.isNotEmpty(configuration.file())) {
@@ -64,21 +69,31 @@ public abstract class AbstractConfigurationParser implements ConfigurationParser
try {
InputStream in = FileUtil.getInputStream(MetaDataConstant.CLASSPATH_RESOURCE_IDENTIFIER.concat("/").concat(fileName));
return parse(in, clazz);
return parse(in, clazz, obj);
} catch (FileNotFoundException e) {
throw new ConfigurationParserException(String.format("配置文件[%s]不存在", fileName));
}
}
@Override
public <T> T parse(Map<String, Object> config, Class<T> clazz) throws ConfigurationParserException {
return parseProxy(config, clazz);
public void parse(Object obj) throws ConfigurationParserException {
Assert.notNull(obj, "对象不能为空");
parse(obj.getClass(), obj);
}
private <T> T parseProxy(Map<String, Object> config, Class<?> clazz) throws ConfigurationParserException {
@Override
public <T> T parse(Map<String, Object> config, Class<T> clazz) throws ConfigurationParserException {
return parse(config, clazz, null);
}
public <T> T parse(Map<String, Object> config, Class<T> clazz, Object obj) throws ConfigurationParserException {
return parseProxy(config, clazz, obj);
}
private <T> T parseProxy(Map<String, Object> config, Class<?> clazz, Object obj) throws ConfigurationParserException {
T instance = null;
try {
instance = (T)clazz.newInstance();
instance = (null != obj) ? (T)obj : (T)clazz.newInstance();
} catch (InstantiationException e) {
throw new ConfigurationParserException(String.format("无法实例化类:%s", clazz.getName()));
} catch (IllegalAccessException e) {
@@ -129,7 +144,7 @@ public abstract class AbstractConfigurationParser implements ConfigurationParser
boolean success = ReflectUtil.setFieldValue(field, instance, fieldValue);
if (!success) {
try {
Object o = parseProxy((Map)config.get(key), field.getType());
Object o = parseProxy((Map)config.get(key), field.getType(), null);
if (null != o) {
ReflectUtil.setFieldValue(field, instance, o);
}
@@ -51,6 +51,13 @@ public interface ConfigurationParser {
*/
<T> T parse(Class<T> clazz) throws ConfigurationParserException;
/**
* 解析
* @param obj
* @throws ConfigurationParserException
*/
void parse(Object obj) throws ConfigurationParserException;
/**
* 解析
* @param config
@@ -49,8 +49,6 @@ import java.util.Map;
@Component
public class SqlMapperInterceptor implements Interceptor {
@Autowired
private JdbcTemplate jdbcTemplate;
private static final Cache<Method, Params> paramsCache = new MemoryCache<>();
@Init
@@ -60,6 +58,7 @@ public class SqlMapperInterceptor implements Interceptor {
@Override
public void intercept(Invocation inv) throws Exception {
JdbcTemplate jdbcTemplate = BeanManager.getBean(JdbcTemplate.class);
Assert.notNull(jdbcTemplate, "JdbcTemplate未注入,调用失败!");
Params params = getParams(inv.getTargetMethod());
if (null == params) {
@@ -38,4 +38,8 @@ public class ConfigUtil {
return ymlParser.parse(clazz);
}
public static <T> T getYmlConfig(Object obj) {
ymlParser.parse(obj);
return (T)obj;
}
}
@@ -36,10 +36,6 @@ public class Test1 implements ApplicationRunner {
@Autowired
private TestGlobalExceptionHandler testGlobalExceptionHandler;
public Test1() {
System.out.println("aaa");
}
@Init
public void init() {
log.info("初始化,注册全局异常拦截器{}...", testGlobalExceptionHandler.hashCode());
@@ -0,0 +1,55 @@
/**
* Copyright (c) 2022 aoshiguchen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package fun.asgc.neutrino.proxy.server.base.rest.config;
import com.alibaba.druid.pool.DruidDataSource;
import fun.asgc.neutrino.core.annotation.*;
import fun.asgc.neutrino.core.base.Ordered;
import fun.asgc.neutrino.core.db.template.JdbcTemplate;
import javax.sql.DataSource;
/**
* 系统配置
*author: aoshiguchen
* @date: 2022/8/1
*/
@Order(Ordered.HIGHEST_PRECEDENCE)
@Component
public class SystemConfiguration {
@Autowired
private SqliteConfig sqliteConfig;
@Bean
public DataSource dataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl(sqliteConfig.getUrl());
dataSource.setDriverClassName(sqliteConfig.getDriverClass());
return dataSource;
}
@Bean
public JdbcTemplate jdbcTemplate() {
return new JdbcTemplate(dataSource());
}
}
@@ -21,6 +21,7 @@
*/
package fun.asgc.neutrino.proxy.server.controller;
import fun.asgc.neutrino.core.annotation.Autowired;
import fun.asgc.neutrino.core.annotation.NonIntercept;
import fun.asgc.neutrino.core.web.annotation.PostMapping;
import fun.asgc.neutrino.core.web.annotation.RequestBody;
@@ -28,8 +29,15 @@ import fun.asgc.neutrino.core.web.annotation.RequestMapping;
import fun.asgc.neutrino.core.web.annotation.RestController;
import fun.asgc.neutrino.proxy.server.controller.req.LoginReq;
import fun.asgc.neutrino.proxy.server.controller.res.LoginRes;
import fun.asgc.neutrino.proxy.server.dal.entity.UserDO;
import fun.asgc.neutrino.proxy.server.dal.entity.UserTokenDO;
import fun.asgc.neutrino.proxy.server.service.UserService;
import fun.asgc.neutrino.proxy.server.util.Md5Util;
import fun.asgc.neutrino.proxy.server.util.ParamCheckUtil;
import java.util.Date;
import java.util.UUID;
/**
*
* @author: aoshiguchen
@@ -39,16 +47,34 @@ import fun.asgc.neutrino.proxy.server.util.ParamCheckUtil;
@RequestMapping
@RestController
public class IndexController {
@Autowired
private UserService userService;
@PostMapping("login")
public LoginRes login(@RequestBody LoginReq req) {
ParamCheckUtil.checkNotEmpty(req.getLoginName(), "loginName");
ParamCheckUtil.checkNotEmpty(req.getLoginPassword(), "loginPassword");
UserDO userDO = userService.findByLoginName(req.getLoginName());
if (null == userDO || !Md5Util.encode(req.getLoginPassword()).equals(userDO.getLoginPassword())) {
// TODO 抛出异常
}
String token = UUID.randomUUID().toString().replaceAll("-", "");
Date now = new Date();
// TODO 计算过期时间
userService.addUserToken(new UserTokenDO()
.setToken(token)
.setUserId(userDO.getId())
.setExpirationTime(now)
.setCreateTime(now)
.setUpdateTime(now)
);
return new LoginRes()
.setToken("1111")
.setUserId(1)
.setUserName("张三");
.setToken(token)
.setUserId(userDO.getId())
.setUserName(userDO.getName());
}
}
@@ -0,0 +1,45 @@
/**
* Copyright (c) 2022 aoshiguchen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package fun.asgc.neutrino.proxy.server.dal;
import fun.asgc.neutrino.core.annotation.Component;
import fun.asgc.neutrino.core.db.annotation.Select;
import fun.asgc.neutrino.core.db.mapper.SqlMapper;
import fun.asgc.neutrino.proxy.server.dal.entity.UserDO;
/**
*
* @author: aoshiguchen
* @date: 2022/8/1
*/
@Component
public interface UserMapper extends SqlMapper {
/**
* 根据登录名查询用户记录
* @param loginName
* @return
*/
@Select("select * from user where login_name = ?")
UserDO findByLoginName(String loginName);
}
@@ -0,0 +1,43 @@
/**
* Copyright (c) 2022 aoshiguchen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package fun.asgc.neutrino.proxy.server.dal;
import fun.asgc.neutrino.core.annotation.Component;
import fun.asgc.neutrino.core.db.annotation.Insert;
import fun.asgc.neutrino.core.db.mapper.SqlMapper;
import fun.asgc.neutrino.proxy.server.dal.entity.UserTokenDO;
/**
*
* @author: aoshiguchen
* @date: 2022/8/1
*/
@Component
public interface UserTokenMapper extends SqlMapper {
/**
* 新增用户token
* @param userToken
* @return
*/
@Insert("insert into `user_token`(`token`,`user_id`,`expiration_time`,`create_time`,`update_time`) values (:token,:userId,:expirationTime,:createTime,:updateTime)")
int add(UserTokenDO userToken);
}
@@ -0,0 +1,62 @@
/**
* Copyright (c) 2022 aoshiguchen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package fun.asgc.neutrino.proxy.server.dal.entity;
import fun.asgc.neutrino.core.db.annotation.Id;
import fun.asgc.neutrino.core.db.annotation.Table;
import lombok.Data;
import lombok.ToString;
import java.util.Date;
/**
*
* @author: aoshiguchen
* @date: 2022/8/1
*/
@ToString
@Data
@Table("user")
public class UserDO {
@Id
private Integer id;
/**
* 用户名
*/
private String name;
/**
* 登录名
*/
private String loginName;
/**
* 登录密码
*/
private String loginPassword;
/**
* 创建时间
*/
private Date createTime;
/**
* 更新时间
*/
private Date updateTime;
}
@@ -0,0 +1,66 @@
/**
* Copyright (c) 2022 aoshiguchen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package fun.asgc.neutrino.proxy.server.dal.entity;
import fun.asgc.neutrino.core.db.annotation.Id;
import fun.asgc.neutrino.core.db.annotation.Table;
import lombok.Data;
import lombok.ToString;
import lombok.experimental.Accessors;
import java.util.Date;
/**
*
* @author: aoshiguchen
* @date: 2022/8/1
*/
@ToString
@Accessors(chain = true)
@Data
@Table("user_token")
public class UserTokenDO {
@Id
private Integer id;
/**
* token
*/
private String token;
/**
* 用户ID
*/
private Integer userId;
/**
* 过期时间
*/
private Date expirationTime;
/**
* 创建时间
*/
private Date createTime;
/**
* 更新时间
*/
private Date updateTime;
}
@@ -21,7 +21,12 @@
*/
package fun.asgc.neutrino.proxy.server.service;
import fun.asgc.neutrino.core.annotation.Autowired;
import fun.asgc.neutrino.core.annotation.Component;
import fun.asgc.neutrino.proxy.server.dal.UserMapper;
import fun.asgc.neutrino.proxy.server.dal.UserTokenMapper;
import fun.asgc.neutrino.proxy.server.dal.entity.UserDO;
import fun.asgc.neutrino.proxy.server.dal.entity.UserTokenDO;
/**
*
@@ -30,5 +35,18 @@ import fun.asgc.neutrino.core.annotation.Component;
*/
@Component
public class UserService {
@Autowired
private UserMapper userMapper;
@Autowired
private UserTokenMapper userTokenMapper;
public UserDO findByLoginName(String loginName) {
return userMapper.findByLoginName(loginName);
}
public void addUserToken(UserTokenDO userTokenDO) {
userTokenMapper.add(userTokenDO);
}
}
@@ -0,0 +1,50 @@
/**
* Copyright (c) 2022 aoshiguchen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package fun.asgc.neutrino.proxy.server.util;
import java.math.BigInteger;
import java.security.MessageDigest;
/**
*
* @author: aoshiguchen
* @date: 2022/8/1
*/
public class Md5Util {
/**
* md5加密
* @param data
* @return
*/
public static String encode(String data) {
byte[] digest = null;
try {
MessageDigest md5 = MessageDigest.getInstance("md5");
digest = md5.digest(data.getBytes("utf-8"));
return new BigInteger(1, digest).toString(16);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
@@ -1,3 +1,3 @@
#
insert into `user`(`id`, `name`,`login_name`,`login_password`,`create_time`, `update_time`) values
(1, '管理员', 'admin', '123456', datetime('now', 'localtime'), datetime('now', 'localtime'));
(1, '管理员', 'admin', 'e10adc3949ba59abbe56e057f20f883e', datetime('now', 'localtime'), datetime('now', 'localtime'));