修改数据结构
This commit is contained in:
parent
3917956ccb
commit
06620dd5d2
@ -1,7 +1,7 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<groupId>io.renren</groupId>
|
||||
<artifactId>security</artifactId>
|
||||
<artifactId>java-admin-template</artifactId>
|
||||
<version>5.4.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
@ -1,8 +1,5 @@
|
||||
|
||||
|
||||
package io.modules.security.controller;
|
||||
|
||||
import io.common.exception.ErrorCode;
|
||||
import io.common.exception.RenException;
|
||||
import io.common.utils.IpUtils;
|
||||
import io.common.utils.Result;
|
||||
@ -33,14 +30,12 @@ import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 登录
|
||||
*
|
||||
|
||||
*/
|
||||
@RestController
|
||||
@Tag(name = "登录管理")
|
||||
@ -56,7 +51,7 @@ public class LoginController {
|
||||
@Parameter(in = ParameterIn.QUERY, ref = "string", name = "uuid", required = true)
|
||||
public void captcha(HttpServletResponse response, String uuid) throws IOException {
|
||||
//uuid不能为空
|
||||
AssertUtils.isBlank(uuid, "UUID不能为空!");
|
||||
AssertUtils.isBlank(uuid, "UUID不能为空~");
|
||||
//生成验证码
|
||||
captchaService.create(response, uuid);
|
||||
}
|
||||
@ -68,7 +63,7 @@ public class LoginController {
|
||||
//验证码是否正确
|
||||
boolean flag = captchaService.validate(login.getUuid(), login.getCaptcha());
|
||||
if (!flag) {
|
||||
return new Result().error("验证码不正确!");
|
||||
return new Result().error("验证码不正确~");
|
||||
}
|
||||
//用户信息
|
||||
SysUserDTO user = sysUserService.getByUsername(login.getUsername());
|
||||
@ -82,7 +77,7 @@ public class LoginController {
|
||||
log.setStatus(LoginStatusEnum.FAIL.value());
|
||||
log.setCreatorName(login.getUsername());
|
||||
sysLogLoginService.save(log);
|
||||
throw new RenException("用户不存在");
|
||||
throw new RenException("用户不存在~");
|
||||
}
|
||||
//密码错误
|
||||
if (!PasswordUtils.matches(login.getPassword(), user.getPassword())) {
|
||||
@ -90,7 +85,7 @@ public class LoginController {
|
||||
log.setCreator(user.getId());
|
||||
log.setCreatorName(user.getUsername());
|
||||
sysLogLoginService.save(log);
|
||||
throw new RenException("密码错误!");
|
||||
throw new RenException("密码错误~");
|
||||
}
|
||||
//账号停用
|
||||
if (user.getStatus() == UserStatusEnum.DISABLE.value()) {
|
||||
@ -98,7 +93,7 @@ public class LoginController {
|
||||
log.setCreator(user.getId());
|
||||
log.setCreatorName(user.getUsername());
|
||||
sysLogLoginService.save(log);
|
||||
throw new RenException("账号停用");
|
||||
throw new RenException("账号停用~");
|
||||
}
|
||||
//登录成功
|
||||
log.setStatus(LoginStatusEnum.SUCCESS.value());
|
||||
@ -112,10 +107,8 @@ public class LoginController {
|
||||
@Operation(summary = "退出")
|
||||
public Result logout(HttpServletRequest request) {
|
||||
UserDetail user = SecurityUser.getUser();
|
||||
|
||||
//退出
|
||||
sysUserTokenService.logout(user.getId());
|
||||
|
||||
//用户信息
|
||||
SysLogLoginEntity log = new SysLogLoginEntity();
|
||||
log.setOperation(LoginOperationEnum.LOGOUT.value());
|
||||
@ -127,8 +120,6 @@ public class LoginController {
|
||||
log.setCreatorName(user.getUsername());
|
||||
log.setCreateDate(new Date());
|
||||
sysLogLoginService.save(log);
|
||||
|
||||
return new Result();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -19,7 +19,7 @@ public class LoginDTO implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Schema(title = "用户名", required = true)
|
||||
@NotBlank(message="{sysuser.username.require}")
|
||||
@NotBlank(message="用户名不能为空~")
|
||||
private String username;
|
||||
|
||||
@Schema(title = "密码")
|
||||
|
@ -23,7 +23,6 @@ import java.io.IOException;
|
||||
/**
|
||||
* oauth2过滤器
|
||||
*
|
||||
|
||||
*/
|
||||
public class Oauth2Filter extends AuthenticatingFilter {
|
||||
|
||||
@ -31,11 +30,9 @@ public class Oauth2Filter extends AuthenticatingFilter {
|
||||
protected AuthenticationToken createToken(ServletRequest request, ServletResponse response) throws Exception {
|
||||
//获取请求token
|
||||
String token = getRequestToken((HttpServletRequest) request);
|
||||
|
||||
if (StrUtil.isBlank(token)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new Oauth2Token(token);
|
||||
}
|
||||
|
||||
@ -57,11 +54,8 @@ public class Oauth2Filter extends AuthenticatingFilter {
|
||||
httpResponse.setContentType("application/json;charset=utf-8");
|
||||
httpResponse.setHeader("Access-Control-Allow-Credentials", "true");
|
||||
httpResponse.setHeader("Access-Control-Allow-Origin", HttpContextUtils.getOrigin());
|
||||
|
||||
String json = JsonUtils.toJsonString(new Result().error("未授权访问!"));
|
||||
|
||||
httpResponse.getWriter().print(json);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -94,12 +88,10 @@ public class Oauth2Filter extends AuthenticatingFilter {
|
||||
private String getRequestToken(HttpServletRequest httpRequest) {
|
||||
//从header中获取token
|
||||
String token = httpRequest.getHeader(Constant.TOKEN_HEADER);
|
||||
|
||||
//如果header中不存在token,则从参数中获取token
|
||||
if (StrUtil.isBlank(token)) {
|
||||
token = httpRequest.getParameter(Constant.TOKEN_HEADER);
|
||||
}
|
||||
|
||||
return token;
|
||||
}
|
||||
|
||||
|
@ -7,7 +7,6 @@ import org.apache.shiro.authc.AuthenticationToken;
|
||||
/**
|
||||
* token
|
||||
*
|
||||
|
||||
*/
|
||||
public class Oauth2Token implements AuthenticationToken {
|
||||
private String token;
|
||||
|
@ -10,7 +10,6 @@ import java.util.UUID;
|
||||
/**
|
||||
* 生成token
|
||||
*
|
||||
|
||||
*/
|
||||
public class TokenGenerator {
|
||||
|
||||
|
@ -3,7 +3,7 @@ spring:
|
||||
druid:
|
||||
#MySQL
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
url: jdbc:mysql://localhost:3306/wenhua?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true
|
||||
url: jdbc:mysql://localhost:3306/admin-template?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true
|
||||
username: root
|
||||
password: 123456
|
||||
initial-size: 10
|
||||
|
@ -5,9 +5,9 @@ server:
|
||||
threads:
|
||||
max: 1000
|
||||
min-spare: 30
|
||||
port: 8080
|
||||
port: 18080
|
||||
servlet:
|
||||
context-path: /admin
|
||||
context-path: /
|
||||
session:
|
||||
cookie:
|
||||
http-only: true
|
||||
|
@ -1,7 +1,7 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<groupId>io.renren</groupId>
|
||||
<artifactId>security</artifactId>
|
||||
<artifactId>java-admin-template</artifactId>
|
||||
<version>5.4.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
@ -1,7 +1,7 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<groupId>io.renren</groupId>
|
||||
<artifactId>security</artifactId>
|
||||
<artifactId>java-admin-template</artifactId>
|
||||
<version>5.4.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
@ -34,10 +34,13 @@ public class UserController {
|
||||
@PostMapping("register")
|
||||
@Operation(summary = "注册")
|
||||
public Result register(@RequestBody RegisterDTO dto) {
|
||||
if (userService.getByUsername(dto.getUsername()) != null) {
|
||||
return new Result().error("用户名已经存在~");
|
||||
}
|
||||
//表单校验
|
||||
ValidatorUtils.validateEntity(dto);
|
||||
UserEntity user = new UserEntity();
|
||||
user.setMobile(dto.getMobile());
|
||||
user.setUsername(dto.getUsername());
|
||||
user.setNickName(dto.getNickName());
|
||||
user.setPassword(DigestUtil.sha256Hex(dto.getPassword()));
|
||||
user.setCreateDate(new Date());
|
||||
@ -56,14 +59,14 @@ public class UserController {
|
||||
return new Result().ok(map);
|
||||
}
|
||||
|
||||
@PostMapping("updateInfo")
|
||||
@PutMapping("update")
|
||||
@Operation(summary = "修改信息")
|
||||
public Result update(@RequestBody RegisterDTO dto) {
|
||||
//表单校验
|
||||
ValidatorUtils.validateEntity(dto);
|
||||
UserEntity user = new UserEntity();
|
||||
user.setId(dto.getId());
|
||||
user.setMobile(dto.getMobile());
|
||||
user.setUsername(dto.getUsername());
|
||||
user.setNickName(dto.getNickName());
|
||||
user.setIntroduce(dto.getIntroduce());
|
||||
user.setPassword(DigestUtil.sha256Hex(dto.getPassword()));
|
||||
@ -93,11 +96,4 @@ public class UserController {
|
||||
public Result<Long> userInfo(@Parameter(hidden = true) @RequestAttribute("userId") Long userId) {
|
||||
return new Result<Long>().ok(userId);
|
||||
}
|
||||
|
||||
@GetMapping("notToken")
|
||||
@Operation(summary = "忽略Token验证测试")
|
||||
public Result<String> notToken() {
|
||||
return new Result<String>().ok("无需token也能访问。。。");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ import org.apache.ibatis.annotations.Mapper;
|
||||
*/
|
||||
@Mapper
|
||||
public interface UserDao extends BaseDao<UserEntity> {
|
||||
UserEntity getUserByMobile(String mobile);
|
||||
UserEntity getUserByUsername(String username);
|
||||
|
||||
UserEntity getUserByUserId(Long userId);
|
||||
}
|
||||
|
@ -3,17 +3,6 @@ package io.dto;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
|
||||
|
||||
package io.dto;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import lombok.Data;
|
||||
|
||||
|
||||
/**
|
||||
* 登录表单
|
||||
*
|
||||
@ -22,12 +11,12 @@ import lombok.Data;
|
||||
@Data
|
||||
@Schema(title = "登录表单")
|
||||
public class LoginDTO {
|
||||
@Schema(title = "手机号")
|
||||
@NotBlank(message="手机号不能为空")
|
||||
private String mobile;
|
||||
@Schema(title = "用户名")
|
||||
@NotBlank(message="用户名不能为空~")
|
||||
private String username;
|
||||
|
||||
@Schema(title = "密码")
|
||||
@NotBlank(message="密码不能为空")
|
||||
@NotBlank(message="密码不能为空~")
|
||||
private String password;
|
||||
|
||||
}
|
||||
|
@ -10,21 +10,19 @@ import lombok.Data;
|
||||
/**
|
||||
* 注册表单
|
||||
*
|
||||
|
||||
*/
|
||||
@Data
|
||||
@Schema(title = "注册表单")
|
||||
public class RegisterDTO {
|
||||
private Long id;
|
||||
@Schema(title = "手机号")
|
||||
@NotBlank(message="手机号不能为空")
|
||||
private String mobile;
|
||||
@Schema(title = "用户名")
|
||||
@NotBlank(message="用户名不能为空")
|
||||
private String username;
|
||||
|
||||
@Schema(title = "密码")
|
||||
@NotBlank(message="密码不能为空")
|
||||
private String password;
|
||||
|
||||
|
||||
@Schema(title = "昵称")
|
||||
private String nickName;
|
||||
|
||||
|
@ -34,9 +34,9 @@ public class UserEntity implements Serializable {
|
||||
*/
|
||||
private String introduce;
|
||||
/**
|
||||
* 手机号
|
||||
* 用户名
|
||||
*/
|
||||
private String mobile;
|
||||
private String username;
|
||||
/**
|
||||
* 密码
|
||||
*/
|
||||
|
@ -37,28 +37,22 @@ public class AuthorizationInterceptor implements HandlerInterceptor {
|
||||
return true;
|
||||
}
|
||||
//从header中获取token
|
||||
String token = request.getHeader("token");
|
||||
|
||||
|
||||
String token = request.getHeader("Authorization");
|
||||
//如果header中不存在token,则从参数中获取token
|
||||
if (StrUtil.isBlank(token)) {
|
||||
token = request.getParameter("token");
|
||||
token = request.getParameter("Authorization");
|
||||
}
|
||||
|
||||
//token为空
|
||||
if (StrUtil.isBlank(token)) {
|
||||
throw new RenException("token为空!");
|
||||
throw new RenException("登录失效~");
|
||||
}
|
||||
|
||||
//查询token信息
|
||||
TokenEntity tokenEntity = tokenService.getByToken(token);
|
||||
if (tokenEntity == null || tokenEntity.getExpireDate().getTime() < System.currentTimeMillis()) {
|
||||
throw new RenException(401,"登录失效!");
|
||||
throw new RenException(401,"登录失效~");
|
||||
}
|
||||
|
||||
//设置userId到request里,后续根据userId,获取用户信息
|
||||
request.setAttribute(USER_KEY, tokenEntity.getUserId());
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ import java.util.Map;
|
||||
*/
|
||||
public interface UserService extends BaseService<UserEntity> {
|
||||
|
||||
UserEntity getByMobile(String mobile);
|
||||
UserEntity getByUsername(String username);
|
||||
|
||||
UserEntity getUserByUserId(Long userId);
|
||||
|
||||
|
@ -22,8 +22,8 @@ public class UserServiceImpl extends BaseServiceImpl<UserDao, UserEntity> implem
|
||||
private final TokenService tokenService;
|
||||
|
||||
@Override
|
||||
public UserEntity getByMobile(String mobile) {
|
||||
return baseDao.getUserByMobile(mobile);
|
||||
public UserEntity getByUsername(String username){
|
||||
return baseDao.getUserByUsername(username);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -33,21 +33,18 @@ public class UserServiceImpl extends BaseServiceImpl<UserDao, UserEntity> implem
|
||||
|
||||
@Override
|
||||
public Map<String, Object> login(LoginDTO dto) {
|
||||
UserEntity user = getByMobile(dto.getMobile());
|
||||
AssertUtils.isNull(user, ErrorCode.ACCOUNT_PASSWORD_ERROR);
|
||||
UserEntity user = getByUsername(dto.getUsername());
|
||||
AssertUtils.isNull(user, "用户名不存在~");
|
||||
|
||||
//密码错误
|
||||
if (!user.getPassword().equals(DigestUtil.sha256Hex(dto.getPassword()))) {
|
||||
throw new RenException("密码错误!");
|
||||
throw new RenException("密码输入错误~");
|
||||
}
|
||||
|
||||
//获取登录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;
|
||||
}
|
||||
|
||||
|
@ -6,7 +6,7 @@ spring:
|
||||
datasource:
|
||||
druid:
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
url: jdbc:mysql://localhost:3306/wenhua?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true
|
||||
url: jdbc:mysql://localhost:3306/admin-template?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true
|
||||
username: root
|
||||
password: 123456
|
||||
initial-size: 10
|
||||
|
@ -5,9 +5,9 @@ server:
|
||||
threads:
|
||||
max: 1000
|
||||
min-spare: 30
|
||||
port: 8081
|
||||
port: 18081
|
||||
servlet:
|
||||
context-path: /front
|
||||
context-path: /
|
||||
session:
|
||||
cookie:
|
||||
http-only: true
|
||||
|
@ -3,8 +3,8 @@
|
||||
|
||||
<mapper namespace="io.dao.UserDao">
|
||||
|
||||
<select id="getUserByMobile" resultType="io.entity.UserEntity">
|
||||
select * from tb_user where mobile = #{value}
|
||||
<select id="getUserByUsername" resultType="io.entity.UserEntity">
|
||||
select * from tb_user where username = #{value}
|
||||
</select>
|
||||
|
||||
<select id="getUserByUserId" resultType="io.entity.UserEntity">
|
||||
|
@ -8,7 +8,7 @@
|
||||
|
||||
<parent>
|
||||
<groupId>io.renren</groupId>
|
||||
<artifactId>security</artifactId>
|
||||
<artifactId>java-admin-template</artifactId>
|
||||
<version>5.4.0</version>
|
||||
</parent>
|
||||
|
||||
|
26
pom.xml
26
pom.xml
@ -2,7 +2,7 @@
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>io.renren</groupId>
|
||||
<artifactId>security</artifactId>
|
||||
<artifactId>java-admin-template</artifactId>
|
||||
<version>5.4.0</version>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
@ -73,29 +73,7 @@
|
||||
<groupId>com.mysql</groupId>
|
||||
<artifactId>mysql-connector-j</artifactId>
|
||||
</dependency>
|
||||
<!-- oracle驱动 -->
|
||||
<dependency>
|
||||
<groupId>com.oracle</groupId>
|
||||
<artifactId>ojdbc6</artifactId>
|
||||
<version>${oracle.version}</version>
|
||||
</dependency>
|
||||
<!-- sqlserver驱动 -->
|
||||
<dependency>
|
||||
<groupId>com.microsoft.sqlserver</groupId>
|
||||
<artifactId>sqljdbc4</artifactId>
|
||||
<version>${sqlserver.version}</version>
|
||||
</dependency>
|
||||
<!-- postgresql驱动 -->
|
||||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
</dependency>
|
||||
<!-- 达梦驱动 -->
|
||||
<dependency>
|
||||
<groupId>com.dameng</groupId>
|
||||
<artifactId>DmJdbcDriver18</artifactId>
|
||||
<version>${dameng.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>druid-spring-boot-3-starter</artifactId>
|
||||
|
Loading…
Reference in New Issue
Block a user