13 changed files with 30 additions and 472 deletions
@ -1,67 +0,0 @@ |
|||
/** |
|||
* Copyright (c) 2018 人人开源 All rights reserved. |
|||
* <p> |
|||
* https://www.renren.io
|
|||
* <p> |
|||
* 版权所有,侵权必究! |
|||
*/ |
|||
|
|||
package com.elink.esua.epdc.controller; |
|||
|
|||
|
|||
import com.elink.esua.epdc.common.token.annotation.Login; |
|||
import com.elink.esua.epdc.commons.tools.utils.Result; |
|||
import com.elink.esua.epdc.commons.tools.validator.ValidatorUtils; |
|||
import com.elink.esua.epdc.dto.LoginDTO; |
|||
import com.elink.esua.epdc.service.TokenService; |
|||
import com.elink.esua.epdc.service.UserService; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.web.bind.annotation.*; |
|||
|
|||
import java.util.Map; |
|||
|
|||
/** |
|||
* 登录接口 |
|||
* |
|||
* @author Mark sunlightcs@gmail.com |
|||
*/ |
|||
@RestController |
|||
@RequestMapping("auth") |
|||
public class ApiLoginController { |
|||
@Autowired |
|||
private UserService userService; |
|||
@Autowired |
|||
private TokenService tokenService; |
|||
|
|||
|
|||
/** |
|||
* 登录 |
|||
* |
|||
* @param dto |
|||
* @return |
|||
*/ |
|||
@PostMapping("login") |
|||
public Result<Map<String, Object>> login(@RequestBody LoginDTO dto) { |
|||
//表单校验
|
|||
ValidatorUtils.validateEntity(dto); |
|||
|
|||
//用户登录
|
|||
Map<String, Object> map = userService.login(dto); |
|||
|
|||
return new Result().ok(map); |
|||
} |
|||
|
|||
/** |
|||
* 退出 |
|||
* |
|||
* @param userId |
|||
* @return |
|||
*/ |
|||
@Login |
|||
@PostMapping("logout") |
|||
public Result logout(@RequestAttribute("userId") Long userId) { |
|||
tokenService.expireToken(userId); |
|||
return new Result(); |
|||
} |
|||
|
|||
} |
|||
@ -1,67 +0,0 @@ |
|||
/** |
|||
* Copyright (c) 2018 人人开源 All rights reserved. |
|||
* <p> |
|||
* https://www.renren.io
|
|||
* <p> |
|||
* 版权所有,侵权必究! |
|||
*/ |
|||
|
|||
package com.elink.esua.epdc.controller; |
|||
|
|||
import com.elink.esua.epdc.commons.tools.utils.Result; |
|||
import com.elink.esua.epdc.commons.tools.validator.ValidatorUtils; |
|||
import com.elink.esua.epdc.dto.RegisterDTO; |
|||
import com.elink.esua.epdc.entity.UserEntity; |
|||
import com.elink.esua.epdc.service.UserService; |
|||
import org.apache.commons.codec.digest.DigestUtils; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.web.bind.annotation.PostMapping; |
|||
import org.springframework.web.bind.annotation.RequestBody; |
|||
import org.springframework.web.bind.annotation.RequestMapping; |
|||
import org.springframework.web.bind.annotation.RestController; |
|||
|
|||
import java.util.Date; |
|||
|
|||
/** |
|||
* 注册接口 |
|||
* |
|||
* @author Mark sunlightcs@gmail.com |
|||
*/ |
|||
@RestController |
|||
@RequestMapping("register") |
|||
public class ApiRegisterController { |
|||
@Autowired |
|||
private UserService userService; |
|||
|
|||
/** |
|||
* 注册 |
|||
* |
|||
* @param dto |
|||
* @return |
|||
*/ |
|||
@PostMapping |
|||
public Result register(@RequestBody RegisterDTO dto) { |
|||
//表单校验
|
|||
ValidatorUtils.validateEntity(dto); |
|||
|
|||
UserEntity user = new UserEntity(); |
|||
user.setMobile(dto.getMobile()); |
|||
user.setUsername(dto.getMobile()); |
|||
user.setPassword(DigestUtils.sha256Hex(dto.getPassword())); |
|||
user.setCreateDate(new Date()); |
|||
userService.insert(user); |
|||
|
|||
return new Result(); |
|||
} |
|||
|
|||
/** |
|||
* 注册 |
|||
* |
|||
* @param dto |
|||
* @return |
|||
*/ |
|||
@PostMapping("register2") |
|||
public Result register2(@RequestBody RegisterDTO dto) { |
|||
return userService.register2(dto); |
|||
} |
|||
} |
|||
@ -1,25 +0,0 @@ |
|||
/** |
|||
* Copyright (c) 2018 人人开源 All rights reserved. |
|||
* |
|||
* https://www.renren.io
|
|||
* |
|||
* 版权所有,侵权必究! |
|||
*/ |
|||
|
|||
package com.elink.esua.epdc.dao; |
|||
|
|||
import com.elink.esua.epdc.commons.mybatis.dao.BaseDao; |
|||
import com.elink.esua.epdc.entity.TokenEntity; |
|||
import org.apache.ibatis.annotations.Mapper; |
|||
|
|||
/** |
|||
* 用户Token |
|||
* |
|||
* @author Mark sunlightcs@gmail.com |
|||
*/ |
|||
@Mapper |
|||
public interface TokenDao extends BaseDao<TokenEntity> { |
|||
TokenEntity getByToken(String token); |
|||
|
|||
TokenEntity getByUserId(Long userId); |
|||
} |
|||
@ -1,25 +0,0 @@ |
|||
/** |
|||
* Copyright (c) 2018 人人开源 All rights reserved. |
|||
* |
|||
* https://www.renren.io
|
|||
* |
|||
* 版权所有,侵权必究! |
|||
*/ |
|||
|
|||
package com.elink.esua.epdc.dao; |
|||
|
|||
import com.elink.esua.epdc.commons.mybatis.dao.BaseDao; |
|||
import com.elink.esua.epdc.entity.UserEntity; |
|||
import org.apache.ibatis.annotations.Mapper; |
|||
|
|||
/** |
|||
* 用户 |
|||
* |
|||
* @author Mark sunlightcs@gmail.com |
|||
*/ |
|||
@Mapper |
|||
public interface UserDao extends BaseDao<UserEntity> { |
|||
UserEntity getUserByMobile(String mobile); |
|||
|
|||
UserEntity getUserByUserId(Long userId); |
|||
} |
|||
@ -1,36 +0,0 @@ |
|||
/** |
|||
* Copyright (c) 2018 人人开源 All rights reserved. |
|||
* |
|||
* https://www.renren.io
|
|||
* |
|||
* 版权所有,侵权必究! |
|||
*/ |
|||
|
|||
package com.elink.esua.epdc.service; |
|||
|
|||
import com.elink.esua.epdc.commons.mybatis.service.BaseService; |
|||
import com.elink.esua.epdc.entity.TokenEntity; |
|||
|
|||
/** |
|||
* 用户Token |
|||
* |
|||
* @author Mark sunlightcs@gmail.com |
|||
*/ |
|||
public interface TokenService extends BaseService<TokenEntity> { |
|||
|
|||
TokenEntity getByToken(String token); |
|||
|
|||
/** |
|||
* 生成token |
|||
* @param userId 用户ID |
|||
* @return 返回token信息 |
|||
*/ |
|||
TokenEntity createToken(Long userId); |
|||
|
|||
/** |
|||
* 设置token过期 |
|||
* @param userId 用户ID |
|||
*/ |
|||
void expireToken(Long userId); |
|||
|
|||
} |
|||
@ -1,38 +0,0 @@ |
|||
/** |
|||
* Copyright (c) 2018 人人开源 All rights reserved. |
|||
* |
|||
* https://www.renren.io
|
|||
* |
|||
* 版权所有,侵权必究! |
|||
*/ |
|||
|
|||
package com.elink.esua.epdc.service; |
|||
|
|||
import com.elink.esua.epdc.commons.mybatis.service.BaseService; |
|||
import com.elink.esua.epdc.commons.tools.utils.Result; |
|||
import com.elink.esua.epdc.dto.RegisterDTO; |
|||
import com.elink.esua.epdc.entity.UserEntity; |
|||
import com.elink.esua.epdc.dto.LoginDTO; |
|||
|
|||
import java.util.Map; |
|||
|
|||
/** |
|||
* 用户 |
|||
* |
|||
* @author Mark sunlightcs@gmail.com |
|||
*/ |
|||
public interface UserService extends BaseService<UserEntity> { |
|||
|
|||
UserEntity getByMobile(String mobile); |
|||
|
|||
UserEntity getUserByUserId(Long userId); |
|||
|
|||
/** |
|||
* 用户登录 |
|||
* @param dto 登录表单 |
|||
* @return 返回登录信息 |
|||
*/ |
|||
Map<String, Object> login(LoginDTO dto); |
|||
|
|||
Result register2(RegisterDTO dto); |
|||
} |
|||
@ -1,94 +0,0 @@ |
|||
/** |
|||
* Copyright (c) 2018 人人开源 All rights reserved. |
|||
* |
|||
* https://www.renren.io
|
|||
* |
|||
* 版权所有,侵权必究! |
|||
*/ |
|||
|
|||
package com.elink.esua.epdc.service.impl; |
|||
|
|||
import com.elink.esua.epdc.dao.TokenDao; |
|||
import com.elink.esua.epdc.service.TokenService; |
|||
import com.elink.esua.epdc.commons.mybatis.service.impl.BaseServiceImpl; |
|||
import com.elink.esua.epdc.dao.TokenDao; |
|||
import com.elink.esua.epdc.entity.TokenEntity; |
|||
import com.elink.esua.epdc.service.TokenService; |
|||
import org.springframework.stereotype.Service; |
|||
|
|||
import java.util.Date; |
|||
import java.util.UUID; |
|||
|
|||
|
|||
@Service |
|||
public class TokenServiceImpl extends BaseServiceImpl<TokenDao, TokenEntity> implements TokenService { |
|||
/** |
|||
* 12小时后过期 |
|||
*/ |
|||
private final static int EXPIRE = 3600 * 12; |
|||
|
|||
@Override |
|||
public TokenEntity getByToken(String token) { |
|||
return baseDao.getByToken(token); |
|||
} |
|||
|
|||
@Override |
|||
public TokenEntity createToken(Long userId) { |
|||
//当前时间
|
|||
Date now = new Date(); |
|||
//过期时间
|
|||
Date expireTime = new Date(now.getTime() + EXPIRE * 1000); |
|||
|
|||
//用户token
|
|||
String token; |
|||
|
|||
//判断是否生成过token
|
|||
TokenEntity tokenEntity = baseDao.getByUserId(userId); |
|||
if(tokenEntity == null){ |
|||
//生成一个token
|
|||
token = generateToken(); |
|||
|
|||
tokenEntity = new TokenEntity(); |
|||
tokenEntity.setUserId(userId); |
|||
tokenEntity.setToken(token); |
|||
tokenEntity.setUpdateDate(now); |
|||
tokenEntity.setExpireDate(expireTime); |
|||
|
|||
//保存token
|
|||
this.insert(tokenEntity); |
|||
}else{ |
|||
//判断token是否过期
|
|||
if(tokenEntity.getExpireDate().getTime() < System.currentTimeMillis()){ |
|||
//token过期,重新生成token
|
|||
token = generateToken(); |
|||
}else { |
|||
token = tokenEntity.getToken(); |
|||
} |
|||
|
|||
tokenEntity.setToken(token); |
|||
tokenEntity.setUpdateDate(now); |
|||
tokenEntity.setExpireDate(expireTime); |
|||
|
|||
//更新token
|
|||
this.updateById(tokenEntity); |
|||
} |
|||
|
|||
return tokenEntity; |
|||
} |
|||
|
|||
@Override |
|||
public void expireToken(Long userId){ |
|||
Date now = new Date(); |
|||
|
|||
TokenEntity tokenEntity = new TokenEntity(); |
|||
tokenEntity.setUserId(userId); |
|||
tokenEntity.setUpdateDate(now); |
|||
tokenEntity.setExpireDate(now); |
|||
|
|||
this.updateById(tokenEntity); |
|||
} |
|||
|
|||
private String generateToken(){ |
|||
return UUID.randomUUID().toString().replace("-", ""); |
|||
} |
|||
} |
|||
@ -1,91 +0,0 @@ |
|||
/** |
|||
* Copyright (c) 2018 人人开源 All rights reserved. |
|||
* <p> |
|||
* https://www.renren.io
|
|||
* <p> |
|||
* 版权所有,侵权必究! |
|||
*/ |
|||
|
|||
package com.elink.esua.epdc.service.impl; |
|||
|
|||
import com.elink.esua.epdc.commons.mybatis.service.impl.BaseServiceImpl; |
|||
import com.elink.esua.epdc.commons.tools.exception.ErrorCode; |
|||
import com.elink.esua.epdc.commons.tools.exception.RenException; |
|||
import com.elink.esua.epdc.commons.tools.utils.Result; |
|||
import com.elink.esua.epdc.commons.tools.validator.AssertUtils; |
|||
import com.elink.esua.epdc.dao.UserDao; |
|||
import com.elink.esua.epdc.dto.RegisterDTO; |
|||
import com.elink.esua.epdc.entity.TokenEntity; |
|||
import com.elink.esua.epdc.entity.UserEntity; |
|||
import com.elink.esua.epdc.dto.LoginDTO; |
|||
import com.elink.esua.epdc.feign.DemoFeignClient; |
|||
import com.elink.esua.epdc.service.TokenService; |
|||
import com.elink.esua.epdc.service.UserService; |
|||
import io.seata.spring.annotation.GlobalTransactional; |
|||
import org.apache.commons.codec.digest.DigestUtils; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.stereotype.Service; |
|||
|
|||
import java.util.Date; |
|||
import java.util.HashMap; |
|||
import java.util.Map; |
|||
|
|||
@Service |
|||
public class UserServiceImpl extends BaseServiceImpl<UserDao, UserEntity> implements UserService { |
|||
|
|||
@Autowired |
|||
private DemoFeignClient demoFeignClient; |
|||
|
|||
@Autowired |
|||
private TokenService tokenService; |
|||
|
|||
@Override |
|||
public UserEntity getByMobile(String mobile) { |
|||
return baseDao.getUserByMobile(mobile); |
|||
} |
|||
|
|||
@Override |
|||
public UserEntity getUserByUserId(Long userId) { |
|||
return baseDao.getUserByUserId(userId); |
|||
} |
|||
|
|||
@Override |
|||
public Map<String, Object> login(LoginDTO dto) { |
|||
UserEntity user = getByMobile(dto.getMobile()); |
|||
AssertUtils.isNull(user, ErrorCode.ACCOUNT_PASSWORD_ERROR); |
|||
|
|||
//密码错误
|
|||
if (!user.getPassword().equals(DigestUtils.sha256Hex(dto.getPassword()))) { |
|||
throw new RenException(ErrorCode.ACCOUNT_PASSWORD_ERROR); |
|||
} |
|||
|
|||
//获取登录token
|
|||
TokenEntity tokenEntity = tokenService.createToken(user.getId()); |
|||
|
|||
Map<String, Object> map = new HashMap<>(2); |
|||
map.put("token", tokenEntity.getToken()); |
|||
map.put("expire", tokenEntity.getExpireDate().getTime() - System.currentTimeMillis()); |
|||
|
|||
return map; |
|||
} |
|||
|
|||
@Override |
|||
@GlobalTransactional |
|||
public Result register2(RegisterDTO dto) { |
|||
UserEntity user = new UserEntity(); |
|||
user.setMobile(dto.getMobile()); |
|||
user.setUsername(dto.getMobile()); |
|||
user.setPassword(DigestUtils.sha256Hex(dto.getPassword())); |
|||
user.setCreateDate(new Date()); |
|||
boolean success = this.insert(user); |
|||
if (success) { |
|||
Result<String> result = demoFeignClient.seata(dto.getMobile(), 1405); |
|||
if (!result.success()) { |
|||
throw new RuntimeException("失败lelelelellele"); |
|||
} |
|||
return new Result(); |
|||
} |
|||
return new Result().error(); |
|||
} |
|||
|
|||
} |
|||
@ -1,14 +0,0 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> |
|||
|
|||
<mapper namespace="com.elink.esua.epdc.dao.TokenDao"> |
|||
|
|||
<select id="getByToken" resultType="com.elink.esua.epdc.entity.TokenEntity"> |
|||
select * from tb_token where token = #{value} |
|||
</select> |
|||
|
|||
<select id="getByUserId" resultType="com.elink.esua.epdc.entity.TokenEntity"> |
|||
select * from tb_token where user_id = #{value} |
|||
</select> |
|||
|
|||
</mapper> |
|||
@ -1,13 +0,0 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> |
|||
|
|||
<mapper namespace="com.elink.esua.epdc.dao.UserDao"> |
|||
|
|||
<select id="getUserByMobile" resultType="com.elink.esua.epdc.entity.UserEntity"> |
|||
select * from tb_user where mobile = #{value} |
|||
</select> |
|||
|
|||
<select id="getUserByUserId" resultType="com.elink.esua.epdc.entity.UserEntity"> |
|||
select * from tb_user where id = #{value} |
|||
</select> |
|||
</mapper> |
|||
Loading…
Reference in new issue