feat: auth模块初始化
This commit is contained in:
@ -0,0 +1,25 @@
|
||||
package day.gitlab.dolphin.auth.controller;
|
||||
|
||||
import day.gitlab.dolphin.auth.entity.dto.LoginDTO;
|
||||
import day.gitlab.dolphin.auth.service.AuthService;
|
||||
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("/auth")
|
||||
public class AuthController {
|
||||
|
||||
@Resource
|
||||
private AuthService authService;
|
||||
|
||||
@AuthorityIgnore
|
||||
@PostMapping("/login")
|
||||
public Result login(@RequestBody LoginDTO loginDTO) {
|
||||
return Result.success(authService.login(loginDTO));
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,14 @@
|
||||
package day.gitlab.dolphin.auth.entity.dto;
|
||||
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class LoginDTO {
|
||||
|
||||
@NotNull
|
||||
private String username;
|
||||
|
||||
@NotNull
|
||||
private String password;
|
||||
}
|
||||
@ -0,0 +1,17 @@
|
||||
package day.gitlab.dolphin.auth.entity.vo;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class LoginVO {
|
||||
|
||||
private String accessToken;
|
||||
|
||||
private String refreshToken;
|
||||
}
|
||||
@ -0,0 +1,9 @@
|
||||
package day.gitlab.dolphin.auth.service;
|
||||
|
||||
import day.gitlab.dolphin.auth.entity.dto.LoginDTO;
|
||||
import day.gitlab.dolphin.auth.entity.vo.LoginVO;
|
||||
|
||||
public interface AuthService {
|
||||
|
||||
LoginVO login(LoginDTO loginDTO);
|
||||
}
|
||||
@ -0,0 +1,69 @@
|
||||
package day.gitlab.dolphin.auth.service.impl;
|
||||
|
||||
import cn.hutool.v7.crypto.digest.BCrypt;
|
||||
import day.gitlab.dolphin.auth.entity.dto.LoginDTO;
|
||||
import day.gitlab.dolphin.auth.entity.vo.LoginVO;
|
||||
import day.gitlab.dolphin.auth.service.AuthService;
|
||||
import day.gitlab.dolphin.common.core.exception.BusinessException;
|
||||
import day.gitlab.dolphin.common.security.AuthenticationProvider;
|
||||
import day.gitlab.dolphin.common.security.UserPrincipal;
|
||||
import day.gitlab.dolphin.common.security.jwt.JwtInfo;
|
||||
import day.gitlab.dolphin.rbac.constants.UserEnabled;
|
||||
import day.gitlab.dolphin.rbac.entity.Role;
|
||||
import day.gitlab.dolphin.rbac.entity.User;
|
||||
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 AuthServiceImpl implements AuthService, AuthenticationProvider {
|
||||
|
||||
@Resource
|
||||
private UserMapper userMapper;
|
||||
|
||||
@Resource
|
||||
private JwtInfo jwtInfo;
|
||||
|
||||
@Override
|
||||
public LoginVO login(LoginDTO loginDTO) {
|
||||
User dbUser = userMapper.findByUsername(loginDTO.getUsername());
|
||||
if (dbUser == null || !BCrypt.checkpw(loginDTO.getPassword(), dbUser.getPassword())) {
|
||||
throw new BusinessException("Username or password is incorrect");
|
||||
}
|
||||
if (!UserEnabled.ENABLED.equals(dbUser.getEnabled())) {
|
||||
throw new BusinessException("User is not enabled");
|
||||
}
|
||||
|
||||
return LoginVO.builder()
|
||||
.accessToken(jwtInfo.generateAccessToken(dbUser.getId()))
|
||||
.refreshToken(jwtInfo.generateRefreshToken(dbUser.getId()))
|
||||
.build();
|
||||
}
|
||||
|
||||
@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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user