feat: auth模块初始化
This commit is contained in:
@ -15,7 +15,7 @@
|
||||
<!-- Project -->
|
||||
<dependency>
|
||||
<groupId>day.gitlab</groupId>
|
||||
<artifactId>dolphin-common-security</artifactId>
|
||||
<artifactId>dolphin-module-core</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
@ -23,6 +23,11 @@
|
||||
<artifactId>dolphin-module-rbac</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>day.gitlab</groupId>
|
||||
<artifactId>dolphin-module-auth</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<!-- Spring Boot -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
@ -52,6 +57,19 @@
|
||||
<artifactId>postgresql</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<!-- JWT -->
|
||||
<dependency>
|
||||
<groupId>io.jsonwebtoken</groupId>
|
||||
<artifactId>jjwt-api</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.jsonwebtoken</groupId>
|
||||
<artifactId>jjwt-impl</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.jsonwebtoken</groupId>
|
||||
<artifactId>jjwt-jackson</artifactId>
|
||||
</dependency>
|
||||
<!-- Lombok -->
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
|
||||
@ -1,25 +0,0 @@
|
||||
package day.gitlab.dolphin.authorize.controller;
|
||||
|
||||
import day.gitlab.dolphin.authorize.entity.dto.LoginDTO;
|
||||
import day.gitlab.dolphin.authorize.service.AuthorizeService;
|
||||
import day.gitlab.dolphin.common.core.entity.Result;
|
||||
import day.gitlab.dolphin.common.security.annotation.AuthorityIgnore;
|
||||
import jakarta.annotation.Resource;
|
||||
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;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/authorize")
|
||||
public class AuthorizeController {
|
||||
|
||||
@Resource
|
||||
private AuthorizeService authorizeService;
|
||||
|
||||
@AuthorityIgnore
|
||||
@PostMapping("/login")
|
||||
public Result login(@RequestBody LoginDTO loginDTO) {
|
||||
return Result.success(authorizeService.login(loginDTO));
|
||||
}
|
||||
}
|
||||
@ -1,11 +0,0 @@
|
||||
package day.gitlab.dolphin.authorize.entity.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class LoginDTO {
|
||||
|
||||
private String username;
|
||||
|
||||
private String password;
|
||||
}
|
||||
@ -1,11 +0,0 @@
|
||||
package day.gitlab.dolphin.authorize.entity.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class LoginVO {
|
||||
|
||||
private String accessToken;
|
||||
|
||||
private String refreshToken;
|
||||
}
|
||||
@ -1,10 +0,0 @@
|
||||
package day.gitlab.dolphin.authorize.service;
|
||||
|
||||
import day.gitlab.dolphin.authorize.entity.dto.LoginDTO;
|
||||
import day.gitlab.dolphin.authorize.entity.vo.LoginVO;
|
||||
import day.gitlab.dolphin.common.security.AuthenticationProvider;
|
||||
|
||||
public interface AuthorizeService extends AuthenticationProvider {
|
||||
|
||||
LoginVO login(LoginDTO loginDTO);
|
||||
}
|
||||
@ -1,71 +0,0 @@
|
||||
package day.gitlab.dolphin.authorize.service.impl;
|
||||
|
||||
import cn.hutool.v7.crypto.digest.BCrypt;
|
||||
import com.mybatisflex.core.query.QueryCondition;
|
||||
import day.gitlab.dolphin.authorize.entity.dto.LoginDTO;
|
||||
import day.gitlab.dolphin.authorize.entity.vo.LoginVO;
|
||||
import day.gitlab.dolphin.authorize.service.AuthorizeService;
|
||||
import day.gitlab.dolphin.common.core.exception.BusinessException;
|
||||
import day.gitlab.dolphin.common.security.UserPrincipal;
|
||||
import day.gitlab.dolphin.common.security.jwt.JwtInfo;
|
||||
import day.gitlab.dolphin.rbac.entity.Role;
|
||||
import day.gitlab.dolphin.rbac.entity.User;
|
||||
import day.gitlab.dolphin.rbac.entity.table.UserTableDef;
|
||||
import day.gitlab.dolphin.rbac.mapper.UserMapper;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
public class AuthorizeServiceImpl implements AuthorizeService {
|
||||
|
||||
@Resource
|
||||
private UserMapper userMapper;
|
||||
|
||||
@Resource
|
||||
private JwtInfo jwtInfo;
|
||||
|
||||
@Override
|
||||
public UserPrincipal getUserPrincipal(String userId) {
|
||||
User user = userMapper.selectOneById(userId);
|
||||
|
||||
return UserPrincipal.builder()
|
||||
.id(user.getId())
|
||||
.username(user.getUsername())
|
||||
.nickname(user.getNickname())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getUserAuthorities(String userId) {
|
||||
User user = userMapper.selectOneWithRelationsById(userId);
|
||||
|
||||
List<Role> roles = user.getRoles();
|
||||
if (roles == null || roles.isEmpty()) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
return roles.stream().map(Role::getCode).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public LoginVO login(LoginDTO loginDTO) {
|
||||
QueryCondition queryCondition = UserTableDef.USER.USERNAME.eq(loginDTO.getUsername());
|
||||
User user = userMapper.selectOneByCondition(queryCondition);
|
||||
|
||||
if (user == null || !BCrypt.checkpw(loginDTO.getPassword(), user.getPassword())) {
|
||||
throw new BusinessException("User name or password is incorrect");
|
||||
}
|
||||
if (!"1".equalsIgnoreCase(user.getEnabled())) {
|
||||
throw new BusinessException("User has been disabled");
|
||||
}
|
||||
|
||||
LoginVO loginVO = new LoginVO();
|
||||
loginVO.setAccessToken(jwtInfo.generateAccessToken(user.getId()));
|
||||
loginVO.setRefreshToken(jwtInfo.generateRefreshToken(user.getId()));
|
||||
return loginVO;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user